0

I use Laravel 5.3

I try cc, it works

But I try bcc, there exist error like this :

Call to undefined method Illuminate\Notifications\Messages\MailMessage::bcc() 

My code like this :

<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; class ConfirmOrder extends Notification implements ShouldQueue, ShouldBroadcast { use Queueable; private $data; public function __construct($data) { $this->data = $data; } public function via($notifiable) { return ['mail']; } public function toMail($notifiable) { $mail_myshop = explode(',',config('app.mail_myshop')); return (new MailMessage) ->bcc($mail_myshop) ->subject('Thanks') ->greeting('Hi '.$notifiable->name.',') ->line('....') ->line('...'); } } 

Seems Laravel 5.3 not support bcc

How can I solve the error?

Update

I had find a solution

In my controller like this :

Mail::to(auth()->user())->send(new ConfirmOrder($data, auth()->user())); 

In my mail like this :

<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class ConfirmPaymentMail extends Mailable { use Queueable, SerializesModels; public $data; public $user; public function __construct($data, $user) { $this->data = $data; $this->user = $user; } public function build() { $mail_myshop = explode(',',config('app.mail_myshop')); return $this->view('vendor.notifications.mail.email-confirm-order',['data'=>$this->data, 'name' => $this->user->name]) ->bcc($mail_myshop) ->subject('Test'); } } 

It works

1 Answer 1

2

As you've seen, the MailMessage object does not have a bcc() method in Laravel 5.3. It was added in 5.4.

However, in Laravel 5.3, when you send a MailMessage to an array of recipients, it automatically uses bcc behind the scenes. So, you can create an array with the original to address plus the list of people to bcc, and then use the to() method, and everyone will be bcc'd. This will, however, leave the "to" address empty.

// get the bcc list $mail_myshop = explode(',',config('app.mail_myshop')); // add the original recipient array_unshift($mail_myshop, $notifiable->routeNotificationFor('mail')); return (new MailMessage) ->to($mail_myshop) // if $mail_myshop is an array, will auto convert to bcc in 5.3 ->... 

It would be wise to make a note here, however, that if you do upgrade your application to Laravel 5.4+, this is no longer the case, and all your recipients will be added to the to() field.

And, one final solution, would be to create a Mailable object. This Mailable object can be returned from the toMail() method instead of a MailMessage. The Mailable object supports bcc(), and When you return a Mailable object from the toMail() function, it simply calls the send() method.

Working from the code provided in your update, your toMail() method would look like:

public function toMail($notifiable) { $data = /*whatever data is*/; $mail_myshop = explode(',', config('app.mail_myshop')); return (new ConfirmPaymentMail($data, $notifiable)) ->to($notifiable->routeNotificationFor('mail')) ->bcc($mail_myshop) ->subject('Thanks'); } 
Sign up to request clarification or add additional context in comments.

21 Comments

I call the notification from controlller like this : auth()->user()->notify(new ConfirmOrder($data));. The email of user not sent. It just sends emails inside ->to(...) only
It works. But on the email, the view looks a bit strange. Look at this : ibb.co/evfDD6. On the email, label to: not exist. It should contain email from the user
@samueltoh That is correct. On the back end, it doesn't set the to address at all, it sends to everyone using the bcc address. This is the easiest solution to work with. If this is unacceptable, you will need to extend and replace Laravel's MailChannel to do what you need.
I want to receive your answer, but the view looks a bit strange. If you have a solution to fix the view, then you can update your answer. Now I try mail class (laravel.com/docs/5.3/mail) to solve my problem. Because it can use bcc
See my question. I had find a solution
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.