【问题标题】:PHPMailer XOAUTH2 with gMail - SMTP Could not authenticate带有 gMail 的 PHPMailer XOAUTH2 - SMTP 无法验证
【发布时间】:2020-05-11 17:22:42
【问题描述】:

我陷入了以下奇怪的情况。

尝试将 phpMailer 与 gMail 和 xoauth2 一起使用,当连接成功时,在发送“request AUTH”命令后,我收到“SMTP 错误:无法验证”的响应。

使用 phpmailer 5.2.21 和 oauth2 联盟 1.4.1(全部通过 composer 加载) PHP版本:7.0.1(已加载openssl)

配置如下

$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);

调试输出如下:

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

google app被授权使用gmail(https://mail.google.com),整个app设置和获取刷新token的过程都是成功的。此外,我已经阅读了整个 phpmailer 故障排除指南,但没有运气。

我可能在这里遗漏了一些明显的东西,但是有什么想法吗? 提前致谢

【问题讨论】:

    标签: phpmailer


    【解决方案1】:

    是的,一些简单的事情 - 错误的类:

    $mail = new PHPMailerOAuth;
    

    查看 PHPMailer 提供的 gmail_xoauth.phps 示例。请注意,OAuth 支持将在 PHPMailer 6.0 中得到显着改进。

    【讨论】:

    • 谢谢!节省了一天
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 2016-01-16
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    相关资源
    最近更新 更多