Laravel 5.4可以在一起添加电子邮件地址

时间:2017-02-16 16:03:08

标签: laravel laravel-5.4

考虑这个代码示例,它采用一组地址,并使用mailables功能向每个地址发送一封电子邮件。

//In my controller

$email = new EmailToWinners($sender_name, $letter);
foreach ($recipients as $recipient){
        Mail::to($recipient)->send($email);
}

//In my App\Mail\EmailToWinner

public function build()
{
    return $this->view('emails.winner-email');
}

这些电子邮件都被发送好了但是当我测试这个时我注意到了,它正在堆叠电子邮件。

所以我在日志中得到了这个。

[2017-02-16 15:58:59] local.DEBUG: Message-ID: <250443348fee18f568f4f263153d5101@testing.dev>
Date: Thu, 16 Feb 2017 15:58:59 +0000
Subject: Email To Winner
From: Example Dev <example@me.dev>
To: 1@example.com
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

[2017-02-16 15:58:59] local.DEBUG: Message-ID: <1b5f39ef0cc17d4d8573019f3d5ec808@testing.dev>
Date: Thu, 16 Feb 2017 15:58:59 +0000
Subject: Email To Winner
From: Example Dev <example@me.dev>
To: 1@example.com, 2@example.com
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

[2017-02-16 15:58:59] local.DEBUG: Message-ID: <ddf6c62a7f881f9381693435b48ef5a3@testing.dev>
Date: Thu, 16 Feb 2017 15:58:59 +0000
Subject: Email To Winner
From: Example Dev <example@me.dev>
To: 1@example.com, 2@example.com, 3@example.com
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

如果你查看电子邮件。第一封电子邮件显示给 1@example.com

第二个显示为 1 @ example.com,2 @ example.com

第三个显示1 @ example.com,2 @ example.com,3 @ example.com

所以第一个人收到3封电子邮件。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您可以向用户集合传递to方法:

Mail::to($recipients)->send(new EmailToWinners());

来自Lavavel Mail Docs

  

to方法接受电子邮件地址,用户实例或用户集合。如果您传递对象或对象集合,邮件程序将在设置电子邮件收件人时自动使用其电子邮件和名称属性,因此请确保这些属性在您的对象上可用

答案 1 :(得分:1)

我认为您需要为每个收件人制作一个新的电子邮件对象。

foreach ($recipients as $recipient){
        Mail::to('$recipient')->send(new EmailToWinners($sender_name, $letter));
}