无法使用 php 使用 gmail 发送电子邮件 - 授权错误

Can't send email with php using gmail - Authorising error

我是 PHP 的新手,正在尝试建立一个带有注册表单的网站。用户应该会收到一封带有确认 link 的电子邮件(因为我有一个免费的托管服务器,所以我正在尝试使用 gmail 服务器)。问题是我正在努力处理电子邮件 php 代码。我正在尝试使用 PHPMailer 函数,如下所示:

<?php
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "MYEMAIL@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "MYPASSWORD";

//Set who the message is to be sent from
$mail->setFrom('example@example.com', 'Name S');

//Set an alternative reply-to address
$mail->addReplyTo('example@example.com', 'Name S');

//Set who the message is to be sent to
$mail->addAddress('email@gmail.com', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("HELLo");

//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}
?>

我尝试了端口 587 和 465。还尝试了 ssl 和 tls。每次我尝试 运行 代码时,我都会收到以下错误:

SERVER -> CLIENT: 220 smtp.gmail.com ESMTP gw4sm17090153wjc.45 - gsmtp CLIENT -> SERVER: EHLO spec.dr-manny.co.uk SERVER -> CLIENT: 250-smtp.gmail.com at your service, [185.27.134.36]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8 CLIENT -> SERVER: AUTH LOGIN SERVER -> CLIENT: 334 VXNlcm5hbWU6 CLIENT -> SERVER: bWFubnlzYWVkaUBnbWFpbC5jb20= SERVER -> CLIENT: 334 UGFzc3dvcmQ6 CLIENT -> SERVER: a2luZ29uZW1vaDk5 SERVER -> CLIENT: 534-5.7.14 Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 - gsmtp SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 - gsmtp SMTP Error: Could not authenticate. CLIENT -> SERVER: QUIT SERVER -> CLIENT: 221 2.0.0 closing connection gw4sm17090153wjc.45 - gsmtp SMTP connect() failed. Error: SMTP connect() failed.

另外,我从 Gmail 收到了一封主题为 "Someone has your password" 的电子邮件。我打开电子邮件,发现这个“
嗨曼尼, 有人刚刚使用您的密码尝试登录您的 Google 帐户 MYEMAIL@gmail.com,使用的是电子邮件客户端或移动设备等应用程序。” 我在 运行 php 页面后大约 15 分钟收到了这封电子邮件。

我关闭了“两步验证”并关闭了"allow less secure apps"。似乎没有任何帮助。有人可以帮助我吗?

我去年遇到了同样的问题:代码 (VB.Net) 没问题,所以凭据在哪里。问题是 gmail 不喜欢远程网络服务器(我的主机)中尝试使用相同用户名的某些应用程序。

我通过以下方式修复了它(在两个不同的场合):

之后再次测试您的代码。