You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHPMailer通过OAuth 2.0连接Microsoft Office授权失败求助

PHPMailer SMTP OAuth 2.0授权失败问题求助

使用PHPMailer通过SMTP发送邮件时遭遇OAuth 2.0授权失败,已尝试以下所有配置组合,但均未成功。

已尝试的权限范围(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

已尝试的授权URI组合

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

已尝试的SMTP主机名

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

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

AccessToken变量信息(Steven Maguire 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)

  }

}

补充说明

PHPMailer内部代码建议使用wl.offline_access和wl.imap,多数指南也推荐wl.basic和wl.emails,但这些权限范围已被弃用。

恳请协助解决此授权失败问题。

内容的提问来源于stack exchange,提问作者Rudy Grinberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 23:05:28