Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swiftmailer exception doesn't catch in Symfony2 controller

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.

like image 636
CoderInNetwork Avatar asked Aug 13 '13 17:08

CoderInNetwork


2 Answers

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

like image 178
Terje Bråten Avatar answered Nov 14 '22 23:11

Terje Bråten


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());
like image 21
ROLO Avatar answered Nov 14 '22 22:11

ROLO