Dmitry Malys
Dmitry Malys

Reputation: 1323

Laravel sending emails to multiple users takes too long

I'm looking for a solution how can I optimize my code to send email to all users in my app?

    $emails = [
        '[email protected]',
        '[email protected]',
        '[email protected]',
        ...
    ];

    $data = [
        'subject' => 'Items List',
        'mailFrom' => '[email protected]',
        'items' => $items
    ];

    foreach ($emails as $email) {

        Mail::to($email)->queue(new Mailable($data));

    }
    return back();

Is there are any solution to optimize it? In the live app I have more than 100 users.

Upvotes: 1

Views: 1121

Answers (1)

Rings
Rings

Reputation: 421

If I am not mistaken, you can use the Mail::bcc($emails)->queue(new Mailable($data));. That way you can just send 1 mail, with all the emails in BCC.

However if every mail is different/customised to the users, you might want to check how to dispatch the email event from a job (https://laravel.com/docs/5.7/queues#creating-jobs).

Upvotes: 4

Related Questions