【问题标题】:unable to send token and email value in mail using phpmailer for forgot password无法使用 phpmailer 在邮件中发送令牌和电子邮件值以忘记密码
【发布时间】:2019-02-04 08:29:09
【问题描述】:

我想将 $email 和 $token 的值与邮件一起发送给发件人,但它没有发送。 这是我在电子邮件中收到的链接:

http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token

这并没有显示 $token 和 $femail 的值。 谁能帮忙??

$token = "qwertyuiopasdfghjklzxcvbnm1234567890jksdhfljdhfajlsdbhkfdajsfhaljsdfhb";
                    $token = str_shuffle($token);
                    $token = substr($token, 0,10);
//                  $femail = '';

                    $stmt_i = $conn->prepare("UPDATE users SET token=?, tokenExpire=DATE_ADD(NOW(), INTERVAL 5 MINUTE) WHERE email=?");
                    $stmt_i->bind_param("ss", $token, $femail);
                    $stmt_i->execute();
                    $result = $stmt_i->affected_rows;
//                    echo $result;



                    //Load Composer's autoloader
                    // require '.../vendor/autoload.php';

                    require '../PHPMailer/src/PHPMailer.php';
                    require '../PHPMailer/src/Exception.php';

                    require '../PHPMailer/src/SMTP.php';

                    $mail = new PHPMailer(true);
//                  echo $femail.$token;
                    try {
                        //Server settings
//                      $mail->SMTPDebug = 2;
                        $mail->isSMTP();                                      
                        $mail->Host = ' smtp.gmail.com';
                        $mail->SMTPAuth = true;

                        $mail->Username = 'myusername';
                        $mail->Password = 'mypassword';

                        $mail->Port = 587; 
                        $mail->SMTPSecure = 'tls';
                        //Recipients
                        $mail->setFrom('email', 'name');
                        $mail->addAddress($femail);     // Add a recipient

                        //Content
                        $mail->isHTML(true);
                        $mail->Subject = 'Reset Password';`enter code here`                     $mail->Body    = '<h3>Click the link to reset your password</h3><br><a href= "http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token">http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token</a><br><h3>Regards<br>Moon</h3>';

//                      $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

                        $mail->send();
                        echo 'Message has been sent';
                    } catch (Exception $e) {
                        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
                    }

【问题讨论】:

    标签: php mysql mysqli phpmailer


    【解决方案1】:

    除了您注释掉了起作用的行之外,您还使用了错误类型的引号 - single quotes do not support variable interpolation。为避免字符串中的引号类型冲突(例如,在引用属性的位置),请使用单引号或转义双引号:

    $mail->Body = "<h3>Click the link to reset your password</h3><br><a href= 'http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token'>http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token</a><br><h3>Regards<br>Moon</h3>";
    

    或:

    $mail->Body = "<h3>Click the link to reset your password</h3><br><a href= \"http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token\">http://localhost/admin-dashboard/resetPassword.php?email=$femail&token=$token</a><br><h3>Regards<br>Moon</h3>";
    

    请注意,在这些 URL 中使用 localhost 作为主机名将不适用于除您之外的任何人 - 您需要在其中使用真实的主机名,并且您应该使用 TLS(即 https URL)。

    另外,删除电子邮件主机名的前导空格:

    $mail->Host = 'smtp.gmail.com';
    

    【讨论】:

    • 如果您对此感到满意,请将其标记为您选择的答案 - 单击它旁边的复选标记。否则将被视为“未答复”。
    猜你喜欢
    • 2016-08-31
    • 2011-02-19
    • 2014-10-25
    • 2015-06-22
    • 2013-06-13
    • 2013-03-04
    • 2015-10-21
    • 2012-02-29
    • 1970-01-01
    相关资源
    最近更新 更多