PHPMailer's infamous SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed

StackOverflow https://stackoverflow.com/questions/21693388

Pergunta

I'm trying to get PHPMailer working using a Google Apps SMTP server. I've tried:

  1. Uncommenting openssl in php.ini
  2. Telneting Google's server on port 465 (success)
  3. Telneting my webserver on port 465 (success)
  4. Telnetting Google's server from my server (success)
  5. Checking DNS SPF/MX records (and sanitizing to IPv4)
  6. tls on port 587
  7. webhost confirms they allow outbound SMTP traffic
  8. google captcha's unlock trick
  9. Reading everything I could find on StackOverflow (solutions covered the above)

Can someone provide a solution to the timed out connection?

Here's my code:

require_once ( 'class.phpmailer.php' ); // Add the path as appropriate
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host        = "smtp.gmail.com"; // Sets SMTP server
$Mail->SMTPDebug   = 1; // 2 to enable SMTP debug information
$Mail->SMTPAuth    = TRUE; // enable SMTP authentication
$Mail->SMTPSecure  = "ssl"; //Secure conection
$Mail->Port        = 465; // set the SMTP port
$Mail->Username    = 'account@googleappsaddress.com'; // SMTP account username
$Mail->Password    = 'mypassword'; // SMTP account password
$Mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet     = 'UTF-8';
$Mail->Encoding    = '8bit';
$Mail->Subject     = 'Test Email Using Gmail';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From        = 'myemail@gmail.com';
$Mail->FromName    = 'GMail Test';
$Mail->WordWrap    = 900; // RFC 2822 Compliant for Max 998 characters per line

$Mail->AddAddress( $to ); // To:
$Mail->isHTML( TRUE );
$Mail->Body    = $body;
$Mail->AltBody = $MessageTEXT;
$Mail->Send();
$Mail->SmtpClose();

if(!$Mail->Send()) {
    $error = 'Mail error: '.$Mail->ErrorInfo; 
    echo($error);
    return false;
} else {
    $error = 'Message sent!';
    return true;
}
Foi útil?

Solução

It turns out I was a victim of coincidence. When I ssh'ed to my server and telneted to Gmail, it was open - however, due to the openssl not being uncommented, I suspect that is why it initially failed.

However, while attempting to get it working, the host firewall blacklisted Gmail. They're currently looking into why, but the short answer is it is white-listed and working.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top