如何将数组值一一传入邮件class

How to pass array values into the mail class one by one

我想将数组的值传递到邮件 class。我有这个代码可以用来接收电子邮件。

从数据库获取电子邮件

$vendorsId = OrdersProduct::select('vendor_id')->where('order_id', $order_id)->pluck('vendor_id');

$vendorEmails = Admin::select('email')->whereIn('id', $vendorsId)->get()->toArray();

结果

array:2 [▼
  0 => array:1 [▼
    "email" => "mxxxxx@gmail.com"
  ]
  1 => array:1 [▼
    "email" => "myyyyy@gmail.com"
  ]
]

所以我想把这些邮箱地址传到邮件class中去一个一个发邮件,下面是邮件代码

正在发送电子邮件

$messageData = [

                'order_id' => $order_id,                  
            ];

Mail::send('emails.order', $messageData, function ($message) use ($email) {
                $message->to($email)->subject('Order Placed');
            });

谢谢!

为什么不使用 foreach?

            $email = Auth::user()->email;

            $messageData = [
                'email' => $email,
                'name' => Auth::user()->name,
                'order_id' => $order_id,
                'orderDetails' => $orderDetails,
            ];
    

           foreach ($vendorEmails as $vemail) {
               Mail::send('emails.order', $messageData, function ($message) use ($email, $vemail) 

           {
                    $message->to($vemail)->subject('Order Placed - stylooworld.info');
                });
           }

你可以这样试试;

$emailList = Admin::->whereIn('id', $vendorsId)->pluck('email')->toArray();

Mail::send('emails.order', $messageData, function ($message) use ($emailList) {
                $message->to($emailList)->subject('Order Placed');
            });