php - SMTP 错误的问题:无法验证
问题描述
SMTP
使用新的 phpmailer 版本时出现此错误:
SMTP 错误:无法验证。
我已经尝试使用SSL
而不是TLS
使用端口号 465,并且我已将 gmail 设置为允许安全性较低的应用程序,但这些都没有影响我是否遗漏了什么?
这是代码:
class gmail_xoauth
{
public static function sendMail($subject,$body,$address)
{
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_CLIENT;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
//Fill in authentication details here
//Either the gmail account owner, or the user that gave consent
$email = 'myemail@gmail.com';
$clientId = 'clientid';
$clientSecret = 'clientSecret';
//Obtained by configuring and running get_oauth_token.php
//after setting up an app in Google Developer Console.
$refreshToken = 'refreshToken';
//Create a new OAuth2 provider instance
$provider = new Google(
[
'clientId' => $clientId,
'clientSecret' => $clientSecret,
]
);
//Pass the OAuth provider instance to PHPMailer
$mail->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'refreshToken' => $refreshToken,
'username' => $email,
]
)
);
//Set who the message is to be sent from
//For gmail, this generally needs to be the same as the user you logged in as
$mail->setFrom($email, 'me');
//Set who the message is to be sent to
$mail->addAddress($address, 'John Doe');
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->msgHTML(file_get_contents('PHPMailer/PHPMailer-master/examples/contentsutf8.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('PHPMailer/PHPMailer-master/examples/images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: '. $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}
}
是的,我有客户端 ID 和密码,这是完整的错误:
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP a9sm1354888otc.75 - gsmtp
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2600:1700:760:3400:794a:d1c1:d15d:e94d]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2600:1700:760:3400:794a:d1c1:d15d:e94d]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection a9sm1354888otc.75 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/TroubleshootingEmail sent!
解决方案
推荐阅读
- c++ - Googletest 在 bazel 测试中永远不会失败(应该在哪里失败),但可以与 cmake 和 clion 一起使用
- javascript - 如何在nodejs中发送响应错误代码
- java - 向列表中添加第二项似乎会覆盖我添加的第一个元素
- javascript - 电子
: Uncaught SyntaxError: Unexpected token - php - 调用未定义的方法 Silverstripe Gridfield
- java - 你如何从一个方法返回一个值并在另一个方法中调用它?
- c# - 文件上传到 dotnetcore api 不适用于红隼套接字异常
- python-3.x - 将 int 和 str 与 print 一起使用
- python - 为什么元组解包可以在使用两行时提供正确的结果?
- python - 如何对一组相似的产品名称进行分组?