laravel Mail::failures() 函数是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32045181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:12:21  来源:igfitidea点击:

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

phpemaillaravellaravel-4swiftmailer

提问by Rajan Rawal

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. forloop is used as because each user to be received customized message.

我一直在尝试使用 laravelMail::send()功能获取未收到电子邮件的收件人列表。我正在尝试以下代码。for循环被用作因为每个用户要接收定制的消息。

// 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);

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

我一直在尝试所有可能的选择。我将正确的邮件配置更改为错误的配置config/mail.php。但是如果我这样做,那么 Laravel 会显示错误页面,但$failure变量总是返回空。

回答by Hasan Tareque

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:

我认为没有办法检查电子邮件实际上是否已发送给收件人。只要电子邮件有效(即使是虚拟的),它就会返回 true。但是,您可以使用 try catch 块代替 Mail::failures(),如下所示:

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