Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luyznascimento's avatar

Mail Sending Multiple Recipients Using ForEach

Hi,

I have a command that sends every 60 minutes e-mails to a short list with the use of Mailable, I pass some personal issues to content body of every single message. This command works perfectly and the e-mail is sent, but, just to one recipient.

This is the way:

  1. Query Students DB with Eloquent that returns, for example, six results;
  2. I do : Foreach. During the loop, I call : Mail::to($request->email)->send(new pendingStudents($info)); 3 If I print $request->email before the "Mail" - all of them appears. But just one is sent, the first one of the loop.

And there is no error.

Regards,

0 likes
3 replies
luyznascimento's avatar

Hi Mike!

Thank you for your time here. I left below this piece of block code with just two options "whereIn" result with my e-mails for debug purposes.

Here what you asked me for:

$registered = DB::table("students")->select(
"students.name AS student", 
"students.email")
->whereIn('students.id', [1, 8])
->get();

if(count($registered)){
    foreach($registered as $key => $request)
    {
        $to = $request->email;
        $info = array(
            "name"      => $request->student,
            "email"     => $request->email
        );
        Mail::to($to)->send(new pendingStudents($info));
    }
}
luyznascimento's avatar
luyznascimento
OP
Best Answer
Level 1

Finally I have the answer!

Probably...probably relates to my mail server (hostgator). I am now sending e-mails throught AWS SES and everything worked like a charm.

Please or to participate in this conversation.