I'm trying to use OAuth2 to send emails via Gmail.
I've done all the steps described here: https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
The only difference is that by default get_oauth_token.php requests offline access, and in the guide they don't say anything about changing this, but on screenshot it requests "View and manage your mail" rather then "Offline access". But ok, I'm authorising an app that will send emails in future without a permission prompt, so I think "offline" is exactly what I need.
But the problem is, when I use all gained credentials to send an email, it fails. Here is my PHP code:
<?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';
}
Here is what server responds:
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
It says username and password not accepted; however, I'm not using any password.
What am I missing? Thanks!
UPDATE: I've tried 6.0 branch, but it fails with exactly the same result. I've replaced code sample and log with a new one (above). Real credentials are in the code, so if you @synchro can reproduce this problem and help solving it, that would be much appreciated!