11

I downloaded SwiftMailer 4.1.6 for sending email using Gmail. I had written the following code for that purpose.

<?php

require_once 'swiftmailer/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
        ->setUsername('[email protected]')
        ->setPassword('***********');

$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
        ->setFrom(array('[email protected]' => 'Jomit Jose'))
        ->setTo(array('[email protected]' => 'Jomit Jose'))
        ->setBody('This is the text of the mail send by Swift using SMTP transport.');

$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);

and it resulted in the following error:

Fatal error: Uncaught exception 'Swift_TransportException' with message 
'Connection could not be established with host smtp.gmail.com 
[Permission denied #13]' in 
/home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:266 

Stack trace: 

#0 /home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php(66): 
Swift_Transport_StreamBuffer->_establishSocketConnection() 

#1 /home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(117):   
Swift_Transport_StreamBuffer->initialize(Array) 

#2 /home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Mailer.php(79): 
Swift_Transport_AbstractSmtpTransport->start() 

#3 /home/jomit/public_html/email_test/test.php(16): 
Swift_Mailer->send(Object(Swift_Message)) 

#4 {main} thrown in /home/jomit/public_html/email_test/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 266

What could have been go wrong?

4
  • try port using 443 instead of 465
    – maxjackie
    Apr 2, 2012 at 8:58
  • @maxjackie Oops! It also results in the same error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Permission denied #13]'
    – Jomoos
    Apr 2, 2012 at 9:03
  • Does your PHP set-up support SSL? Apr 2, 2012 at 9:08
  • @ÁlvaroG.Vicario phpinfo() shows OpenSSL support enabled
    – Jomoos
    Apr 2, 2012 at 9:12

1 Answer 1

37

We had this problem and the cause turned out to be a SELinux setting that prevents Apache and therefore PHP from opening any outgoing socket connections. We had disabled it but forgot the -P parameter so it was reverted in the next reboot. If you are using CentOS, RHEL or some other SELinux enabled distribution, this is a likely cause for the problem.

The connection restriction can be disabled with this command:

setsebool -P httpd_can_network_connect on
5
  • 1
    Had this problem with an OrangeHRM installation. Took me a while to find this hidden answer. Thanks a lot.
    – jasperado
    Jul 24, 2014 at 14:23
  • 1
    After a long day of searching, I found this post and it solved my problem with flyspray not sending emails, thanks :)
    – steabert
    Aug 20, 2014 at 15:51
  • I know this is old, but if you get a message saying "Cannot set persistent booleans without managed policy", then try running "sudo setsebool -P httpd_can_network_connect on" Dec 10, 2015 at 15:05
  • This got me in the right direction. On my PHP server, I have outgoing connections that have auto-banned IPs. I had to go in there and allow each one for smtp.office365.com through.
    – jc.021286
    Oct 6, 2016 at 16:47
  • what is the command for reversing setsebool -P httpd_can_network_connect on ??
    – dev4life
    Jan 5, 2017 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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