Can't Send Email PHPMailer 6.0 with Oauth Google

风流意气都作罢 提交于 2019-12-11 04:47:12

问题


0 with Google Oauth and i get this error

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

My script

<?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';
}

i don't have any idea, i've done search google but i dont find any solution. Before i ask in Can't Send PHPMailer with Oauth Google

Any solution for my case?


回答1:


For XOAUTH2 AuthType, if you are using the 5.2 version, you are instantiating the wrong class. The 6.0 also works different.

You should use:

$mail = new PHPMailerOAuth;

That is the reason of the following error:

2017-08-04 06:38:43 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [180...... XOAUTH2 ...... SMTP Error: Could not authenticate.

Cheers!




回答2:


A few things:

First, OAuth is really hard to debug anyway. I really recommend that you take a look at the 6.0 branch, in which the OAuth support is significantly improved. It comes with much better examples, and it would be very useful if you could help test it. This second step is key point and might solve your problem. There is slight difference in initiation( Use PHPMailerOAuth class), so please read the docs carefully.

Second, Go through this documentation, you might have followed it, but see if you are missing any step.

Third, Are you sure you got a refresh token and not a normal token?

Try these things and let me know, if it worked for you or not?




回答3:


hi You can have to let them.
https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha



来源:https://stackoverflow.com/questions/45499897/cant-send-email-phpmailer-6-0-with-oauth-google

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!