1

I need to send bulk emails using Laravel queue and jobs. If I understood, my method this way should dispatch 1 job where all the emails are fetched and send it one by one going through the foreach loop, right? Somehow, only one email got send. And when I check the message, it appears the recipient message is in this format - "[email protected]" <[email protected]>. Only test1 email account received the email. I am not sure what causing it. Thank you for your help.

Controller

$body = $request->body;
        $titleName = $request->subject;
        
        $job = (new \App\Jobs\SendQueueEmail($body, $titleName))
                ->delay(now()->addSeconds(2)); 

        dispatch($job);

Job

public function handle(Request $request)
    {

        $emailsAlumni = ['[email protected]', '[email protected]'];
        $date = Carbon::now()->format('d M Y');

        $data = [
            "body" => $this->body,
            "date" => $date
        ];

        foreach ($emailsAlumni as $email) {
            Mail::send('main.admin.email.general', $data, function ($message) use ($email) {
                $message->to($email);
                $message->subject('title');
            });
        }

    }
0

1 Answer 1

1

You don't need to loop whole Mail instance you can just try it as

Mail::send('main.admin.email.general', $data, function ($message) use ($emailsAlumni) {
            $message->to($emailsAlumni);
            $message->subject('title');
        });
Sign up to request clarification or add additional context in comments.

2 Comments

Note that this will send 1 email to all recipients, with everybody's email address visible in the 'To:' field.
You can use ->bcc() of course for the blind carbon copies

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.