swiftmailer Swift_TransportException gets uncaught by try-catch block

php, swiftmailer, symfony

Solution

remove from your config.yml `spool: { type: memory }`

Then you will be to add try catch like this

    try{
        $mailer = $this->getMailer();
        $response = $this->getMailer()->send($message);
    }catch(\Swift_TransportException $e){
        $response = $e->getMessage() ;
    }

Problem

For a project (which is about estate properties) i have included a contact form where a visitor can contact an estate broker if the visitor is interested to buy/hire an estate property. I am using Symfony2 and its library. For the contact mail, i am using the Swiftmailer library. Well, i have the next code which handles the form submit. there, I create a mail object to be able to send mails. It works, but i want to provide a error resolving service if there are problems with the smtp host from sender/receiver. Here is the code, ``` $data = $contactForm->getData(); try { // create body text $body = $app['twig']->render('mailTemplate.twig', array('data' => $data, 'immoid' => $immoID)); // set mail $mail = \Swift_Message::newInstance() ->setSubject('Contact reaction on your immo offer.') ->setFrom($app['swiftconfig']['sender']) ->setTo($contactinfo['contactmail']) ->setBody($body, 'text/html'); // send mail $app['mailer']->send($mail); // redirect if successfull $app->redirect($app['url_generator']->generate('immoDetail', array('immoID' => $immoID))); } catch (Swift_TransportException $STe) { // logging error $string = date("Y-m-d H:i:s") . ' - ' . $STe->getMessage() . PHP_EOL; file_put_contents("errorlog.txt", $string, FILE_APPEND); // send error note to user $errorMsg = "the mail service has encountered a problem. Please retry later or contact the site admin."; } catch (Exception $e) { // logging error $string = date("Y-m-d H:i:s") . ' - GENERAL ERROR - ' . $e->getMessage() . PHP_EOL; file_put_contents("errorlog.txt", $string, FILE_APPEND); // redirect to error page $app->abort(500, "Oops, something went seriously wrong. Please retry later !"); } ``` ($app['swiftconfig']['sender'] = mailaddress from host / $contactinfo['contactmail'] = mailaddress from site visitor (submitted in contact form)) Now, when the smtp host doesn't work, Swiftmailer DOES send an exception, but the try-catch block ISN'T catching it. The function is just being continued. Even the root try-catch block (in app.php) isn't catching it too. As a result of this, you see a large PHP error on the webpage, which shouldn't happen. The message from it is described here below, ``` SCREAM: Error suppression ignored for --- Fatal error: Uncaught exception 'Swift_TransportException' with message ' in C:\...\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php on line 266 --- Swift_TransportException: Connection could not be established with host <output omitted> ``` Does anyone know why the try catch block isn't catching the custom exception ? I have investigated the class files and the progress, but i don't see any unusual activity. I hope that someone can come with a solution to this because PHP errors shouldn't appear on site pages.

Original source