首页 > 解决方案 > 为什么不使用 PHPMailer 发送电子邮件?

问题描述

有谁知道我在这里的代码哪里出错了?可能是因为我使用本地主机发送?或者是其他东西?

当我单击带有不正确电子邮件的重置密码按钮时,会出现消息,但是当我输入正确的电子邮件时,什么也没有显示,也没有发送电子邮件。使用 hotmail 发送电子邮件,更改了电子邮件帐户上的 IMAP 设置。

PHP:

include_once(ROOT_PATH . "/app/database/db.php");
use PHPMailer\PHPMailer\PHPMailer;

if(isset($_POST['email'])) {
  global $conn;

  $email = $_POST['email'];

  $sql = $conn->query("SELECT u_id FROM users WHERE email='$email'");
  if ($sql->num_rows > 0) {

    $token = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioplkjhgfdsazxcvbnm1234567890£$%&?@!#";
    $token = str_shuffle($token);
    $token = substr($token, 0 , 20);

    $conn->query("UPDATE users SET token='$token',
      tokenExpire=DATE_ADD(NOW(), INTERVAL 5 MINUTE)
      WHERE email='$email'");

      require_once(ROOT_PATH . "/app/includes/phpmailer/PHPMailer.php");
      require_once(ROOT_PATH . "/app/includes/phpmailer/Exception.php");

      $mail = new PHPMailer();
      $mail->SMPTDebug = 3;
      $mail->Host = "";
      $mail->SMPTAuth = true;
      $mail->Username = "********";
      $mail->Password = "********";
      $mail->SMTPSecure = "tls";
      $mail->Port = 587;
      $mail->addAddress($email);
      $mail->From("********");
      $mail->FromName = "Cath";
      $mail->Subject = "Reset Password";
      $mail->isHTML(true);
      $mail->smtpConnect(
        array(
          "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false,
            "allow_self_signed" => true
          )
        )
      );

      $mail->Body = "
        Hi,<br><br>

        In order to reset your password, please click on the link below:<br>
        <a href='
        http://localhost/weinspire/blog/resetPassword.php?email=$email&token=$token
        '>http://localhost/weinspire/blog/resetPassword.php?email=$email&token=$token</a><br><br>

        Thanks,<br><br>

        Seeing The New
      ";

      if($mail->send())
        exit(json_encode(array("status" => 1, "msg" => 'Please check your email inbox.')));
      else
        exit(json_encode(array("status" => 0, "msg" => 'Something has gone wrong.')));
  }   else
        exit(json_encode(array("status" => 0, "msg" => 'Please check your inputs.')));
}

HTML:

  <div class="fp-container">
    <h1 class="top-banner">Forgotten Password</h1>
    <h2>Forgotten your password?</h2>
    <h3>Don't worry - simply type in your email address, click <span class="btn">reset password</span> and we'll send you an email to reset your password.</h3>
    <div class="fp-form">
       <input id="email" type="text" name="email" placeholder="Email">
       <button class="btn reset-btn" type="submit" name="button">Reset Password</button>
       <br><br>
       <p id="response"></p>
    </div>
  </div>

谢谢你的帮助。

标签: phpphpmailer

解决方案


推荐阅读