Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel sending separate, multiple mail without using foreach loop

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

Mail::send('mail', array('key' => $todos1), function($message) {
        $message->to(array('[email protected]','[email protected]','[email protected]','[email protected]'))->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: [email protected], [email protected], [email protected], [email protected]

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

like image 997
Ronser Avatar asked Nov 11 '14 11:11

Ronser


2 Answers

You can use bcc (blind carbon copy):

Mail::send('mail', array('key' => $todos1), function($message) {
    $message->to('[email protected]')
    ->bcc(array('[email protected]','[email protected]','[email protected]','[email protected]'))
    ->subject('Welcome!');
});
like image 58
Steve Avatar answered Oct 03 '22 18:10

Steve


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('[email protected]','name');
        $message->replyTo('[email protected]', $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');
      });
like image 32
Muruga Avatar answered Oct 03 '22 18:10

Muruga