【问题标题】:Can't Send Email PHPMailer 6.0 with Oauth Google无法使用 Oauth Google 发送电子邮件 PHPMailer 6.0
【发布时间】:2018-01-11 23:38:03
【问题描述】:

使用 Google Oauth 为 0,我收到此错误

2017-08-04 06:38:42 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP v19sm895098pgc.93 - gsmtp
2017-08-04 06:38:42 CLIENT -> SERVER: EHLO localhost
2017-08-04 06:38:43 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [180.243.143.147]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-08-04 06:38:43 CLIENT ->; SERVER: STARTTLS
2017-08-04 06:38:43 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2017-08-04 06:38:43 CLIENT -> SERVER: EHLO localhost
2017-08-04 06:38:43 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [180.243.143.147]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
SMTP Error: Could not authenticate.
2017-08-04 06:38:43 CLIENT -> SERVER: QUIT
2017-08-04 06:38:43 SERVER -> CLIENT: 221 2.0.0 closing connection v19sm895098pgc.93 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我的脚本

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require './vendor/autoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';

$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';

$mail->oauthUserEmail = "info@modelunitednation.org";
$mail->oauthClientId = "932857915584-eco8v9aejdb2n3mkltgvftf8e5h1eiko.apps.googleusercontent.com";
$mail->oauthClientSecret = "7SqFo7aPYvZ05cHlh5p3kMUD";
$mail->oauthRefreshToken = "1/4KJJ5XMyXqELMAhRXGYjz_I4SfoWfFR9N9RRA2VA2I0";

$mail->addAddress('jakabanasuy2@gmail.com');     // Add a recipient
$mail->isHTML(true);                               // Set email format to HTML
$mail->setFrom('from@example.com', 'Mailer');
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

我没有任何想法,我已经完成了谷歌搜索,但我没有找到任何解决方案。在我问之前Can't Send PHPMailer with Oauth Google

我的情况有什么解决方案吗?

【问题讨论】:

标签: php oauth-2.0 phpmailer google-oauth


【解决方案1】:

对于XOAUTH2 AuthType,如果您使用的是5.2 version,则您正在实例化错误的类The 6.0 也有不同的工作方式。

你应该使用:

$mail = new PHPMailerOAuth;

这就是以下错误的原因:

2017-08-04 06:38:43 服务器 -> 客户端:250-smtp.gmail.com 在您的 服务,[180...... XOAUTH2 ...... SMTP 错误:无法验证。

干杯!

【讨论】:

    【解决方案2】:

    一些事情:

    首先,OAuth 无论如何都很难调试。我真的建议您看一下6.0 branch,其中 OAuth 支持得到了显着改进。它带有更好的示例,如果您可以帮助测试它,它将非常有用。第二步是关键点,可能会解决您的问题。初始化略有不同(使用PHPMailerOAuth类),请仔细阅读文档。

    其次,查看this 文档,您可能已经遵循它,但请查看是否缺少任何步骤。

    第三,你确定你得到的是刷新令牌而不是普通令牌?

    试试这些方法,然后告诉我,它对你有用吗?

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 2020-11-25
      • 2019-01-19
      • 2023-03-20
      • 2017-07-19
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      相关资源
      最近更新 更多