Laravel Mail::send() sending to multiple to or bcc addresses

I can't seem to successfully send to multiple addresses when using Laravel's Mail::send() callback, the code does however work when I only specify one recipient.

I've tried chaining:

// for example
$emails = array("myemail1@email.com", "myemail2@email.com");
$input = Input::all();


Mail::send('emails.admin-company', array('body' => Input::get('email_body')),
function($message) use ($emails, $input) {
$message
->from('admin@admin.org', 'Administrator')
->subject('Admin Subject');


foreach ($emails as $email) {
$message->to($email);
}
});

and passing an array:

// for example
$emails = array("myemail1@email.com", "myemail2@email.com");
$input = Input::all();


Mail::send('emails.admin-company', array('body' => Input::get('email_body')),
function($message) use ($emails, $input) {
$message
->from('admin@admin.org', 'Administrator')
->subject('Admin Subject');


$message->to($emails);
});

but neither seem to work and I get failure messages when returning Mail::failures(), a var_dump() of Mail::failures() shows the email addresses that I tried to send to, for example:

array(2) {
[0]=>
string(18) "myemail1@email.com"
[1]=>
string(18) "myemail2@email.com"
}

Clearly doing something wrong, would appreciate any help as I'm not understanding the API either: http://laravel.com/api/4.2/Illuminate/Mail/Message.html#method_to

I realise I could put the Mail::send() method in a for/foreach loop and Mail::send() for each email address, but this doesn't appear to me to be the optimal solution, I was hoping I would also be able to ->bcc() to all addresses once everything was working so the recipients wouldn't see who else the mail is being sent to.

198205 次浏览

I've tested it using the following code:

$emails = ['myoneemail@esomething.com', 'myother@esomething.com','myother2@esomething.com'];


Mail::send('emails.welcome', [], function($message) use ($emails)
{
$message->to($emails)->subject('This is test e-mail');
});
var_dump( Mail:: failures());
exit;

Result - empty array for failures.

But of course you need to configure your app/config/mail.php properly. So first make sure you can send e-mail just to one user and then test your code with many users.

Moreover using this simple code none of my e-mails were delivered to free mail accounts, I got only emails to inboxes that I have on my paid hosting accounts, so probably they were caught by some filters (it's maybe simple topic/content issue but I mentioned it just in case you haven't received some of e-mails) .

it works for me fine, if you a have string, then simply explode it first.

$emails = array();

Mail::send('emails.maintenance',$mail_params, function($message) use ($emails) {
foreach ($emails as $email) {
$message->to($email);
}


$message->subject('My Email');
});

If you want to send emails simultaneously to all the admins, you can do something like this:

In your .env file add all the emails as comma separated values:

ADMIN_EMAILS=admin1@site.com,admin2@site.com,admin3@site.com

so when you going to send the email just do this (yes! the 'to' method of message builder instance accepts an array):

So,

$to = explode(',', env('ADMIN_EMAILS'));

and...

$message->to($to);

will now send the mail to all the admins.

This works great - i have access to the request object and the email array

        $emails = ['tester@blahdomain.com', 'anotheremail@blahdomian.com'];
Mail::send('emails.lead', ['name' => $name, 'email' => $email, 'phone' => $phone], function ($message) use ($request, $emails)
{
$message->from('no-reply@yourdomain.com', 'Joe Smoe');
//            $message->to( $request->input('email') );
$message->to( $emails);
//Add a subject
$message->subject("New Email From Your site");
});

the accepted answer does not work any longer with laravel 5.3 because mailable tries to access ->email and results in

ErrorException in Mailable.php line 376: Trying to get property of non-object

a working code for laravel 5.3 is this:

$users_temp = explode(',', 'first@example.com,second@example.com');
$users = [];
foreach($users_temp as $key => $ut){
$ua = [];
$ua['email'] = $ut;
$ua['name'] = 'test';
$users[$key] = (object)$ua;
}
Mail::to($users)->send(new OrderAdminSendInvoice($o));

With Laravel 5.6, if you want pass multiple emails with names, you need to pass array of associative arrays. Example pushing multiple recipients into the $to array:

$to[] = array('email' => $email, 'name' => $name);

Fixed two recipients:

$to = [['email' => 'user.one@example.com', 'name' => 'User One'],
['email' => 'user.two@example.com', 'name' => 'User Two']];

The 'name' key is not mandatory. You can set it to 'name' => NULL or do not add to the associative array, then only 'email' will be used.

In a scenario where you intend to push a single email to different recipients at one instance (i.e CC multiple email addresses), the solution below works fine with Laravel 5.4 and above.

Mail::to('dunsin.olubobokun@domain.com')
->cc(['paul.yomi@domain.com','stack.overflow@domain.com','abc.xyz@domain.com','nigeria@domain.com'])
->send(new document());

where document is any class that further customizes your email.

I am using Laravel 5.6 and the Notifications Facade.

If I set a variable with comma separating the e-mails and try to send it, I get the error: "Address in mail given does not comply with RFC 2822, 3.6.2"

So, to solve the problem, I got the solution idea from @Toskan, coding the following.

        // Get data from Database
$contacts = Contacts::select('email')
->get();


// Create an array element
$contactList = [];
$i=0;


// Fill the array element
foreach($contacts as $contact){
$contactList[$i] = $contact->email;
$i++;
}


.
.
.


\Mail::send('emails.template', ['templateTitle'=>$templateTitle, 'templateMessage'=>$templateMessage, 'templateSalutation'=>$templateSalutation, 'templateCopyright'=>$templateCopyright], function($message) use($emailReply, $nameReply, $contactList) {
$message->from('myemail@somecompany.com', 'Some Company Name')
->replyTo($emailReply, $nameReply)
->bcc($contactList, 'Contact List')
->subject("Subject title");
});

It worked for me to send to one or many recipients. 😉

Try this:

$toemail = explode(',', str_replace(' ', '', $request->toemail));

You can loop over recipientce like:

foreach (['taylor@example.com', 'dries@example.com'] as $recipient) {
Mail::to($recipient)->send(new OrderShipped($order));
}

See documentation here

This is what I am doing in one of my multi-vendor e-commerce projects.

$vendorEmails[0]    =    'myemail@gmail.com';
foreach ($this->order->products as $orderProduct){
$vendorEmails[$count] = \App\User::find($orderProduct->user_id)->email;
$count++;
}


return $this->from('from_email_@gmail.com', 'Made In India')
->to($this->order->billing_email, $this->order->billing_first_name . ' ' . $this->order->billing_last_name)
->bcc('bcc@email.com')
->cc($vendorEmails)
->subject('Order Placed Successfully - Made In India - ' . $this->order->generated_order_id)
->markdown('emails.orderplaced');
}

This options works for me when sending dynamic email as a variable

$emails = ['yenemmanuel@gmail.com', $email];                                                    Mail::send('emails.patchmail', [], function($message) use ($emails)
{
$message->to($emails)->subject('PATCH BIKER REG')->from('customer-service@patch.com.ng', 'Patch Global'); });

Also includes the senders email