何解决 SMTP 连接失败错误

Ho to resolve SMTP connect failed error

我正在尝试使用 PHPMailer 发送带有 pdf 附件的邮件。

代码:

    include_once("./PHPMailer/PHPMailerAutoload.php");
    $mail = new PHPMailer(); // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 587; // or 587
    $mail->IsHTML(true);
    $mail->Username = "username@gmail.com";
    $mail->Password = "accual password";
    $mail->SetFrom("test1@gmail.com");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("test2@gmail.com");

    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message has been sent";
    }

错误:

2016-10-25 10:40:27 CLIENT -> SERVER: EHLO test.something.com
2016-10-25 10:40:27 CLIENT -> SERVER: STARTTLS
2016-10-25 10:40:27 CLIENT -> SERVER: EHLO test.something.com
2016-10-25 10:40:27 CLIENT -> SERVER: AUTH LOGIN
2016-10-25 10:40:27 CLIENT -> SERVER: ... user name removed ... 
2016-10-25 10:40:27 CLIENT -> SERVER: ... password removed ...
2016-10-25 10:40:27 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials cy7sm4615595wjc.26 - gsmtp
2016-10-25 10:40:27 SMTP Error: Could not authenticate.
2016-10-25 10:40:27 CLIENT -> SERVER: QUIT
2016-10-25 10:40:27 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshootingthanks

如何解决这个问题?我感谢所有的回应。先谢谢了。

错误消息非常清楚地说明了(最有可能的)问题是什么:

Username and Password not accepted.

所以仔细检查用户名和密码是否正确,并且没有任何字符集问题。尝试快速搜索以找出 STMP 服务器期望的字符集类型,但找不到任何具体信息。

此外,您可能需要检查错误消息中包含的 link 以及 the gmail SMTP support page。更具体地说,这个位:

To connect with SSL, you need to provide a Google username and password for authentication. Ensure that the username you use has cleared the CAPTCHA word verification test that appears when the user first logs in. We also recommend ensuring that the account has a secure password.

请阅读您收到的错误消息。他们明确指出您的密码不正确。由于 SMTP 输出确实包含您的用户名和密码,因此我尝试登录,但 gmail 报告您的密码不正确:

请立即更改您的密码并清除错误消息,然后再将它们添加到问题中