2015-08-17 81 views
3

我一直在嘗試使用laravel Mail::send()函數獲取沒有收到電子郵件的收件人列表。我正在嘗試下面的代碼。 for循環用作因爲每個用戶都要接收定製的消息。laravel Mail :: failures()函數是如何工作的?

// First recipient is actual, the second is dummy. 
$mail_to_users = ["[email protected]","[email protected]"]; 
$failures = []; 

foreach($mail_to_users as $mail_to_user) { 
    Mail::send('email', [], function($msg) use ($mail_to_user){ 
    $msg->to($mail_to_user); 
    $msg->subject("Document Shared"); 
    }); 

    if(count(Mail::failures()) > 0) { 
     $failures[] = Mail::failures()[0]; 
    } 
} 

print_r($failures); 

我一直在嘗試所有可能的選擇。我將config/mail.php中正確的郵件配置更改爲錯誤的配置。但如果我這樣做,然後laravel顯示錯誤頁面,但$failure變量總是返回空。

+2

值得什麼,只有非常特殊類型的故障是由Swiftmailer重新調整:幾乎只有在這個意義上失敗的方法用於郵件拒絕了地址/電子郵件。使用'SMTP'這通常是可以的,因爲SMTP服務器會立即返回失敗,但像使用sendmail這樣的東西很少(如果有的話)會返回失敗,因爲電子郵件只是被接受而不管,然後發送。因此,如果您在郵件配置中使用'sendmail'(或者甚至可能是'mail'),則可能需要使用不同的方式來監視傳遞失敗。 – alexrussell

+0

請參閱http://swiftmailer.org/docs/sending.html以獲取何時進行的文檔以及是否返回故障。 – alexrussell

回答

0

我覺得沒有辦法檢查郵件實際上是否已經到達收件人或沒有。只要電子郵件有效(即使是假的),它也會返回true。然而,相反的郵件::失敗(),您可以使用try catch塊如下:

foreach ($mail_to_users as $mail_to_user) { 
      try { 
       Mail::send('email', [], function($msg) use ($mail_to_user) { 
        $msg->to($mail_to_user); 
        $msg->subject("Document Shared"); 
       }); 
      } catch (Exception $e) { 

       if (count(Mail::failures()) > 0) { 
        $failures[] = $mail_to_user; 
       } 
      } 
     } 
+0

我也一樣,但是這對我來說不起作用:-( –

+0

如果你能告訴讀者可以通過方法調用拋出什麼樣的異常,會有幫助:) – AshMenhennett