0

This is my code which sends an email to a single address:

Route::get('/send-mail', function () {
$details = [
    'title' => 'Mail From KN7',
    'body' => 'Email test in Laravel SMTP'
];
\Mail::to('[email protected]')->send(new \App\Mail\TestMail($details));
echo "Email has been Sent!";
});

Is there any way to change this code so I can send the same email to multiple email addresses?

5
  • 1
    Do you want to send them separately (only 1 address in the To field), or all together (multiple addresses on the To field) Commented Mar 8, 2021 at 19:55
  • Wrap your Mail::to() in a loop of some kind (like looping over Users/email addresses you want to send to), or see if multiple addresses works (like Mail::to(['address1', 'address2'])), or use ->cc() or ->bcc() with the same logic. Commented Mar 8, 2021 at 19:55
  • @TimLewis Multiple addresses in the Mail::to would need to be sent as an array, so just needs the braces. Commented Mar 8, 2021 at 19:57
  • @aynber Cool, that's what I would have expected, but you never know :) Thanks for confirming. Commented Mar 8, 2021 at 19:58
  • @aynber I need to send all together (multiple addresses) Commented Mar 8, 2021 at 20:02

2 Answers 2

1

You can add simple array :

 $usersArray = ['[email protected]', '[email protected]', '[email protected]'];

    foreach($usersArray as $user){

        \Mail::to($user)->send(new \App\Mail\TestMail($details));
        echo "Email has been Sent!";
        });
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Mail::to() also accepts an array so no need for the foreach() loop
1

You can use the array variables for multiple email IDs

Route::get('/send-mail', function () {
$details = [
    'title' => 'Mail From KN7',
    'body' => 'Email test in Laravel SMTP'
];
\Mail::to(['[email protected]','[email protected]','[email protected]'])->cc(['[email protected]','[email protected]'])->send(new \App\Mail\TestMail($details));
echo "Email has been Sent!";
});

Comments

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.