2014-09-22 35 views
2

如何在symfony控制器/ swift消息中抑制錯誤消息,例如如果用戶不存在的郵件,什麼是錯與SMPT服務器或電子郵件地址根本不符合RFC 2822Symfony/Twig中的Supress錯誤消息?

例如,我們得到了以下關鍵錯誤...

request.CRITICAL:Swift_RfcComplianceException:給定郵箱中的地址[[email protected]]不符合RFC 2822,3.6.2。 (未捕獲的異常)在$

...用戶然後得到一個symfony錯誤頁面「An Error occured」,我需要在任何情況下禁止。

一個簡單的@ $ 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('[email protected]' => '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; 
} 

回答

1

我不知道,一個try catch塊將工作,因爲郵件可以稍後在請求過程中發送:如果您使用的框架完整的堆棧版本http://symfony.com/fr/doc/current/components/http_kernel/introduction.html#l-evenement-kernel-terminate

,這是默認的行爲。

我也發現了一些裁判在這裏:Unable to send e-mail from within custom Symfony2 command but can from elsewhere in app

你可以改變你的配置的Swiftmailer閥芯策略發送直郵...

+0

嗨Yann,這很有趣,我將不得不嘗試一下! – Mike 2014-10-03 10:55:18

4

要簡單地取消錯誤,在塊包發送方法。明智地選擇例外類型。以下示例僅捕獲Swift_RfcComplicanceExceptions

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

我認爲最好事先應用一些驗證,以便您可以向用戶顯示一個很好的錯誤。

+0

嘿devsheeep,謝謝,關於向Yanns評論下面,我如果作品會嘗試。但你的回答對我來說似乎很好! – Mike 2014-10-03 10:55:53