Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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("[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) "[email protected]"   [1]=>   string(18) "[email protected]" } 

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.

like image 591
haakym Avatar asked Oct 27 '14 09:10

haakym


People also ask

How do I send an email to multiple addresses in Bcc?

If you're sending an email to multiple recipients who don't need to know each other's email address, use Blind Carbon Copy (Bcc) instead of Carbon Copy (Cc). Click Bcc / Show Bcc - A Bcc field will appear in each new message. Enter addresses into the Bcc field to avoid other recipients seeing them.

How do I send an email to Mul?

The BCC (Blind Carbon Copy) method is the most common approach to send emails to multiple recipients at the same time. Emailing to multiple recipients using the BCC feature hides other recipients from the recipient, making it look like he is the sole recipient of the email.

How do I send to multiple email addresses in smtp?

In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses. The sendmail() parameter to_addrs however should be a list of email addresses.


1 Answers

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

like image 176
Marcin Nabiałek Avatar answered Sep 16 '22 11:09

Marcin Nabiałek