Syed Ameer Hamza
Syed Ameer Hamza

Reputation: 63

Laravel send email using mail::to without viewing other recipients

I have an array of recipients $this->recipients and I want to send an email to all recipients without showing each other emails. Currently, it shows all the recipients in an email. enter image description here

 if (count($this->recipients) > 1) {
                Mail::bcc($this->recipients)
                    ->send(new EmailNotificationMailable($this->notificationRequest));
            } else {
                Mail::to($this->recipients)
                    ->send(new EmailNotificationMailable($this->notificationRequest));
            }

I tried this code but when I send with Mail::bcc the To of email is empty. Please give the working solution for this. I don't want to loop recipients array

Upvotes: 0

Views: 560

Answers (2)

Bharat Rawat
Bharat Rawat

Reputation: 42

You need to loop through the recipients collection:

if(count($this->recipients) > 1)
{
 $this->recipients->each(function($recipient)
 {
    Mail::to(recipient)->bcc($this->recipients)->send(new EmailNotificationMailable($this->notificationRequest));
 }
}else{
    Mail::to($this->recipients)->send(new EmailNotificationMailable($this->notificationRequest));
}

Upvotes: 1

Pusparaj
Pusparaj

Reputation: 1639

Use something like this:

Mail::to(array_pop($this->recipients))->bcc($this->recipients)

This will set the last entry in the recipients array as the mail receiver and every other addresses will be included via BCC.

Upvotes: 0

Related Questions