dopq87915 2016-10-25 19:51
浏览 63
已采纳

Silex Swiftmailer Swift_TransportException无法捕获

I am developing a web applicationen on base of the silex framework with decent Mail functionality. Sending mails works fine with the basic configuration and established connection to the mail server. In the case of errors e.g. due to connection loss, i want to log the causes and prevent them to be sent to the client. But iam unable to catch the mailer errors, especially the Swift_TransportException. I encapsulated the call of the 'send' function and also added an Error handler, both without success. Is this a bug, or am i missing something?

The error handler:

    $app->error(function (\Swift_TransportException $e, Request $request, $code) {
      //TODO log
    });

The usage of the send function:

        try{
            $message = \Swift_Message::newInstance()
                ->setSubject($mail->getSubject())
                ->setFrom($mail->getFrom())
                ->setTo($mail->getTo())
                ->setBody($mail->getBody());

            $app['mailer']->send($message);
        }
        catch(\Swift_TransportException $exception)
        {
          //TODO log
        }
  • 写回答

1条回答 默认 最新

  • dtrhd2850 2016-11-03 15:54
    关注

    I found a solution to my problem in this post:

    silex-swiftmailer-not-making-smtp-connection-upon-execution

    To sum it up, the problem was caused by the default behaviour of the swiftmailer in silex. By default the mails aren't send directly at code execution time, but buffered and send after the request result is returned to the client. Therefore errors aren't caught in the try block. To change this behavior i added slightly modified code from the referenced post to my mailer registration.

    $app->register(new Silex\Provider\SwiftmailerServiceProvider());
    $app['mailer'] = new \Swift_Mailer($app['swiftmailer.transport']);
    

    thanks to igor for the good explanation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题