symfony - 在 Symfony/Twig 中抑制错误消息?

标签 symfony swiftmailer

如何抑制 symfony Controller /swift 消息中的错误消息,例如如果用户的电子邮件不存在,则 smpt 服务器有问题或电子邮件地址根本不符合 RFC 2822。

例如,我们遇到以下严重错误...

request.CRITICAL: Swift_RfcComplianceException: Address in mailbox given [ xxx@xxx.com ] does not comply with RFC 2822, 3.6.2. (uncaught exception) at $

...然后用户会得到一个 symfony 错误页面“发生错误”,无论如何我都需要抑制它。

不幸的是,一个简单的@$this->get('mailer')->send($message);在这里不起作用......

protected function generateEmail($name)
{
        $user = $this->getDoctrine()
            ->getRepository('XXX')
            ->findOneBy(array('name' => $name));

        if (!$user) {
            exit();
        }
        else {
            $message = \Swift_Message::newInstance()
            ->setSubject('xxx')
            ->setFrom(array('xxx@xxx.com' => 'xxx'))
            ->setTo($user->getEmail())
            ->setContentType('text/html')
            ->setBody(
                $this->renderView(
                    'AcmeBundle:Template:mail/confirmed.html.twig'
                )
            )
            ;
            $this->get('mailer')->send($message);
            // a simple @$this->get('mailer')->send($message); doesn't work here
        }

    return true;
}

最佳答案

要简单地抑制错误,请将发送方法包装在 try-catch 中堵塞。明智地选择异常类型。以下示例仅捕获 Swift_RfcComplicanceExceptions .

try {
    $this->get('mailer')->send($message);
} catch (\Swift_RfcComplianceException $exception) {
    // do something like proper error handling or log this error somewhere
}

我认为事先应用一些验证是明智的,这样您就可以向用户显示一个很好的错误。

关于symfony - 在 Symfony/Twig 中抑制错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25972410/

相关文章:

php - Symfony2 将复选框值从 0/1 更改为 'no'/'yes'

php - 使用 Swiftmailer 将唯一参数传递给 SendGrid

php - 从 symfony 中的 swift mailer 发送时防止电子邮件中的标题

PHP + MySQL 存储 UTC 时间戳与日期时间

forms - 如何注入(inject) Symfony 表单扩展?

twig 模板布局中的 javascript 产生错误

php - 如何在 Controller symfony 2 中渲染 Twig 宏

php - Swiftmailer 在 'cc' 为空时提示

php - Mysqli fetch array 警告,无法调试

用于阅读电子邮件的 PHP 库