Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]'

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

Question

I had already searched it in google and tried all the suggestions here in StackOverflow but I still get a Fatal Error:

I am using SwiftMailer for sending and email to GMAIL. It works perfectly on my localhost but when I uploaded it and try It gives me a Fatal Error:

Here is a part of my code:

require_once 'Swift-5.1.0/lib/swift_required.php';


$email = "fromemail@gmail.com";
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465);
$transport->setUsername("username@gmail.com");
$transport->setPassword("usernamepassword");

// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
   "to@gmail.com" => "First Name"
));
$message->setCc(array("cc_email1@gmail.com" => "Name NAme" , "cc_email2@gmail.com" => "His Name"));

$message->setSubject("Subject");

$message->setBody("The Body");

$message->setFrom($email, "Full Name");

$message->setReplyTo(array($email => "Full Name"));

// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);

This is the Fatal Error I get when I send an email to gmail :

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]' in /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/StreamBuffer.php:266 Stack trace: #0 /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/StreamBuffer.php(64): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2 /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /homepages/41/d513702051/htdocs/php/contactus.php(33): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/StreamBuffer.php on line 266

Was it helpful?

Solution

I'm not sure but I think when you adding port 465, Swift class automatically will add ssl protocol.

Try

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465);

OTHER TIPS

In the case of using Windows and XAMPP, the solution is to either deactivate your anti-virus software or open the proper port within it.

In my case, I had installed Avast and was getting this same problem (I had thought it may have been the Apache or PHP configuration within XAMPP, but no dice). Each time, once I deactivated my anti-virus software, it worked.

You must include this (for Swiftmailer):

$transport->setStreamOptions(array('ssl' => array('allow_self_signed' => false,'verify_peer' => false))); 

That's all folks

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top