Issues with authorization on Microsoft Office using OAuth 2.0 on PHPMailer

Viewed 105

I'm having some issues with OAuth 2.0 authorization during SMTP mail sending via PHPMail.

Basically, I've tried all of the below listed scopes:

  • email
  • openid
  • profile
  • https://graph.microsoft.com/Mail.Send
  • https://graph.microsoft.com/SMTP.Send
  • https://graph.microsoft.com/User.Read
  • User.Read
  • SMTP.Send
  • Mail.Send
  • offline_access

With the following Auth URIs:

  • https://login.microsoftonline.com/common/oauth2/v2.0/token with https://login.microsoftonline.com/common/oauth2/v2.0/authorize
  • https://login.microsoftonline.com/common/oauth2/token with https://login.microsoftonline.com/common/oauth2/authorize

With the following hostnames:

  • smtp.office365.com
  • smtp.outlook.com

And nothing works. :(

Here is the verbose output of PHPMailer:

2022-09-06 14:55:41 Connection: opening to smtp.office365.com:587, timeout=300, options=array()
2022-09-06 14:55:41 Connection: opened
2022-09-06 14:55:41 SMTP INBOUND: "220 BLAPR03CA0009.outlook.office365.com Microsoft ESMTP MAIL Service ready at Tue, 6 Sep 2022 14:55:40 +0000"
2022-09-06 14:55:41 SERVER -> CLIENT: 220 BLAPR03CA0009.outlook.office365.com Microsoft ESMTP MAIL Service ready at Tue, 6 Sep 2022 14:55:40 +0000
2022-09-06 14:55:41 CLIENT -> SERVER: EHLO <REMOVED_FOR_SECURITY_REASONS>.org
2022-09-06 14:55:41 SMTP INBOUND: "250-BLAPR03CA0009.outlook.office365.com Hello [150.136.30.43]"
2022-09-06 14:55:41 SMTP INBOUND: "250-SIZE 157286400"
2022-09-06 14:55:41 SMTP INBOUND: "250-PIPELINING"
2022-09-06 14:55:41 SMTP INBOUND: "250-DSN"
2022-09-06 14:55:41 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2022-09-06 14:55:41 SMTP INBOUND: "250-STARTTLS"
2022-09-06 14:55:41 SMTP INBOUND: "250-8BITMIME"
2022-09-06 14:55:41 SMTP INBOUND: "250-BINARYMIME"
2022-09-06 14:55:41 SMTP INBOUND: "250-CHUNKING"
2022-09-06 14:55:41 SMTP INBOUND: "250 SMTPUTF8"
2022-09-06 14:55:41 SERVER -> CLIENT: 250-BLAPR03CA0009.outlook.office365.com Hello [150.136.30.43]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
2022-09-06 14:55:41 CLIENT -> SERVER: STARTTLS
2022-09-06 14:55:41 SMTP INBOUND: "220 2.0.0 SMTP server ready"
2022-09-06 14:55:41 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2022-09-06 14:55:41 CLIENT -> SERVER: EHLO <REMOVED_FOR_SECURITY_REASONS>.org
2022-09-06 14:55:41 SMTP INBOUND: "250-BLAPR03CA0009.outlook.office365.com Hello [150.136.30.43]"
2022-09-06 14:55:41 SMTP INBOUND: "250-SIZE 157286400"
2022-09-06 14:55:41 SMTP INBOUND: "250-PIPELINING"
2022-09-06 14:55:41 SMTP INBOUND: "250-DSN"
2022-09-06 14:55:41 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2022-09-06 14:55:41 SMTP INBOUND: "250-AUTH LOGIN XOAUTH2"
2022-09-06 14:55:41 SMTP INBOUND: "250-8BITMIME"
2022-09-06 14:55:41 SMTP INBOUND: "250-BINARYMIME"
2022-09-06 14:55:41 SMTP INBOUND: "250-CHUNKING"
2022-09-06 14:55:41 SMTP INBOUND: "250 SMTPUTF8"
2022-09-06 14:55:41 SERVER -> CLIENT: 250-BLAPR03CA0009.outlook.office365.com Hello [150.136.30.43]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN XOAUTH2250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
2022-09-06 14:55:41 Auth method requested: XOAUTH2
2022-09-06 14:55:41 Auth methods available on the server: LOGIN,XOAUTH2
2022-09-06 14:55:41 CLIENT -> SERVER: AUTH XOAUTH2 <TOKEN REMOVED FOR SECURITY REASONS>
2022-09-06 14:55:47 SMTP INBOUND: "535 5.7.3 Authentication unsuccessful [BLAPR03CA0009.namprd03.prod.outlook.com]"
2022-09-06 14:55:47 SERVER -> CLIENT: 535 5.7.3 Authentication unsuccessful [BLAPR03CA0009.namprd03.prod.outlook.com]
2022-09-06 14:55:47 SMTP ERROR: AUTH command failed: 535 5.7.3 Authentication unsuccessful [BLAPR03CA0009.namprd03.prod.outlook.com]
SMTP Error: Could not authenticate.
2022-09-06 14:55:47 CLIENT -> SERVER: QUIT
2022-09-06 14:55:47 SMTP INBOUND: "221 2.0.0 Service closing transmission channel"
2022-09-06 14:55:47 SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel
2022-09-06 14:55:47 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

And here is the var_dump of the Access Token variable handled by Steven Maguire's Provider:

object(League\OAuth2\Client\Token\AccessToken)#33 (5) {

  ["accessToken":protected]=>

  string(2012) "<TOKEN REMOVED FOR SECURITY REASONS>"

  ["expires":protected]=>

  int(1662481291)

  ["refreshToken":protected]=>

  string(848) "<TOKEN REMOVED FOR SECURITY REASONS>"

  ["resourceOwnerId":protected]=>

  NULL

  ["values":protected]=>

  array(3) {

    ["token_type"]=>

    string(6) "Bearer"

    ["scope"]=>

    string(134) "email openid profile https://graph.microsoft.com/Mail.Send https://graph.microsoft.com/SMTP.Send https://graph.microsoft.com/User.Read"

    ["ext_expires_in"]=>

    int(5150)

  }

}

The funny thing is that PHPMailer's internal code suggests using wl.offline_access and wl.imap, as well as most of the how-tos suggesting wl.basic and wl.emails. Those kinds of scopes are deprecated.

Could you please help me with it?

0 Answers
Related