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

PhpEmailLaravelLaravel 4

Php Problem Overview


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("[email protected]", "[email protected]");
$input = Input::all();

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

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

and passing an array:

// for example
$emails = array("[email protected]", "[email protected]");
$input = Input::all();

Mail::send('emails.admin-company', array('body' => Input::get('email_body')), 
	function($message) use ($emails, $input) {
        $message
    	->from('[email protected]', '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.

Php Solutions


Solution 1 - Php

I've tested it using the following code:

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

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) .

Solution 2 - Php

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.

Solution 3 - Php

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(',', '[email protected],[email protected]');
    $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));

Solution 4 - Php

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' => '[email protected]', 'name' => 'User One'], 
       ['email' => '[email protected]', '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.

Solution 5 - Php

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('[email protected]')
    ->cc(['[email protected]','[email protected]','[email protected]','[email protected]'])
    ->send(new document());

where document is any class that further customizes your email.

Solution 6 - Php

You can loop over recipientce like:

foreach (['[email protected]', '[email protected]'] as $recipient) {
    Mail::to($recipient)->send(new OrderShipped($order));
}

See documentation here

Solution 7 - Php

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('[email protected]', 'Some Company Name')
						->replyTo($emailReply, $nameReply)
						->bcc($contactList, 'Contact List')
						->subject("Subject title");
			});

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

Solution 8 - Php

Try this:

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

Solution 9 - Php

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');
}

Solution 10 - Php

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

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

Solution 11 - Php

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');
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhaakymView Question on Stackoverflow
Solution 1 - PhpMarcin NabiałekView Answer on Stackoverflow
Solution 2 - PhpAbhishekView Answer on Stackoverflow
Solution 3 - PhpToskanView Answer on Stackoverflow
Solution 4 - Phpplus5voltView Answer on Stackoverflow
Solution 5 - PhpDunsin OlubobokunView Answer on Stackoverflow
Solution 6 - PhpAdam PeryView Answer on Stackoverflow
Solution 7 - PhpAlexandre RibeiroView Answer on Stackoverflow
Solution 8 - PhpFaridul KhanView Answer on Stackoverflow
Solution 9 - PhphackernewbieView Answer on Stackoverflow
Solution 10 - PhpRadmationView Answer on Stackoverflow
Solution 11 - PhpRizwan MughalView Answer on Stackoverflow