I'm trying to use OAuth2 to send emails via Gmail.
I've done all the steps described here: https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
The only difference is that by default get_oauth_token.php requests offline access, and in the guide they don't say anything about changing this, but on screenshot it requests "View and manage your mail" rather then "Offline access". But ok, I'm authorising an app that will send emails in future without a permission prompt, so I think "offline" is exactly what I need.
But the problem is, when I use all gained credentials to send an email, it fails. Here is my PHP code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;
date_default_timezone_set('Etc/UTC');
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';
$email = 'hello#otdelkalux.ru';
$clientId = '407883698840-47e4rhg839eh8fm3qsfuva8b46acnl36.apps.googleusercontent.com';
$clientSecret = 'xr1Q08dGYjpSFLrwWB7laBb3';
$refreshToken = '1/JRTle_utTpfG4Mz_Z1fQiCf-qrNXzkS2PDg4iaqSHa4';
//Create a new OAuth2 provider instance
$provider = new Google([
'clientId' => $clientId,
'clientSecret' => $clientSecret
]);
//Pass the OAuth provider instance to PHPMailer
$mail->setOAuth(
new OAuth([
'provider' => $provider,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'refreshToken' => $refreshToken,
'userName' => $email
])
);
$mail->addAddress('mike.shestakov#gmail.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$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';
}
Here is what server responds:
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP q13sm3464715lfi.21 - gsmtp
CLIENT -> SERVER: EHLO otdelkalux.ru
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [87.242.64.154]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO otdelkalux.ru
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [87.242.64.154]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: AUTH XOAUTH2 dXNlcj1oZWxsb0BvdGRlbGthbHV4LnJ1AWF1dGg9QmVhcmVyIHlhMjkuLnpnSThvZ3pGMzRsQ01Fc2pUWjJTblRWb2FOR3lncXoxM0twMUNzaTNCLVJRd21IaWVMRzhFUVVQdExXZVY2cW1yekUBAQ==
SERVER -> CLIENT: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
SMTP ERROR: AUTH command failed: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/answer/14257 q13sm3464715lfi.21 - gsmtp
SMTP ERROR: QUIT command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/answer/14257 q13sm3464715lfi.21 - 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
It says username and password not accepted; however, I'm not using any password.
What am I missing? Thanks!
UPDATE: I've tried 6.0 branch, but it fails with exactly the same result. I've replaced code sample and log with a new one (above). Real credentials are in the code, so if you #synchro can reproduce this problem and help solving it, that would be much appreciated!
Related
I'm getting this SMTP error when using the newest PHPMailer release:
SMTP Error: Could not authenticate.
I've tried using SSL instead of TLS with port number 465. OpenSSL is already loaded in the config file, and I've looked through the troubleshooting guide none of this has made a difference, what exactly is wrong with my code? One thing I'm not sure of is including the OAuth2 provider class with this :
use League\OAuth2\Client\Provider\Google;
There's no file path for that, should I change this? Also, when I change encryption to $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; and now I get this : SMTP ERROR: Failed to connect to server: (0).
Here's the code:
class gmail_xoauth
{
public static function sendMail($subject,$body,$address)
{
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_CLIENT;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
//Fill in authentication details here
//Either the gmail account owner, or the user that gave consent
$email = 'myemail#gmail.com';
$clientId = 'clientid';
$clientSecret = 'clientSecret';
//Obtained by configuring and running get_oauth_token.php
//after setting up an app in Google Developer Console.
$refreshToken = 'refreshToken';
//Create a new OAuth2 provider instance
$provider = new Google(
[
'clientId' => $clientId,
'clientSecret' => $clientSecret,
]
);
//Pass the OAuth provider instance to PHPMailer
$mail->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'refreshToken' => $refreshToken,
'username' => $email,
]
)
);
//Set who the message is to be sent from
//For gmail, this generally needs to be the same as the user you logged in as
$mail->setFrom($email, 'me');
//Set who the message is to be sent to
$mail->addAddress($address, 'John Doe');
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->msgHTML(file_get_contents('PHPMailer/PHPMailer-master/examples/contentsutf8.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('PHPMailer/PHPMailer-master/examples/images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: '. $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}
}
This is the full error
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP w67sm7347917yww.16 - gsmtp
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [108.203.6.59]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [108.203.6.59]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection w67sm7347917yww.16 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/TroubleshootingEmail sent!
I have installed PHPMailer using Composer and added all the Composer dependency to use Google XOAuth2 authentication.
Then I have followed some tutorials to get the refresh token with the Gmail API.
Everything should be working just fine. I did all the paper work right. Right?!
Despite my best intention and all the docs, I'm not able to establish a SMTP connection to smtp.gmail.com
I get this error:
2017-10-20 18:01:45 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP c17sm2715728wrg.26 - gsmtp
2017-10-20 18:01:45 CLIENT -> SERVER: EHLO localhost
2017-10-20 18:01:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [151.61.40.58]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-10-20 18:01:45 CLIENT -> SERVER: STARTTLS
2017-10-20 18:01:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-10-20 18:01:45 CLIENT -> SERVER: QUIT
2017-10-20 18:01:45
2017-10-20 18:01:45
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
My code is straight from the PHPMailer Gmail XOAUTH example.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;
date_default_timezone_set('Europe/Rome');
require 'lib/php/vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
$email = 'seckrit-stuff';
$clientId = 'seckrit-stuff';
$clientSecret = 'seckrit-stuff';
$refreshToken = 'seckrit-stuff';
$provider = new Google(
[
'clientId' => $clientId,
'clientSecret' => $clientSecret,
]
);
$mail->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'refreshToken' => $refreshToken,
'userName' => $email,
]
)
);
$mail->setFrom($email, 'Matteo Bini');
$mail->addAddress('seckrit-stuff', 'Matteo Bini');
$mail->Subject = 'PHPMailer GMail XOAUTH2 SMTP test';
$mail->CharSet = 'utf-8';
$mail->msgHTML('<strong>HTML</strong> message!');
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Could you please help me finding a way to use PHPMailer with Gmail in localhost (xampp)?
This is nothing to do with OAuth. You’ve got a much earlier issue at the TLS level. Either you’re missing the openssl extension, it’s misconfigured, or your CA certificates are out of date.
This issue is covered in the troubleshooting guide the error links to - check using openssl.
I'm getting an EOF error. I'm using the basic template with a few subtractions (attachments, cc, etc) and one addition (AuthType='PLAIN').
<?php
require __DIR__ . '/vendor/autoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.zoho.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->AuthType = 'PLAIN';
$mail->Username = '*******'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('*************', '*****');
$mail->addAddress('*********', '******'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'From phpmailer-test.php';
$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've tried STMP via 465 both with this script and with telnet and gotten the same error.
-I've tested this with tls and 587 with this script (but not telent) and gotten this error.
-I'm using composer, per the Troubleshooting page's instructions.
-Script is at phillyguitarlessons.com, mail account is at *#kalegood.com (hosted by zoho.com). I don't think this would cause an error, but am not sure.
-I've tried looking through the docs (via a page search for "EOF") and found nothing.
-From my VPS, my ports should be wide-open. To zoho.com
-Opensssl is activated (confirmed via info.php)
Output (switched to debug level 2):
2016-10-03 13:47:40 SERVER -> CLIENT: 220 mx.zohomail.com SMTP Server ready October 3, 2016 6:47:40 AM PDT
2016-10-03 13:47:40 CLIENT -> SERVER: EHLO phillyguitarlessons.com
2016-10-03 13:47:40 SERVER -> CLIENT: 250-mx.zohomail.com Hello phillyguitarlessons.com (162.243.32.109 (162.243.32.109)) 250-STARTTLS 250 SIZE 53477376
2016-10-03 13:47:40 CLIENT -> SERVER: STARTTLS
2016-10-03 13:47:40 SERVER -> CLIENT: 220 Ready to start TLS.
2016-10-03 13:47:41 CLIENT -> SERVER: EHLO phillyguitarlessons.com
2016-10-03 13:47:41 SERVER -> CLIENT: 250-mx.zohomail.com Hello phillyguitarlessons.com (162.243.32.109 (162.243.32.109)) 250-AUTH LOGIN PLAIN 250 SIZE 53477376
2016-10-03 13:47:41 CLIENT -> SERVER: AUTH PLAIN
2016-10-03 13:49:46 SERVER -> CLIENT: 334
2016-10-03 13:49:46 SMTP NOTICE: EOF caught while checking if connected
2016-10-03 13:49:46 SMTP Error: Could not authenticate.
2016-10-03 13:49:46 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
I want to know where the problem is because I get authentication error.
I already have a few gmail accounts,
I created a new one for my website,
I added a new email in "Account and import" to my old gmail account.
I copied my new email username and password to phpmailer code
include_once('phpmailer/class.phpmailer.php');
include_once('phpmailer/class.smtp.php');
//6nb5Drv;
function sendmail(){
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->Port = 587; // Set the SMTP port i tried and 457
$mail->Username = 'newmail#gmail.com'; // SMTP username
$mail->Password = 'newmailpass'; // SMTP password
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->From = 'from#yahoo.com';
$mail->FromName = 'From';
$mail->AddAddress('to#gmail.com', 'To'); // Add a recipient
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <strong>in bold!</strong>';
$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;
exit;
}
echo 'Message has been sent';
}
sendmail();
But I get an authentication error.
What is wrong?
Probably something with the credentials, how do I configure gmail smtp?
Debug report:
2015-12-04 17:56:15 CLIENT -> SERVER: EHLO www.site.co
2015-12-04 17:56:15 CLIENT -> SERVER: STARTTLS 2015-12-04
17:56:15 CLIENT -> SERVER: EHLO www.site.co 2015-12-04
17:56:15 CLIENT -> SERVER: AUTH LOGIN 2015-12-04 17:56:15 CLIENT ->
SERVER: UHJlZGljdG9sb2d5 2015-12-04 17:56:15 CLIENT -> SERVER:
U2dHZlB0VHZUbTZ1SW9ZMi1qTlNCQQ== 2015-12-04 17:56:17 SMTP ERROR:
Password command failed: 435 4.7.8 Error: authentication failed:
UGFzc3dvcmQ6 2015-12-04 17:56:17 SMTP Error: Could not authenticate.
2015-12-04 17:56:17 CLIENT -> SERVER: QUIT 2015-12-04 17:56:17 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
According to this post:
PHPMailer - SMTP ERROR: Password command failed when send mail from my server, in some case, you have to specify google that this is not a suspicious activity and activate some less secure option in your account.
Assuming this is the right password...
when i try to send mail by phpmailer,there is some error.
Like this:
SMTP -> FROM SERVER: 250-m1.mydomain.com
250-PIPELINING
250-SIZE 15728640
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
CLIENT -> SMTP: STARTTLS
SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS
CLIENT -> SMTP: EHLO localhost.localdomain
250-PIPELINING
250-SIZE 15728640
250-VRFY
250-ETRN
250-AUTH PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
SMTP -> ERROR: AUTH not accepted from server: 535 5.7.8 Error: authentication failed: Invalid authentication mechanism
CLIENT -> SMTP: RSET
SMTP -> FROM SERVER:250 2.0.0 Ok
CLIENT -> SMTP: MAIL FROM:
SMTP -> FROM SERVER:250 2.1.0 Ok
CLIENT -> SMTP: RCPT TO:
SMTP -> FROM SERVER:554 5.7.1 : Relay access denied
SMTP -> ERROR: RCPT not accepted from server: 554 5.7.1 : Relay access denied
CLIENT -> SMTP: quit
SMTP -> FROM SERVER:221 2.0.0 Bye
And my php code like this:
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Host = "m1.mydomain.com";
$mail->Port = 25;
$mail->Username = "service#mydomain.com";
$mail->Password = "password";
$mail->SetFrom('service#mydomain.com', 'service');
$mail->Subject = "Fine Design - Avise me";
$mail->Body = "This is very interest email for you";
$mail->AddAddress("to#domain.com","name");
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo "We're sorry, however, an error has occurred. You may manually e-mail us .";
return false;
} else {
echo "Thanks! Your message was successfully sent.";
return true;
}
?>
but i could send mail by foxmail and other client,
Did anybody could give me some suggestion?
You are using tls on port 25.
That normally should not work as port 25 is normally used for smtp without ssl/tls.
Try to test it with port 25, and with port 587.
Add:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
I used it and works fine without SSL.