PHPMailer不会发送忘记用户名页面的邮件

时间:2017-06-21 06:33:24

标签: php email

构建忘记用户名页面:

使用php文件将在localhost xampp下托管。 使用Gmail电子邮件地址将邮件发送到另一个使用用户名

的Gmail地址

没有错误,只是发送邮件。

我在包含目录中的Github中添加了以下文件。

  1. PHPMailerAutoload.php,
  2. class.phpmailer.php,
  3. class.smtp.php,
  4. class.phpmaileroauth.php,
  5. class.phpmaileroauthgoogle.php,
  6. class.pop3.php
  7. 然后在里面包含创建的目录" forgot_username_inc.php"更新 这是它的PHP代码

    <?php
     include_once 'db_connect.php';
     include_once 'psl-config.php';
     require_once 'PHPMailerAutoload.php';
     require_once 'class.phpmailer.php';
     require_once 'class.smtp.php';
     require_once 'class.phpmaileroauth.php';
     require_once 'class.phpmaileroauthgoogle.php';
     require_once 'class.pop3.php';
     $error_msg_username = "";
    
      if (isset($_POST['email'])) {
    // Sanitize and validate the data passed in
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Not a valid email
        $error_msg_username .= '<p class="error">The email address you entered 
        is not valid</p>';
    }
    
    $prep_stmt = "SELECT username FROM client WHERE email = ? LIMIT 1";
    $stmt = $mysqli->prepare($prep_stmt);
    
      // check existing email  
      if ($stmt) {
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $stmt->store_result();
    
        if (!($stmt->num_rows == 1)) {
            // If no user with such email
            $error_msg_username .= '<p class="error">No User Registered with that Email</p>';
                        $stmt->close();
        }      
    
    } 
    
    else {
        $error_msg_username .= '<p class="error">Database error Line 39</p>';
                $stmt->close();
    }
    
      if (empty($error_msg_username)) {
    
      if($stmt_username = $mysqli->prepare("SELECT username FROM *** WHERE email = ? LIMIT 1")) {
       $stmt_username->bind_param('s', $email);
       $stmt_username->execute();
       $stmt_username->store_result();
       $stmt_username->bind_result($username);
    
       // user found
        if ($stmt_username->num_rows == 1) {
        // sending mail
            $mail = new PHPMailer;
            $mail->isSMTP();                                      // Set mailer to use SMTP
     $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP 
    authentication
       $mail->Port     = 587;                    // set the SMTP port for the GMAIL 
        server
    $mail->Username = 'example@gmail.com';                 // SMTP username
    $mail->Password = 'password';                           // SMTP password
       $mail->SMTPSecure = 'tls';                            // Enable encryption, 
        'ssl' also accepted
      $mail->SMTPDebug = 2;
      $mail->From = 'example@gmail.com';
      $mail->FromName = 'Admin';
      $mail->addAddress($email);        // Name is optional
      $mail->WordWrap = 50;                                 // Set word wrap to 50 
     characters
       $mail->isHTML(true);                                  // Set email format to 
      HTML
    
     $mail->Subject = 'User Name Forgot';
     $mail->Body    = 'Here is your UserName'.$username;
     $mail->AltBody = 'Here is your UserName'.$username;
    
      if(!$mail->send()) {
      echo 'Message could not be sent.';
      echo 'Mailer Error: ' . $mail->ErrorInfo;
      } else {
    echo 'Message has been sent';
     }
     //---End of Mail       
        } // end of userfound
       } // end of prepare statement
    
        if (! $stmt_username->execute()) {
                header('Location: ../error.php?err=Username Search: FAILED');
                exit();
            }
            $stmt_username->close();
        header('Location: ./forgot_username_success.php');
        exit();
    }
    
        }
        ?>
    

    显示错误:

      

    请通过您的网络浏览器登录无法发送消息.Mailer错误:SMTP连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

    我做了什么:禁用双向身份验证。允许不太安全的应用访问。 清除浏览器缓存。我也是#34;解锁CAPTCHA&#34; https://accounts.google.com/DisplayUnlockCaptcha 仍然失败。

    更新 我使用GMX而不是gmail&amp;现在它成功发送邮件。

0 个答案:

没有答案