PHPMailer XOAUTH2 with gMail - SMTP Could not authenticate - phpmailer

I'm stuck with the following bizarre situation.
Trying to use phpMailer with gMail and xoauth2, and while the connection succeeds, just after sending the "request AUTH" command, I get back a response of "SMTP Error: Could not authenticate".
Using phpmailer 5.2.21, with oauth2 league 1.4.1 (all loaded via composer)
PHP Version: 7.0.1 (openssl is loaded)
The configuration is the following
$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);
And the debug output is the following:
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
The google app is authorized to use gmail (https://mail.google.com) and the whole process of setting up the app and getting the refresh token is successful. Moreover, I've gone over the whole phpmailer troubleshooting guide with no luck.
I'm probably missing something obvious here, but any ideas?
Thanks in advance

Yep, something simple - wrong class:
$mail = new PHPMailerOAuth;
Look at the gmail_xoauth.phps example provided with PHPMailer. Note that OAuth support will be substantially improved in PHPMailer 6.0.

Related

"Connection unexpectedly closed" error while sending email using SES from AWS

So I'm trying to send an email to my self using SMTP and AWS. The email I'm using on my configuration is verified since I'm still using sandbox mode in SES. While running the script I keep getting the error Connection unexpectedly closed even dough I tried to connect with OpenSSL and it connected but it showed a Didn't find STARTTLS in server response, trying anyway... error after connecting.
Here is my code:
MAIL = {}
MAIL['USERNAME'] = 'AKIAXARHTFGFKCDAO7PD'
MAIL['PASSWORD'] = 'BE0tXPq8wteiKZYtgD2TgtfFTGhgFGOhUp3F0lG0uqn'
MAIL['HOST'] = 'email-smtp.eu-central-1.amazonaws.com'
MAIL['PORT'] = 465
# Set user code
code = random.randrange(000000, 999999)
# Send email to user
print(code)
print(current_user.email)
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Ruby - Verification code'
msg['From'] = 'amng835#gmail.com'
msg['To'] = current_user.email
msg.attach(MIMEText(f'Your verification code is: {code}', 'plain'))
try:
server = smtplib.SMTP(MAIL['HOST'], MAIL['PORT'])
server.ehlo()
server.starttls()
server.ehlo()
server.login(MAIL('MAIL_USERNAME'), MAIL('MAIL_PASSWORD'))
server.sendmail('amng835#gmail.com', current_user.email, msg.as_string())
server.close()
except Exception as error:
print('Failed to send message to user')
print(error)
OpenSSL output:
The command:
openssl s_client -connect email-smtp.eu-central-1.amazonaws.com:465 -starttls smtp
The output:
CONNECTED(00000005)
Didn't find STARTTLS in server response, trying anyway...
write:errno=0
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 372 bytes
Verification: OK
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
---
My documentation source:
https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-using-smtp.html
There seems to have some issue with port 465.Change the code to below and it will work fine.
MAIL['PORT'] = 587

PHPMailer cannot send via smtp.gmail.com

I'm trying to send mail with PHPMailer plugin via smtp.gmail.com. The sending works fine on my local computer but when I use my remote server with following code it delays the sending, normally around 30 seconds.
function sendAuthEmail($subject, $mail_body, $mailTo, $mailToTitle, $mailFrom, $fromTitle)
{
require 'PhpMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->Username = 'no-reply#domail.com';
$mail->Password = 'password';
$mail->AddReplyTo($mailFrom, $fromTitle);
$mail->SetFrom($mail->Username, 'Domain.com');
$mail->AddAddress($mailTo, $mailToTitle);
$mail->CharSet = 'UTF-8';
$mail->Subject = $subject;
$mail->MsgHTML($mail_body);
if($mail->Send()) {
return;
} else {
die('There was an error: '.$mail->ErrorInfo.'</i>');
}
}
But when I try to use gethostbyname('smtp.gmail.com') it fails with following debug information:
2016-11-23 12:36:11 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP a47sm16079918qtc.17 - gsmtp
2016-11-23 12:36:11 CLIENT -> SERVER: EHLO www.sellercloud.com
2016-11-23 12:36:11 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [199.189.62.92] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
2016-11-23 12:36:11 CLIENT -> SERVER: STARTTLS
2016-11-23 12:36:11 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2016-11-23 12:36:11 SMTP Error: Could not connect to SMTP host.
2016-11-23 12:36:11 CLIENT -> SERVER: QUIT
2016-11-23 12:36:11 SERVER -> CLIENT:
2016-11-23 12:36:11 SMTP ERROR: QUIT command failed:
2016-11-23 12:36:11 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
The problem with the server turned out to be a DNS resolving issue. PHP mailer couldn't resolve smtp.gmail.com correctly, and this was the main reason why none of the known fixes worked.
If you're having similar issue, check your DNS servers.

phpmailer suddenly got very slow

I use phpmailer for sending emails and while it worked flawlesly until now, I noticed that sending emails got very very slow, around 2-3 minutes. The mails are sent, but the waiting time is killing me.
My code is:
protected function SendPHPMailerMail($from = null, $name = null, $subject = null, $message = null)
{
require_once ('phpmailer/PHPMailerAutoload.php');
if($from && $name && $subject && $message)
{
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // sets GMAIL as the SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = 'tls'; // sets the prefix to the servier
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = 'x#gmail.com'; // GMAIL username
$mail->Password = 'pass'; // GMAIL password
$mail->AddReplyTo($from, $name);
$mail->AddAddress('x#gmail.com', 'Me');
$mail->SetFrom('x#gmail.com', 'Me');
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send();
}
}
I tried debugging, there are no errors. I read in a different question thread that using gethostbyname('smtp.gmail.com'); worked for some, but not for me. I haven't changed anything on the server and this popped out of nowhere. Not sure what to do.
Thanks!
edit: used SMTPdebug=2 and this is what I get
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: EHLO www.host.com
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [107.182.234.254]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 www.host.com
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [107.182.234.254]250- SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 334 VXNlcm5hbWU6
CLIENT -> SERVER: Y29udGFjdEBmaWxlY2x1c3Rlci5jb20=
SERVER -> CLIENT: 334 UGFzc3dvcmQ6
CLIENT -> SERVER: Z2l6bW8uMTIzcXdl
SERVER -> CLIENT: 235 2.7.0 Accepted
CLIENT -> SERVER: MAIL FROM:<x#gmail.com>
SERVER -> CLIENT: 250 2.1.0 OK 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: RCPT TO:<x#gmail.com>
SERVER -> CLIENT: 250 2.1.5 OK 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: DATA
SERVER -> CLIENT: 354 Go ahead 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: Date: Thu, 20 Oct 2016 20:44:01 +0000
CLIENT -> SERVER: To: Me <x#gmail.com>
CLIENT -> SERVER: From: Me <x#gmail.com>
CLIENT -> SERVER: Reply-To: X <sender#gmail.com>
CLIENT -> SERVER: Subject: Partnership and business development
CLIENT -> SERVER: Message-ID: <6dae364927ad751cc524f07a77f5eea1#www.host.com>
CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.16 (https://github.com/PHPMailer/PHPMailer)
CLIENT -> SERVER: MIME-Version: 1.0
CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-1
CLIENT -> SERVER:
CLIENT -> SERVER: message
CLIENT -> SERVER:
CLIENT -> SERVER: .
SERVER -> CLIENT: 250 2.0.0 OK 1476996370 90sm16517084otw.3 - gsmtp
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection 90sm16517084otw.3 - gsmtp
I replaced the actual values of the site and emails.

Exim: Force email auth for users of local domains

I'm configuring exim4 for virutal users with dovecot and postgresql and I got a problem I cannot resolve, the situation is this:
I need that when a 'localdomain' user tries to send a email to another 'localdomain' user or foreign user, needs to authenticate, otherwise refuse the operation.
Practically I'm trying to avoid any type of smtp open relay.
Actually the smtp works fine, just need to set that requirement, and I can't find the correct way of how I can to configure exim correctly for get this behavior (Is a acl or route or transport rule?)
How could I define this configuration?
Example:
220 my-server ESMTP Exim 4.84 Thu, 31 Mar 2016 22:26:28 +0000
ehlo localhost
250-my-server Hello localhost [192.168.1.X]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250 HELP
mail from: a#my-server.com
250 OK
rcpt to: b#my-server.com
250 Accepted
data
354 Enter message, ending with "." on a line by itself
this must not happen, the user 'a#my-server.com' is not authenticated,
he must not be able to send any message yet until of use
of 'auth login' or 'auth plain' because is a user of local domains.
.
250 OK id=1all3Q-0004l2-V4
Example 2
220 my-server ESMTP Exim 4.84 Thu, 31 Mar 2016 22:58:56 +0000
ehlo localhost
250-my-server Hello localhost [192.168.1.X]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250 HELP
mail from: a#anotherserver.com
250 OK
rcpt to: user#my-server.com
250 Accepted
data
354 Enter message, ending with "." on a line by itself
this is already valid.
.
250 OK id=1allZR-00050E-Sq
ACL Config File:
acl_check_rcpt:
accept
hosts = :
control = dkim_disable_verify
.ifdef CHECK_RCPT_LOCAL_LOCALPARTS
deny
domains = +local_domains
local_parts = CHECK_RCPT_LOCAL_LOCALPARTS
message = restricted characters in address
.endif
deny
domains = !+local_domains
local_parts = CHECK_RCPT_REMOTE_LOCALPARTS
message = restricted characters in address
.endif
accept
.ifndef CHECK_RCPT_POSTMASTER
local_parts = postmaster
.else
local_parts = CHECK_RCPT_POSTMASTER
.endif
domains = +local_domains : +relay_to_domains
.ifdef CHECK_RCPT_VERIFY_SENDER
deny
message = Failed!
!acl = acl_local_deny_exceptions
!verify = sender
.endif
deny
!acl = acl_local_deny_exceptions
senders = ${if exists{CONFDIR/local_sender_callout}\
{CONFDIR/local_sender_callout}\
{}}
!verify = sender/callout
accept
hosts = +relay_from_hosts
control = submission/sender_retain
control = dkim_disable_verify
accept
authenticated = *
control = submission/sender_retain
control = dkim_disable_verify
require
message = Relay denied!
domains = +local_domains : +relay_to_domains
require
verify = recipient
deny
!acl = acl_local_deny_exceptions
recipients = ${if exists{CONFDIR/local_rcpt_callout}\
{CONFDIR/local_rcpt_callout}\
{}}
!verify = recipient/callout
deny
message = sender envelope address $sender_address is locally blacklisted here. If you think this is wrong, get in touch with postmaster
!acl = acl_local_deny_exceptions
senders = ${if exists{CONFDIR/local_sender_blacklist}\
{CONFDIR/local_sender_blacklist}\
{}}
deny
message = sender IP address $sender_host_address is locally blacklisted here. If you think this is wrong, get in touch with postmaster
!acl = acl_local_deny_exceptions
hosts = ${if exists{CONFDIR/local_host_blacklist}\
{CONFDIR/local_host_blacklist}\
{}}
accept
domains = +relay_to_domains
endpass
verify = recipient
accept

Mail rejected using phpmailer on GoDaddy.com host

Using this:
require("lib/class.phpmailer.php");
$mail = new PHPMailer();
$mail->Username = "XXX#validGoDaddyAccount.com";
$mail->Password = "xxx";
$mail->Subject = $subject;
$mail->Body= $message;
$mail->AddAddress($toAddr);
$mail->FromName = "XXXXXXXXXXXX";
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "smtpout.secureserver.net";
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->From = $mail->Username;
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
return true;
}
I get this response:
SMTP -> FROM SERVER:220 p3plsmtpa07-05.prod.phx3.secureserver.net ESMTP
SMTP -> FROM SERVER: 250-p3plsmtpa07-05.prod.phx3.secureserver.net hello [00.00.0.0], secureserver.net 250-HELP 250-AUTH LOGIN PLAIN 250-SIZE 31457280 250-PIPELINING 250-8BITMIME 250 OK
SMTP -> FROM SERVER:550 User XXX has exceeded their send quota
SMTP -> ERROR: MAIL not accepted from server: 550 User XXX has exceeded their send quota
The following From address failed: XXX : MAIL not accepted from server,550,User XXXm has exceeded their send quota
SMTP server error: User XXX has exceeded their send quota
Mailer Error: The following From address failed: XXX : MAIL not accepted from server,550,User XXX has exceeded their send quota
SMTP server error: User XXX has exceeded their send quota
I know about setting the SMTP Relays for GoDaddy accounts and they are set right. I can send email from this account all day long as long as I don't use PHPmailer.
see the following line
SMTP -> FROM SERVER:550 User XXX has exceeded their send quota
That's why it is rejecting.
The solution is that I needed to pre-authorize with POP prior to starting the SMTP operation:
require("lib/class.phpmailer.php");
require_once('lib/class.pop3.php');
$pop = new POP3();
$pop->Authorise('pop.server.net', 110, 30, 'username', 'password', 1);
$mail = new PHPMailer();
...

Resources