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

samuel toh

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

patricus

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I solve "Cannot call method "addL" of undefined"

From Dev

How to solve Call to undefined method Illuminate\Database\Query\Builder::notify()? (laravel 5.3)

From Dev

How can I bcc with mailx?

From Dev

How can i solve this Undefined index:?

From Dev

How can I solve Uncaught TypeError: Cannot read property 'call' of undefined?

From Dev

How to solve Uncaught Error: Call to undefined method stdClass::query()

From Dev

How can I solve the following error: NameError: undefined local variable or method `email' for #<AuthenticateUser>

From Dev

Can not call method of undefined

From Dev

How to solve laravel notification with broadcast ? Laravel 5.3

From Dev

How can I identify bcc sender in Outlook?

From Dev

Laravel 5 - Call to undefined method

From Dev

Laravel - Call to undefined method in repository

From Dev

Call to undefined method (laravel 5.2)

From Dev

Call to undefined method error in Laravel

From Dev

How can I solve undefined reference from a shared library?

From Dev

Java: How can i solve the Error: "implicit super constructor is undefined"

From Dev

How can I solve "Cannot read property 'define' of undefined"?

From Dev

How can I solve undefined reference from a shared library?

From Dev

How can I debounce a method call?

From Dev

How can I call Timer as a method?

From Dev

How can I call a method from ViewController?

From Dev

How can I call a method in Squeak?

From Dev

How can I "intercept" a method call for a test?

From Dev

How can I call a method from ViewController?

From Dev

How can I call the displayAd() method?

From Dev

How Can I Pass an Undefined Method Like an Undefined Function

From Dev

How can I trigger my notification from a method?

From Dev

How can I send a notification in Laravel to a user that is not currently logged in?

From Dev

How to store an object together with a notification so that I can call it when receiving the notification?

Related Related

  1. 1

    How can I solve "Cannot call method "addL" of undefined"

  2. 2

    How to solve Call to undefined method Illuminate\Database\Query\Builder::notify()? (laravel 5.3)

  3. 3

    How can I bcc with mailx?

  4. 4

    How can i solve this Undefined index:?

  5. 5

    How can I solve Uncaught TypeError: Cannot read property 'call' of undefined?

  6. 6

    How to solve Uncaught Error: Call to undefined method stdClass::query()

  7. 7

    How can I solve the following error: NameError: undefined local variable or method `email' for #<AuthenticateUser>

  8. 8

    Can not call method of undefined

  9. 9

    How to solve laravel notification with broadcast ? Laravel 5.3

  10. 10

    How can I identify bcc sender in Outlook?

  11. 11

    Laravel 5 - Call to undefined method

  12. 12

    Laravel - Call to undefined method in repository

  13. 13

    Call to undefined method (laravel 5.2)

  14. 14

    Call to undefined method error in Laravel

  15. 15

    How can I solve undefined reference from a shared library?

  16. 16

    Java: How can i solve the Error: "implicit super constructor is undefined"

  17. 17

    How can I solve "Cannot read property 'define' of undefined"?

  18. 18

    How can I solve undefined reference from a shared library?

  19. 19

    How can I debounce a method call?

  20. 20

    How can I call Timer as a method?

  21. 21

    How can I call a method from ViewController?

  22. 22

    How can I call a method in Squeak?

  23. 23

    How can I "intercept" a method call for a test?

  24. 24

    How can I call a method from ViewController?

  25. 25

    How can I call the displayAd() method?

  26. 26

    How Can I Pass an Undefined Method Like an Undefined Function

  27. 27

    How can I trigger my notification from a method?

  28. 28

    How can I send a notification in Laravel to a user that is not currently logged in?

  29. 29

    How to store an object together with a notification so that I can call it when receiving the notification?

HotTag

Archive