by: Helge, published: May 2, 2012, in

Authentication Fails Trying to Send Mail via Google Apps SMTP from PHP

As you may know helgeklein.com also hosts a forum where I help people who have questions related to my software. The forum is configured to send out e-mails when new posts are added or when newly registered users need to confirm their e-mail addresses. Since I use Google Apps for helgeklein.com I configured Google’s SMTP server for outgoing e-mails. All worked well until recently when I started to wonder why I had not received new forum post e-mails for several days.

The Error

I registered a test user in the forum and initiated sending a confirmation e-mail. After enabling my forum’s debug mode I got this:

Hmm, SMTP Error: Could not authenticate sounds like a problem on Google’s end. I tried every combination of authentication mechanisms (TLS and SSL) and ports (587 and 465) and changed the password – nothing worked.

I tried to log in to Google Mail and to connect with Thunderbird via SMTP – both worked.

I tried to debug the script used by the forum to send e-mail. PHPMailer seems to be well known and many people have reported all kinds of problems, but the suggested solutions did not apply in my case.

The Analysis

Finally, after a long bug-hunt I found this answer on stackoverflow, where netcoder explains that many providers redirect outgoing SMTP connections back to localhost. After I had read it I was speechless. And I suddenly realized what I had seen earlier in a log I had generated by replacing line 815 of class.phpmailer.php with my own debug output code:

 $connection = true;
 if ($this->SMTPAuth) {
   if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
     $arr = $this->smtp->getError();                // inserted this...
     throw new phpmailerException($arr('error'));   // ...and this
     throw new phpmailerException($this->Lang('authenticate'));
   }
 }

The output looked like this:

SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "220-panther.directrouter.co.uk ESMTP Exim 4.77 #2 Thu, 26 Apr 2012 20:40:38 +0000 "
SMTP -> get_lines(): $data is "220-panther.directrouter.co.uk ESMTP Exim 4.77 #2 Thu, 26 Apr 2012 20:40:38 +0000 "
SMTP -> get_lines(): $data was "220-panther.directrouter.co.uk ESMTP Exim 4.77 #2 Thu, 26 Apr 2012 20:40:38 +0000 "
SMTP -> get_lines(): $str is "220-We do not authorize the use of this system to transport unsolicited, "
SMTP -> get_lines(): $data is "220-panther.directrouter.co.uk ESMTP Exim 4.77 #2 Thu, 26 Apr 2012 20:40:38 +0000 220-We do not authorize the use of this system to transport unsolicited, "
SMTP -> get_lines(): $data was "220-panther.directrouter.co.uk ESMTP Exim 4.77 #2 Thu, 26 Apr 2012 20:40:38 +0000 220-We do not authorize the use of this system to transport unsolicited, "
SMTP -> get_lines(): $str is "220 and/or bulk e-mail. "
SMTP -> get_lines(): $data is "220-panther.directrouter.co.uk ESMTP Exim 4.77 #2 Thu, 26 Apr 2012 20:40:38 +0000 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail. "
SMTP -> FROM SERVER:220-panther.directrouter.co.uk ESMTP Exim 4.77 #2 Thu, 26 Apr 2012 20:40:38 +0000 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
{...]
SMTP -> get_lines(): $data is "250-panther.directrouter.co.uk Hello panther.directrouter.co.uk [212.113.141.30] 250-SIZE 52428800 250-PIPELINING 250-AUTH PLAIN LOGIN "
SMTP -> get_lines(): $data was "250-panther.directrouter.co.uk Hello panther.directrouter.co.uk [212.113.141.30] 250-SIZE 52428800 250-PIPELINING 250-AUTH PLAIN LOGIN "
SMTP -> get_lines(): $str is "250 HELP "
SMTP -> get_lines(): $data is "250-panther.directrouter.co.uk Hello panther.directrouter.co.uk [212.113.141.30] 250-SIZE 52428800 250-PIPELINING 250-AUTH PLAIN LOGIN 250 HELP "
SMTP -> FROM SERVER: 250-panther.directrouter.co.uk Hello panther.directrouter.co.uk [212.113.141.30] 250-SIZE 52428800 250-PIPELINING 250-AUTH PLAIN LOGIN 250 HELP
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "334 USERNAME "
SMTP -> get_lines(): $data is "334 USERNAME "
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "334 PASSWORD "
SMTP -> get_lines(): $data is "334 PASSWORD "
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "535 Incorrect authentication data "
SMTP -> get_lines(): $data is "535 Incorrect authentication data "
SMTP -> ERROR: Password not accepted from server: 535 Incorrect authentication data

My server’s name is panther, and it is pretty obvious that it is talking to itself. My forum’s script was connecting to my own server’s mail server instead of Google’s!

The Solution

I contacted my provider’s technical support and asked them nicely to enable communication to smtp.googlemail.com. They were kind enough to do it, and all is well again.

Previous Article Removing "Eject VMware Virtual disk SCSI Disk Device"
Next Article Setting up Mobile Broadband (WWAN) on HP Elitebook 8560p