Swiftmailer exception doesn't catch in Symfony2 controller
Asked Answered
H

2

8

I am developing a simple mailing application with Gmail account as host.It works like a charm but the problem rises when send() function throw an exception. I see that try catch statement can't handle the exception. It doesn't work even when I use Global exception class. this question discussed in somewhere also .

for example :

Catch swiftmailer exception in Symfony2 dev env controller

or

https://groups.google.com/forum/#!topic/symfony2/SlEzg_PKwJw

but they didn't reach a working answer.

My controller function code is :

    public function ajaxRegisterPublisherAction()
    {

//some irrelevant logic

     $returns=  json_encode(array("username"=>$username,"responseCode"=>$responseCode));


    try{
            $message = \Swift_Message::newInstance()
        ->setSubject('hello world')
        ->setFrom('[email protected]')
        ->setTo('[email protected]')
        ->setBody(
            $this->renderView('AcmeSinamelkBundle:Default:email.txt.twig',array('name'=>$username,"password"=>$password))
        );
      $this->container->get("mailer")->send($message);

    }
   catch (Exception $e)
    {

    }



    return new \Symfony\Component\HttpFoundation\Response($returns,200,array('Content-Type'=>'application/json'));


    }

The response that sent from above code that I receive in firebug console is :

{"username":"xzdvxvvcvxcv","responseCode":200}<!DOCTYPE html>
<html>
    .
    .
Swift_TransportException: Connection could not be established with host smtp.gmail.com [Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?]
    .
    .
</html>

and I catch my hairs because I don't know why the kernel handle the exception in continue of my json object?

when I comment this line:

 $this->container->get("mailer")->send($message);

the exception doesn't occur and I have a valid json in client side.(that is matter-of- course although) I changed Exception to \Exception or \Swift_TransportException or even Swift_TransportException ! but no good result.

Hyperactive answered 13/8, 2013 at 17:37 Comment(5)
Did you try Swift_SwiftException? In the file it says "Base Exception class."Gal
i mean 'Exception' by "Base Exception class"Hyperactive
I tested with "Swift_SwiftException" and "\Swift_SwiftException" now.It doesn't workHyperactive
do you use the spooler?Goldplate
I don't know what do you mean by spooler but I exactly do the setting in symfony.com/doc/current/cookbook/email/gmail.html except that I intentionally inactive openssl extension in php.ini in order to rise a sample exception!! thank you :)Hyperactive
W
3

When you do $this->container->get("mailer")->send($message); the email message is not being sent at that point if you have spooling turned on. See http://symfony.com/doc/current/cookbook/email/spool.html

If you have the default setting of spool: { type: memory }, the \Swift_TransportException will be thrown during the kernel termination phase, after your controller has exited. One way around this is to turn off the spooling (but then your users might have to wait while the email is sent), or you can make your own eventlistener to handle the exception. http://symfony.com/doc/current/cookbook/service_container/event_listener.html

Warila answered 11/10, 2014 at 21:55 Comment(1)
this problem belongs to so many times ago.at the final we didn't able to solve the problem and so used another mailing library.unfortunately I don't know if your solution working or not nowHyperactive
J
2

you need to beat the event dispatcher before it send back an exception, so listen to these kind of events and silence them, though i think this is a BAD way to deal with it

class SuchABadRequest implements \Swift_Events_TransportExceptionListener{

    /**
     * Invoked as a TransportException is thrown in the Transport system.
     *
     * @param \Swift_Events_TransportExceptionEvent $evt
     */
    public function exceptionThrown(\Swift_Events_TransportExceptionEvent $evt)
    {

        $evt->cancelBubble();

        try{
            throw $evt->getException();
        }catch(\Swift_TransportException $e){
            print_r($e->getMessage());
        }
    }
}

inside the controller

$mailer = $this->get('mailer');

$mailer->registerPlugin(new SuchABadRequest());
Jacobite answered 8/10, 2013 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.