dongli1920 2019-05-23 14:19
浏览 182

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

I am trying to iterate and send message to mail in array of emails using Laravel Mail::send function

I searched for same problem and found the code below reference from Radmation here https://stackoverflow.com/a/39625789.

$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");
    });

I am wondering the second paramater for iteration usage, so i can message each email with dynamic message of their name.

  • 写回答

2条回答 默认 最新

  • duanmu2941 2019-05-23 14:32
    关注

    You could put emails in associative array, for example:

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

    And then iterate over key=>value pairs, like:

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

    If you want to send same mail to multiple recipients at once, you could also pass an array of email=>name pairs to the to method:

    $message->to($emails)

    But I don't think it is possible to customize an email content individualy with that approach. Also in that case, all of the email addresses are visible to every recipient.

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致