7

I am using Mail function in laravel under the SwiftMailer library.

Mail::send('mail', array('key' => $todos1), function($message) {
        $message->to(array('TEST@example.com','TESsdT@example.com','TESjxfjT@example.com','TESfssdT@example.com'))->subject('Welcome!');
    });

The above function sends mail to several user, but the users know to who are all the mail is sent as its to address comprises of

To: TEST@example.com, TESsdT@example.com, TESjxfjT@example.com, TESfssdT@example.com

So inorder to rectify this I have used a foreach loop which sends the mails seperatly

    foreach($to as $receipt){
        //Mail::queue('mail', array('key' => $todos1), function($message) use ($receipt)
        Mail::send('mail', array('key' => $todos1), function($message) use ($receipt)
        {
            $message->to($receipt)->subject('Welcome!');
        });
    }   

The above code works fine...

My question is that in this advanced framework is there any function that could send mails to the users with unique to address (i.e.) without one user knowing to how-many others the same mail is sent without using a foreach...

Ronser
  • 1,855
  • 6
  • 20
  • 38

3 Answers3

9

You can use bcc (blind carbon copy):

Mail::send('mail', array('key' => $todos1), function($message) {
    $message->to('firstemail@example.com')
    ->bcc(array('TEST@example.com','TESsdT@example.com','TESjxfjT@example.com','TESfssdT@example.com'))
    ->subject('Welcome!');
});
Steve
  • 20,703
  • 5
  • 41
  • 67
  • If I need to use variables associated to each destinatary in the email, I have to use a ```foreach```? – JCarlosR Aug 03 '16 at 05:26
  • 1
    @JCarlos Yes, if each email is unique, then you will need to loop and send each one. The above only works if you are sending the exact same email to everyone – Steve Aug 03 '16 at 09:59
2

You can use CC or BCC to send same html mail to N number of persons:

$content = '<h1>Hi there!</h1><h2 style="color:red">Welcome to stackoverflow..</h2>';
     $bcc = ['*****@gmail.com','******@gmail.com'];
     $sub = "Sample mail";
      Mail::send([], [], function($message) use ($content, $sub, $bcc) {
        $message->from('ur-mail-id@gmail.com','name');
        $message->replyTo('no-reply@gmail.com', $name = 'no-reply');
        $message->to('******@domain.com', 'name')->subject($sub);
        $message->bcc($bcc, $name = null);
        // $message->attach('ch.pdf'); // if u need attachment
        $message->setBody($content, 'text/html');
      });
DimaSan
  • 12,264
  • 11
  • 65
  • 75
Muruga
  • 31
  • 1
1

SwiftMailer works like your normal email client (Outlook, Thunderbird...).

What you are doing is the only 100% correct to do it, but you can still do as Steve suggested, use BCC, but don't use a noreply or other non-important email address in the to, since all recipients will see that email address.

Note: The single function call will not make your code way faster or less resource hungry.

Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58