Reputation: 2482
below is my code to send a mail to multiple users.
$email_id = User::select('email_id')->get()->pluck('email_id');
Mail::send('mail', [], function($message) use ($email_id)
{
$message->to($email_id)->subject('Welcome!!!');
});
I m getting the values in $email_id
as
["[email protected]","[email protected]","[email protected]"]
With this I get error of
Illegal Offset Type
.
But when I write explicitly as
$email_id = ["[email protected]","[email protected]","[email protected]"];
then I am able to send mail to multiple users.
Why is it not working for
$email_id= User::select('email_id')->get()->pluck('email_id');
and is working fine for
$email_id = ["[email protected]","[email protected]","[email protected]"];
Any help would be grateful.
Upvotes: 2
Views: 1018
Reputation: 111
Simply append
->toArray()
function to the code.
$email_id= User::select('email_id')->get()->pluck('email_id')->toArray();
Note: sending mails this way may create bottleneck on the server and eventually force all mails to be delivered to spam/junk folder (if it ever gets delivered). To avoid this, write a function that will queue all mails. Refer to https://laravel.com/docs/5.1/mail#queueing-mail for better clarification.
Upvotes: 0
Reputation: 2482
If we want to send only one email at a time. then we can use this code
$email_id = User::select('email_id')->get()->pluck('email_id');
Mail::send('test', array('user' => $email_id) , function ($message) {
$message->from('[email protected]'), 'From Example Name');
$message->to('[email protected]')->subject('Welcome!!!');
})
If we want to send an email to multiple users , then we can use this code
$email_id = User::select('email')->get()->pluck('email')->toArray();
Mail::send('test', array('user' => $email_id) , function ($message) use
($email_id) { $message->from('[email protected]'), 'From Example Name');
$message->to($email_id)->subject('Welcome!!!');
});
Upvotes: 0