I'm stuck with the following bizarre situation.
Trying to use phpMailer with gMail and xoauth2, and while the connection succeeds, just after sending the "request AUTH" command, I get back a response of "SMTP Error: Could not authenticate".
Using phpmailer 5.2.21, with oauth2 league 1.4.1 (all loaded via composer) PHP Version: 7.0.1 (openssl is loaded)
The configuration is the following
$mailer = new PHPMailer();
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->AuthType = 'XOAUTH2';
$mailer->oauthUserEmail = $userEmail;
$mailer->oauthClientId = $clientId;
$mailer->oauthClientSecret = $clientSecret;
$mailer->oauthRefreshToken = $token;
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;
$mailer->setFrom($email);
$mailer->addAddress($recipient);
$mailer->Subject = $subject;
$mailer->Body = $message;
if (! $mailer->send())
throw new RuntimeException('Mail submission failed! ' . $mailer->ErrorInfo);
And the debug output is the following:
2017-01-05 08:14:47 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()
2017-01-05 08:14:47 Connection: opened
2017-01-05 08:14:47 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP e7sm325303lfb.10 - gsmtp
2017-01-05 08:14:47 CLIENT -> SERVER: EHLO domain.tld
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
2017-01-05 08:14:47 CLIENT -> SERVER: STARTTLS
2017-01-05 08:14:47 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2017-01-05 08:14:47 CLIENT -> SERVER: EHLO domain.tld
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
2017-01-05 08:14:47 Auth method requested: XOAUTH2
2017-01-05 08:14:47 Auth methods available on the server: LOGIN,PLAIN,XOAUTH2,PLAIN-CLIENTTOKEN,OAUTHBEARER,XOAUTH
2017-01-05 08:14:47 SMTP Error: Could not authenticate.
2017-01-05 08:14:47 CLIENT -> SERVER: QUIT
The google app is authorized to use gmail (https://mail.google.com) and the whole process of setting up the app and getting the refresh token is successful. Moreover, I've gone over the whole phpmailer troubleshooting guide with no luck.
I'm probably missing something obvious here, but any ideas? Thanks in advance
Yep, something simple - wrong class:
$mail = new PHPMailerOAuth;
Look at the gmail_xoauth.phps
example provided with PHPMailer. Note that OAuth support will be substantially improved in PHPMailer 6.0.