Email Exception Not Caught If Mail Server Down

I am using ext.yii-mail.YiiMail class for sending emails in Yii.

It works fine as long as email/smtp server is up and running. but if its unreachable/down I get a 500 internal server error and exception thrown is never caught.

here are logs from application.log

2013/07/01 18:11:21 [error] [php] <-> fsockopen(): unable to connect to tls:// (Connection timed out) (/protected/extensions/yii-mail/vendors/swiftMailer/classes/Swift/Transport/StreamBuffer.php:233)

Stack trace:

#0 /protected/extensions/yii-mail/vendors/swiftMailer/classes/Swift/Transport/AbstractSmtpTransport.php(101): Swift_Transport_StreamBuffer->initialize()

#1 /protected/extensions/yii-mail/vendors/swiftMailer/classes/Swift/Mailer.php(74): Swift_SmtpTransport->start()

I am using the following code and even though the exception is generated in try block it is never caught and I get a 500 internal server error.

$message->view = 'test';

$message->subject = "Test Email";

$message->setBody(array(), 'text/html');





catch (Exception $exc)


	Yii::log("email sending failed due to error: " . $exc->getMessage());



I have tried using

catch (\Exception $exc) 

instead of

catch (Exception $exc)

to see if it was a namespace issue but that did’nt help as well.

fsockopen() does not throw an exception it throws a php error.

You can only catch PHP errors when you define you own php error handler:

Btw: Yii2 converts php errors to exceptions. For version 1.1 this is not implemented.

Thanks, got the php error thing.

But hey I got it fixed and its working fine now even catching the exception.

Here is the solution, might help someone else. I just muted the error by replacing


with this


Muting errors should not be done too often and only in specific cases.

You might get problems debugging things that are not working. So be careful using @ for muting errors.