如何迭代Mail :: send Laravel函数的消息?

时间:2019-05-23 14:19:56

标签: php laravel email

我正在尝试使用Laravel Mail :: send函数迭代并将消息发送到一系列电子邮件中的邮件

我搜索了相同的问题,并在https://stackoverflow.com/a/39625789的Radmation处找到了以下引用的代码。

$emails = ['tester@blahdomain.com', 'anotheremail@blahdomian.com'];
    Mail::send('emails.lead', ['name' => $name, 'email' => $email, 
     'phone' => $phone], function ($message) use ($request, $emails)
    {
        $message->from('no-reply@yourdomain.com', 'Joe Smoe');
        //$message->to( $request->input('email') );
        $message->to( $emails);
        //Add a subject
        $message->subject("New Email From Your site");
    });

我想知道迭代使用的第二个参数,因此我可以向每封电子邮件发送带有其名称的动态消息。

2 个答案:

答案 0 :(得分:0)

您可以将电子邮件放入关联数组,例如:

$emails = [
  'tester@blahdomain.com' => 'tester', 
  'anotheremail@blahdomian.com' => 'anotheremail'
];

然后遍历key=>value对,例如:

foreach($emails as $email=>$name){
  Mail::send('emails.lead', ['name' => $name, 'email' => $email], function ($message) use ($email, $name){
    $message->from('no-reply@yourdomain.com', 'Joe Smoe');
    $message->to($email, $name);
    $message->subject("New Email From Your site");
  });
}

如果您想一次将同一封邮件发送给多个收件人,则还可以将email=>name对数组传递给to方法:

$message->to($emails)

但是我认为用这种方法无法个性化地定制电子邮件内容。同样,在这种情况下,每个收件人都可以看到所有电子邮件地址。

答案 1 :(得分:0)

$emails = ['tester@blahdomain.com', 'anotheremail@blahdomian.com'];
foreach($emails as $currentRecipient){
  $customtMsg = //create here a custom msg
  Mail::send(['text' => 'view'], $customtMsg, function ($message) use ($request, $currentRecipient)
    {
        $message->from('no-reply@yourdomain.com', 'Joe Smoe');
        $message->to($currentRecipient);
        //Add a subject
        $message->subject("New Email From Your site");
    });
}

请检查用法here