我正在尝试使用OAuth2通过Gmail发送电子邮件。
我已完成此处描述的所有步骤:https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
唯一的区别是默认情况下 get_oauth_token.php 请求离线访问,并且在指南中他们没有说明更改此内容,但是在屏幕截图上请求“查看和管理您的邮件“而不是”离线访问“。但是好的,我正在授权一个将来发送电子邮件但没有提示许可的应用程序,所以我认为“离线”正是我需要的。
但问题是,当我使用所有获得的凭据发送电子邮件时,它会失败。这是我的PHP代码:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;
date_default_timezone_set('Etc/UTC');
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
$email = 'hello@otdelkalux.ru';
$clientId = '407883698840-47e4rhg839eh8fm3qsfuva8b46acnl36.apps.googleusercontent.com';
$clientSecret = 'xr1Q08dGYjpSFLrwWB7laBb3';
$refreshToken = '1/JRTle_utTpfG4Mz_Z1fQiCf-qrNXzkS2PDg4iaqSHa4';
//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
])
);
$mail->addAddress('mike.shestakov@gmail.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
以下是服务器响应的内容:
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP q13sm3464715lfi.21 - gsmtp
CLIENT -> SERVER: EHLO otdelkalux.ru
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [87.242.64.154]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 otdelkalux.ru
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [87.242.64.154]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: AUTH XOAUTH2 dXNlcj1oZWxsb0BvdGRlbGthbHV4LnJ1AWF1dGg9QmVhcmVyIHlhMjkuLnpnSThvZ3pGMzRsQ01Fc2pUWjJTblRWb2FOR3lncXoxM0twMUNzaTNCLVJRd21IaWVMRzhFUVVQdExXZVY2cW1yekUBAQ==
SERVER -> CLIENT: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
SMTP ERROR: AUTH command failed: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/answer/14257 q13sm3464715lfi.21 - gsmtp
SMTP ERROR: QUIT command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/answer/14257 q13sm3464715lfi.21 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
它说用户名和密码不被接受;但是,我没有使用任何密码。
我错过了什么?谢谢!
更新:我已尝试过6.0分支,但失败的结果完全相同。我已经用新的代码替换了代码示例和日志(上图)。真实的凭据在代码中,所以如果你@synchro可以重现这个问题并帮助解决它,那将非常感激!