Laravel sends single, multiple mail without using a foreach loop

I am using the Mail function in laravel in 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 users, but users know who all mail is sent to, because its address consists of

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

So, to fix this, I used the foreach , which sends the email messages separately

  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 is working fine ...

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

+6
source share
3 answers

You can use bcc (blind 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!'); }); 
+9
source

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

 $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'); }); 
+2
source

SwiftMailer works like a regular email client (Outlook, Thunderbird ...).

What you do is only 100% correct to do this, but you can still do, as Steve suggested, use BCC, but do not use obscene or other irrelevant email address in, as all recipients will see this address Email.

Note : calling one function will not make your code faster or less resource intensive.

+1
source

Source: https://habr.com/ru/post/978026/


All Articles