Yii Framework Forum: Email Exception Not Caught If Mail Server Down - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Email Exception Not Caught If Mail Server Down uncaught exception, Yii mail Rate Topic: -----

#1 User is offline   Ehtesham 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-December 11

Posted 01 July 2013 - 08:25 AM

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://smtp.server.com:465 (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');
try
{
	Yii::app()->mail->send($message);
}
catch (Exception $exc)
{
	Yii::log("email sending failed due to error: " . $exc->getMessage());
}

0

#2 User is offline   Ehtesham 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-December 11

Posted 01 July 2013 - 08:29 AM

Update:
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.
0

#3 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 535
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 01 July 2013 - 09:08 AM

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: http://www.php.net/m...ror-handler.php

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

#4 User is offline   Ehtesham 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 07-December 11

Posted 01 July 2013 - 09:38 AM

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

Yii::app()->mail->send($message);
with this
@Yii::app()->mail->send($message);

0

#5 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 535
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 01 July 2013 - 09:41 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users