繁体   English   中英

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

[英]unable to send token and email value in mail using phpmailer for forgot password

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

除了注释掉行的事实之外,您还使用了错误的引号类型- 单引号不支持变量插值 为了避免字符串中引号类型发生冲突(例如,在属性处使用引号),请使用单引号或转义双引号:

$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';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM