PHPMailer + Gmail - No route to host (113)

Derokorian
Derokorian used Ask the Experts™
on
Ok So I'm trying to update old code to use SMTP authentication through our GMail hosted company email. I'm running into the following error:

SMTP -> ERROR: Failed to connect to server: No route to host (113)
SMTP Error: Could not connect to SMTP host.
My code is (note some things sanitized for public viewing):

<?php
   $paths = get_include_path();
   set_include_path($paths . PATH_SEPARATOR . dirname(__FILE__) . '/phpmailer');
   include_once('class.phpmailer.php');
   $mail = new phpmailer;
      
   // Constant email settings.
   $mail->IsSMTP();
   $mail->SMTPAuth   = TRUE;
   $mail->SMTPSecure = "tls";
   $mail->Host       = "smtp.gmail.com";
   $mail->Port       = 587;
   $mail->Username   = "--@noas.com";
   $mail->Password   = "--";
   $mail->From       = "contact@noas.com";
   $mail->FromName   = "NOAS Contact";
   $mail->WordWrap   = 75;
   $mail->Subject    = "10th Anniversary Dave Thomas Celebrity Cookoff - Chef Invite";
   $mail->AddReplyTo("--@noas.com", "--");
   $mail->IsHTML(true);
   $mail->addBcc('--@noas.com','--');
      
      $mail->SMTPDebug = 2; // For debugging, change to 0 when working
      
   foreach( $emails as $info ) {
      $mail->AddAddress($info['email'], $info['name']);
      $mail->msgHTML(buildmsg($info['name']));
      
      // Send mail message to each recipient
      if( $mail->Send() )
         $content .= 'Message to '. $info['name'] .' sent successfully.<br />';
      else
         $content .= 'Message to '. $info['name'] .' failed to send.<br />';
      
      $mail->clearAddresses();
   }
?>

Open in new window


I've also tried changing to this as in a document included:

<?php
   $mail->SMTPSecure = "tls";
   $mail->Host       = "smtp.gmail.com";
   $mail->Port       = 587;

Open in new window


I was using http://phpmailer.worxware.com/index....=examplebgmail plus the documents included in the ZIP to set up the settings, So I don't see what's wrong. Sometimes I get this error instead:


SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
SMTP Error: Could not connect to SMTP host.

Either way its not sending emails, Any help would be greatly appreciated =D
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Try to change it to SSL not change it to the exact same thing! Oops my bad, this is what I tried.

   $mail->SMTPSecure = "ssl";
   $mail->Host       = "smtp.gmail.com";
   $mail->Port       = 465;

Open in new window

Thought this should be @gmail.com?

$mail->Username   = "username@gmail.com";
$mail->Password   = "password of the gmail account";


Port should be 587
The domain "noas.com" is a Google Apps domain, so the username syntax is valid:
xxx@lenny:~/www/cs/lib$ dig -t MX noas.com

; <<>> DiG 9.7.0-P1 <<>> -t MX noas.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37031
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 2, ADDITIONAL: 0

;; QUESTION SECTION:
;noas.com.                      IN      MX

;; ANSWER SECTION:
noas.com.               108666  IN      MX      10 ASPMX.L.GOOGLE.com.
noas.com.               108666  IN      MX      20 ALT1.ASPMX.L.GOOGLE.com.
noas.com.               108666  IN      MX      30 ALT2.ASPMX.L.GOOGLE.com.
noas.com.               108666  IN      MX      40 ASPMX2.GOOGLEMAIL.com.
noas.com.               108666  IN      MX      50 ASPMX3.GOOGLEMAIL.com.

;; AUTHORITY SECTION:
noas.com.               3543    IN      NS      ns51.domaincontrol.com.
noas.com.               3543    IN      NS      ns52.domaincontrol.com.

;; Query time: 1 msec
;; SERVER: 10.1.0.3#53(10.1.0.3)
;; WHEN: Fri Aug  5 09:06:21 2011
;; MSG SIZE  rcvd: 208

Open in new window

Daniel McAllisterPresident, IT4SOHO, LLC

Commented:
"No route to host" is the error message...

Why not believe it?

The error doesn't say login failed, or connection error -- it says no route to host.

Not to be snide, but I've found that carefully reading the error message (sometimes all 10 words) often leads me directly to the problem!

To me, this means either that the SMTP server info is bad (including perhaps using the wrong syntax to define the SMTP port), or the port number is wrong, or the server's internal DNS isn't working (check /etc/resolv.conf).

But that's just a first stab.... SOMETHING is screwing up the script's ability to make the SMTP connection at all. Worrying about anything that happens AFTER the initial connection is like trying to pre-set your car stereo before you put in the car battery... (as-in you won't be accomplishing much!)


Just my feeble thoughts on the subject...


Dan
IT4SOHO
Good point, IT4SOHO.  On the server having issues, try doing basic dig/nslookup for the smtp server, or even try pinging (this might be blocked in your network or by the destination server, so don't jump to conclusions if there's no response).

Author

Commented:
I'm on shared hosting, and am unable to login via SSH to run any command line parameters that way. If there a way to run these tests using PHP?

 I did read the whole error message, and I posted the code producing the message because obviously something isn't right, even tho it matches the info on Google's page, PHPMailers page, and the included documentation. I know the login info is fine, since I can log in to the account via mail.noas.com however that doesn't matter its not finding the server at all, and I don't know why! I've tried SSL and TLS to no avail.

Any ideas that would simply help identify the problem would be appreciated even if it doesn't help fix them, but at this point I'm at a loss to even figuring out what's causing the problem. BTW I've now tested this code on my local host machine and it works, so its something on the server. Maybe an INI setting is wrong?
I would suggest contacting your hosting provider.  Explain the problem and the things you've tried, they should be able to help you further.

You might be able to run CLI commands via PHP, using "exec()" or "system()".
http://www.php.net/manual/en/function.system.php
http://www.php.net/manual/en/function.exec.php
The commands you could try are:
nslookup smtp.gmail.com
traceroute smtp.gmail.com

Open in new window


I am unable to do a direct connection to smtp.google.com:
user@lenny:~$ telnet smtp.google.com 25
Trying 74.125.121.57...
Trying 216.239.44.95...
telnet: Unable to connect to remote host: Connection refused

Open in new window

Author

Commented:
After talking with my hosting provider it seems they BLOCK any external SSL or TLS connections making it impossible to send from GMail. Thanks for your help in identifying the problem, it would seem to be the fault of the host - unfortunately the email relay I would be required to use has a 45-60min delay on sending emails out (which is horrible for reseting passwords) I guess I won't be able to update our invitation system until we change providers.

Thanks for your help, any suggestions on good linux hosting that won't cost to much?
I host my own servers via a home broadband connection, haven't dealt with hosting in other places except for DNS.  I'm not really sure what you mean by "too much" in terms of cost: the best I can find locally as far as dedicated servers is ~$100; rackspace.com charges ~$200 for about the same.

If you're looking at basic web hosting, BTInet (local to ND) charges ~$9/month... see [ http://www.btinet.net/Web_Hosting/Basic_Website_Hosting/index.html ] for more info.  My experience is that they try to stay as transparent as possible in the whole equation.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial