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.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向