swiftmailer Swift_TransportException 未被 try-catch 块捕获 [英] swiftmailer Swift_TransportException gets uncaught by try-catch block

查看:33
本文介绍了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.

我正在使用 Symfony2 及其库.对于联系邮件,我使用的是 Swiftmailer 库.好吧,我有下一个处理表单提交的代码.在那里,我创建了一个邮件对象来发送邮件.它有效,但如果来自发送方/接收方的 smtp 主机出现问题,我想提供错误解决服务.

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.

这是代码,

$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'] = 来自主机的邮件地址/$contactinfo['contactmail'] = 来自网站访问者的邮件地址(以联系方式提交)

现在,当 smtp 主机不工作时,Swiftmailer 确实会发送异常,但 try-catch 块不会捕获它.该功能正在继续.甚至根 try-catch 块(在 app.php 中)也没有捕获它.因此,您会在网页上看到一个大的 PHP 错误,这是不应该发生的.下面描述了来自它的消息,

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>

有谁知道为什么 try catch 块没有捕获自定义异常?我已经调查了类文件和进度,但我没有看到任何异常活动.

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.

我希望有人能对此提出解决方案,因为 PHP 错误不应出现在网站页面上.

I hope that someone can come with a solution to this because PHP errors shouldn't appear on site pages.

推荐答案

从你的 config.yml 中删除spool: { type: memory }

然后你会像这样添加try catch

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() ;
    }

这篇关于swiftmailer Swift_TransportException 未被 try-catch 块捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆