dongying3830 2013-07-17 09:19
浏览 63

swiftmailer Swift_TransportException被try-catch块无法捕获

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.

  • 写回答

7条回答 默认 最新

  • duanmiexi2275 2013-07-29 10:07
    关注

    Sure you have the latest version of swiftmailer? Line 226 of StreamBuffer.php doesn't throw any exception. This version from 2 years ago does throw the exception on that specific line.

    I'd try running the latest version before diving into the issue more deeply.

    评论

报告相同问题?

悬赏问题

  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件