Home > Enterprise >  Sending email to multiple recipients using Laravel
Sending email to multiple recipients using Laravel

Time:11-25

I trying to send emails to multiple recipients. I used the looping to send multiple times but it does not seem to work as I get only 1 email sent. I search the internet for guides but not found anything related to this problem.

$mails = User::select('users.name','users.staffid','users.email')->get();

foreach($mails as $mail)
{
    $data[] = [
        'name'     => $mail->name,
        'username' => $mail->staffid,
        'email'    => $mail->email
    ];  
}

foreach($data as $dat)
{
    Mail::send('email.sendReminder', ["data1"=>$dat], function($message) use ($dat) {
        $message->from('[email protected]', 'Test Reminder');
        $message->to($dat['email']);
        $message->subject('Reminder');
    });
}

CodePudding user response:

are you using queue jobs? you must use it otherwise your server may not respond for multiple email. second you are sending multiple emails at the same time may be you should try this.

$mails = User::select('users.name','users.staffid','users.email')->get();

foreach($mails as $dat)
{

Mail::send('email.sendReminder', ["data1"=>$dat], function($message) use ($dat) {
    $message->from('[email protected]', 'Test Reminder');
    $message->to($dat['email']);
    $message->subject('Reminder');
});

}

CodePudding user response:

Just wrap the function into a loop for sending multiple emails. The below code works for me

Mail::send(['html' => 'admin.email-template.lowstock'], array('totalUsers' => $totalUsers, 'item' => $item,'data' =>$data), function ($message) use ($user_to_mail, $item) {
                $message->from('[email protected]', 'POS');
                $message->subject('Notification Mail');
                foreach ($user_to_mail as $mail) {
                    $message->to($mail->email);
                }

            });
  • Related