title: PHPMailer XOAUTH2 with gMail - SMTP Could not authenticate
body: <p>I'm stuck with the following bizarre situation.</p>
  
  <p>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".</p>
  
  <p>Using phpmailer 5.2.21, with oauth2 league 1.4.1 (all loaded via composer)
  PHP Version: 7.0.1 (openssl is loaded)</p>
  
  <p>The configuration is the following</p>
  
  <pre><code>$mailer = new PHPMailer();
  $mailer-&gt;isSMTP();
  $mailer-&gt;Host = 'smtp.gmail.com';
  $mailer-&gt;SMTPAuth = true;
  $mailer-&gt;AuthType = 'XOAUTH2';
  $mailer-&gt;oauthUserEmail = $userEmail;
  $mailer-&gt;oauthClientId = $clientId;
  $mailer-&gt;oauthClientSecret = $clientSecret;
  $mailer-&gt;oauthRefreshToken = $token;
  $mailer-&gt;SMTPSecure = 'tls';
  $mailer-&gt;Port = 587;
  
  $mailer-&gt;setFrom($email);
  $mailer-&gt;addAddress($recipient);
  
  $mailer-&gt;Subject = $subject;
  $mailer-&gt;Body = $message;
  
  if (! $mailer-&gt;send())
      throw new RuntimeException('Mail submission failed! ' . $mailer-&gt;ErrorInfo);
  </code></pre>
  
  <p>And the debug output is the following:</p>
  
  <pre><code>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 -&gt; CLIENT: 220 smtp.gmail.com ESMTP e7sm325303lfb.10 - gsmtp
  2017-01-05 08:14:47 CLIENT -&gt; 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 -&gt; SERVER: STARTTLS
  2017-01-05 08:14:47 SERVER -&gt; CLIENT: 220 2.0.0 Ready to start TLS
  2017-01-05 08:14:47 CLIENT -&gt; 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 -&gt; SERVER: QUIT
  </code></pre>
  
  <p>The google app is authorized to use gmail (<a href="https://mail.google.com" rel="nofollow noreferrer">https://mail.google.com</a>) 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.</p>
  
  <p>I'm probably missing something obvious here, but any ideas?
  Thanks in advance</p>