Laravel 5.3 将邮件发送到多个电子邮件地址

Laravel 5.3 send Mail to multiple email-adresses

我正在使用 Laravel 5.3 并尝试将时事通讯邮件发送到特定的邮件地址。

目前我在做以下事情:

$newsletterMailAdresses = Newsletter::where('user_id', $userID)->pluck('mailAdress');

这个 returns 电子邮件地址数组,电子邮件应该发送到这些地址。

然后我(通常)用来发送邮件的是:

Mail::to("someMailAdress")->send(new newsletterMail($newsletterText));

我考虑过将该数组传递给 to 函数,但我很确定它不会起作用....您可能会做的是对每个邮件地址进行 foreach 并发送邮件,但这是应该如何完成还是有更好的方法?

PS:我知道 that 线程,但它是关于 Laravel 4,所以有很多东西改变了。

我认为当你传递一个数组时它应该可以工作。查看参考:

https://laravel.com/api/5.3/Illuminate/Mail/Message.html#method_to

第一个参数可以是一个数组。试试吧。

我觉得应该可以,不过你也可以这样试试:

     Mail::raw('No Body', function($message) use ($emails)
      {    
        foreach ($emails as $email)
         {
           $message->to($email); 
         }   
      });

我在我的应用程序中使用了这样的方法,效果很好!

也许您也可以尝试发送一封电子邮件并验证它是否正常,然后您可以尝试使用电子邮件数组发送电子邮件。

来自 Laravel 5.4 documentation 发送邮件:

The to method accepts an email address, a user instance, or a collection of users. If you pass an object or collection of objects, the mailer will automatically use their email and name properties when setting the email recipients, so make sure these attributes are available on your objects.

Mail::to(['array','of','emails'])...

还有

Mail::cc(['array','of','emails'])...

Mail::bcc(['array','of','emails'])...