2015-08-15 96 views
0

我嘗試了幾種方法,但沒有工作,其中包括一個在StackOverFlow -> Swiftmailer TransportException get uncaught by try catch block與Swiftmailer捕獲塊異常不工作

我需要捕捉,可以在我上課的時候在控制檯上運行誰運行類Swiftmailer的實例來發送電子郵件

/** 
* @throws Be msut catch swiftmailer exception Swift_TransportException because ->setTo($send_to) containt '' (not valid email sender) 
* But not catch my code. 
*/ 

$transport = \Swift_SmtpTransport::newInstance() 
      ->setHost($settings['smtp_host']) 
      ->setPort($settings['smtp_port']) 
      ->setEncryption($settings['smtp_security']) 
      ->setUsername($settings['smtp_user']) 
      ->setPassword($settings['smtp_pass']); 
     $mailer = \Swift_Mailer::newInstance($transport); 

    echo "Verify code\n"; 
    try { 
     $message = \Swift_Message::newInstance() 
      ->setSubject($subject) 
      ->setFrom(array($settings['smtp_mail_sender'] => $settings['smtp_name_sender'])) 
      ->setTo($send_to) 
      ->setBody($msg); 
     $result = $mailer->send($message); 
    } catch (\Swift_TransportException $Ste) { 
     echo "EROORRRRRRRRRRRRRRRRRRRR\n\n\n"; 
    } 

顯示錯誤的塊產生的異常

php -q cprsyncbackup.php -option param 
No llegue 
PHP Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [] does not comply with RFC 2822, 3.6.2.' in /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php:348 
Stack trace: 
#0 /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php(263): Swift_Mime_Headers_MailboxHeader->_assertValidAddress('') 
#1 /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php(106): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array) 
#2 /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php(63): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array) 
#3 /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/li in /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/cprsync/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php on line 348 
+0

代碼拋出'Swift_RfcComplianceException'但漁獲'Swift_TransportException'。後者是否延伸前者? –

回答

3

你正在包裝try ... catch-圍繞你的swiftmailer郵件組成的錯誤部分。

the manual

摘錄:

如果添加自動根據數據源上的收件人可能 包含無效的電子郵件地址,就可以防止可能出現的異常 使用Swift_Validate::email($email)驗證地址和 只增加地址是驗證。另一種方法是將您的 setTo()setCc()setBcc()調用放在try-catch塊中,並在catch塊中處理 Swift_RfcComplianceException

因此,你應該同時添加地址到你的Swift_Message -object,像這樣使用它:

$message = Swift_Message::newInstance(); 

// add some message composing here... 

$email = "somewrongadress.org"; 
try { 
    $message->setTo(array($email)); 
} catch(Swift_RfcComplianceException $e) { 
    echo "Address ".$email." seems invalid"; 
} 

/* and now your transport... */ 
try { 
    $result = $mailer->send($message); 
} catch (\Swift_TransportException $Ste) { 
    echo "EROORRRRRRRRRRRRRRRRRRRR\n\n\n"; 
} 

That should do the job. 
+0

非常感謝鏈接到新手冊。 – abkrim

+0

對我來說工作正常,但在閱讀完小技巧後,我更願意用Swift_Validate :: emaIL($ EMAIL)創建我自己的摘錄, – abkrim