无法使用phpmailer发送邮件中的令牌和电子邮件值以忘记密码

时间:2019-02-04 08:29:10

标签: php mysql mysqli phpmailer

我想将$ 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;
                    }

1 个答案:

答案 0 :(得分:1)

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

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div style="margin:0;padding:2rem 2rem;min-height:100vh;display: flex;align-items: center;">
  <div class="row" style="display: flex;align-items: start;">
  <div class="col-md-4 p-0" style="text-align: center;">
    <img class="static" style="width: 85%;" src="https://cdn.makeawebsitehub.com/wp-content/uploads/2016/02/learn-code-e1455713167295.jpg" alt="image 1"><img class="active" style="width: 85%;" src="https://c.pxhere.com/photos/20/e5/programming_computer_environment_syntax_highlight_program_computing_display_hacker-1172040.jpg!d"alt="image 2">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, distinctio, cum iusto esse pariatur consectetur eligendi itaque quos totam voluptas rem mollitia voluptatem veniam reiciendis omnis, saepe eaque animi qui.</p>
  </div>
</div>
</div>

或:

$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中使用$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>"; 作为主机名对您而言不起作用,除了您-您需要在其中输入真实的主机名,并且应该使用TLS(即localhost URL)

此外,从电子邮件主机名中删除前导空格:

https