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

Yorkata's avatar

Sending mail to multiple recipients.

Hi,

I want to send mail to more than one user at the same time. Using the recipient - row in my database that holds the emails. The "mail_to" rows hold values as such: test1@gmail.com,test2@gmail.com. And since I am using Mailtrap for local development I am not sure that two emails are actually sent(In Mailtrap I am receiving one mail in my project inbox with: To: <test1@gmail.com, test2@gmail.com> ). Any ideas if my code works before deploying it in prod?

EDIT: I am not sure if thats how Mailtrap works or if the 'Mail' class does not split the string into individual recipients.

Thanks

$mailTo = CCV::get('mail_to'); 
    $recipients = explode(',', $mailTo);

    $mailData = [
        "from" => CCV::get('mail_from'),
        "to" => $recipients,
        "subject" => $subject,
        "message" => "I like bananas",
    ];
0 likes
4 replies
tisuchi's avatar

@yorkata Your code is incomplete.

I assume it supposes to be like this.

 $mailData = [
        "from" => CCV::get('mail_from'),
        "to" => $recipients,
        "subject" => $subject,
        "message" => "I like bananas",
    ];

Mail::send('email.template', $mailData,...
Yorkata's avatar
Yorkata
OP
Best Answer
Level 1

I got it to work after looping the $recipients

foreach ($recipients as $recipient) {
            $mailData["to"] = trim($recipient); // Trim any extra whitespace
        
            Mail::send(new MonitoringEmail($mailData));
        }

Please or to participate in this conversation.