How the laravel Mail::failures() function works?

空扰寡人 提交于 2020-06-27 07:12:25

问题


I've been trying to get the list of recipients who did not get email using laravel Mail::send() function. I am trying following code. for loop is used as because each user to be received customized message.

// First recipient is actual, the second is dummy.
$mail_to_users = ["original_account@domain.com","dummy_account@domain.com"];
$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);

I've been trying all the possible option. I changed the correct mail config in config/mail.php to the wrong one. But if I do this then laravel shows error page, but $failure variable always return empty.


回答1:


I think there is no way to check email is actually gone to the receipient or not. As long as the email is valid (even though dummy) it will return true. However, Instead of Mail::failures() you can use try catch block as follows:

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


来源:https://stackoverflow.com/questions/32045181/how-the-laravel-mailfailures-function-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!