Invalid address: SMTP Error: Could not connect to SMTP host

Hi all,

I am trying to configure PHPMailer for using it with Google Apps.
When I call the function I get this error:
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)

And after about 10 seconds, this one also:
SMTP Error: Could not connect to SMTP host.

I researched about this error and I have checked the e-mail address/password. It works fine as I am able to log into Google mail using them
I also checked the port 465, using “telnet smtp.gmail.com 465” I got no error message, but also not an answer from the server, just a black screen.

This is the finction I am using, if anyone can point me where the error is, I’d apreciate it a lot.
Username and Password are just examples)

<?php
require(“/home/libroman/public_html/phpmailer/PHPMailer_5.2.0/class.phpmailer.php”);

function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->SMTPSecure = “ssl”;
$mail->Host=‘smtp.gmail.com’;
$mail->Port=‘465’;
$mail->Username = ‘myemail@mydomain.com’; // SMTP account username
$mail->Password = ‘password’;
$mail->SMTPKeepAlive = true;
$mail->Mailer = “smtp”;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->CharSet = ‘utf-8’;
$mail->SMTPDebug = 0;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);

if(!$mail-&gt;Send()) {
	$error = 'Mail error: '.$mail-&gt;ErrorInfo; 
	return false;
} else {
	$error = 'Message sent!';
	return true;
}

}

?>

Thanks a lot!!!

Hi,

If you have verified the port and the are using the proper smtp then if you can’t connect using telnet it will never work using php mailer. It is likely that you have made an assumption about the smtp or port. You can also check DNS using nslookup to find if smtp.gmail.com is a valid route.

Please don’t waste your time trying to figure out if the PHP is incorrect until you are sure that you can connect to the smtp using simple means (via telnet).

Steve

Hi,

Thanks a lot for your answer.

I have checked what you suggested:

  • I am using port 25 instead of 465, checked with telnet and getting answer
  • Using nslookup and ping I receive answer from smtp.google.com

The problem is when try to use PHPMailer, I get the same error message.

Any othe idea?

Hi,

Do you know if you need SMTP authentication on or off? Have you turned on SMTP debugging ie.

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug  = 1; // errors and messages
$mail->SMTPAuth   = true;

Does the firewall on the server where php mailer is running have port 25 outgoing open or is NAT over-ridding this?

Steve