4

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

1
  • Can anyone help me with this problem ????
    – ggcodes
    May 2, 2014 at 5:10

3 Answers 3

2

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);
1

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.

1
  • Not sure why this is downvoted, if it's a legit work around for this user it can help other people. Dec 30, 2015 at 20:00
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.