Search code examples
laravelemaillaravel-5notificationslaravel-5.3

How can I solve Call to undefined method Illuminate\Notifications\Messages\MailMessage::bcc() on laravel notification?


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


Solution

  • 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');
    }