使用laravel mailable发送多个电子邮件

时间:2019-12-25 13:08:29

标签: laravel laravel-5

我正在使用laravel 5.6,并尝试将电子邮件发送到多个电子邮件地址。在这里,我的代码仅适用于一封电子邮件。如何使用多封电子邮件?  我正在使用可邮寄

这是我的控制人

public function sendEmail(Request $request)
{
    Mail::send(new sendMail());
    Session::flash('conf_email', 'ok');

    return redirect('/send-email');
}
class SendMail extends Mailable
{
    public function build(Request $request)
    {
        return $this->view('mail', ['msg' => $request->message])->to($request->to);
    }
}

2 个答案:

答案 0 :(得分:0)

to()函数接受多个用户(电子邮件(字符串)或对象的数组) 请参阅以下链接以获取更多信息 https://laravel.com/api/5.5/Illuminate/Contracts/Mail/Mailer.html#method_to

所以你可以做这样的事情

class SendMail extends Mailable
{
   public function build(Request $request)
   {
     $users=User::all();
     return $this->view('mail', ['msg' => $request->message])->to($users);
     //or 
      return $this->view('mail', ['msg' => $request->message])->to(['test@test.com','test1@test.com']);
    }
}

答案 1 :(得分:0)

You just need to change sendEmail function to:

public function sendEmail(Request $request)
{
    Mail::to(['test@test.com','test1@test.com'])->send(new sendMail()); 
    Session::flash('conf_email', 'ok');

    return redirect('/send-email');
}