Catching/handling exceptions Swift_BadResponseException

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
EricWinchell
Forum Newbie
Posts: 5
Joined: Tue Aug 28, 2007 2:27 pm

Catching/handling exceptions Swift_BadResponseException

Post by EricWinchell »

Can anyone tell me how to catch this exception and gracefully handle it? I'm trying to create a sort of spooler daemon and don't want the script to die.


Fatal error: Uncaught exception 'Swift_BadResponseException' with message 'Expec
ted response code(s) [250] but got response []' in C:\Program Files\Apache Group
\Apache2\htdocs\cap\trunk\app\vendors\Swift.php:250
Stack trace:
#0 C:\Program Files\Apache Group\Apache2\htdocs\cap\trunk\app\vendors\Swift.php(
310): Swift->assertCorrectResponse(Object(Swift_Events_ResponseEvent), 250)
#1 C:\Program Files\Apache Group\Apache2\htdocs\cap\trunk\app\vendors\Swift.php(
262): Swift->command('HELO localhost....', 250)
#2 C:\Program Files\Apache Group\Apache2\htdocs\cap\trunk\app\vendors\Swift.php(
220): Swift->handshake(Object(Swift_Events_ResponseEvent))
#3 C:\Program Files\Apache Group\Apache2\htdocs\cap\trunk\app\vendors\Swift.php(
101): Swift->connect()
#4 C:\Program Files\Apache Group\Apache2\htdocs\cap\trunk\app\email_spooler.php(
48): Swift->__construct(Object(Swift_Connection_SMTP))
#5 {main}
thrown in C:\Program Files\Apache Group\Apache2\htdocs\cap\trunk\app\vendors\S
wift.php on line 250
EricWinchell
Forum Newbie
Posts: 5
Joined: Tue Aug 28, 2007 2:27 pm

Post by EricWinchell »

I suppose the exception just needs to be caught, like this?

Code: Select all

try {
        $swift->send( ... );
    } catch (Exception $e) {
        do_something_with_failed($recipient);
        $swift->reset();
    }
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

EricWinchell wrote:I suppose the exception just needs to be caught, like this?

Code: Select all

try {
        $swift->send( ... );
    } catch (Exception $e) {
        do_something_with_failed($recipient);
        $swift->reset();
    }
The exception isn't being thrown during the send(), it's being thrown during the "new Swift(...)" call. You need to use a try/catch yes ;)
EricWinchell
Forum Newbie
Posts: 5
Joined: Tue Aug 28, 2007 2:27 pm

Post by EricWinchell »

I love lamp
snursita
Forum Newbie
Posts: 8
Joined: Thu Jul 12, 2007 2:58 am

Post by snursita »

I also have the same problem, and unfortunately I use PHP4. How can I catch the exception? Sorry I never emulate a try catch in PHP4 before.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

snursita wrote:I also have the same problem, and unfortunately I use PHP4. How can I catch the exception? Sorry I never emulate a try catch in PHP4 before.
PHP4 is being dropped in February, because it's a pain to deal with, but basically you need to use set_error_handler() to catch the errors and deal with them. There is a Swift_Error::expect() workaround but I've not had a massive amount of success with it unless you know the implementation (i.e. it works for me, but does unexpected things for others).

If you can upgrade to PHP5, or push your host to upgrade then do that, otherwise jusy use set_error_handler().

http://www.swiftmailer.org/gophp5/
Post Reply