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

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

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

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;
                    }

除了您注释掉起作用的那一行之外,您使用的引号类型错误 - 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网址)。

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

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