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

samuel toh
  • 6,836
  • 21
  • 71
  • 108

1 Answers1

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');
}
patricus
  • 59,488
  • 15
  • 143
  • 145
  • 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 – samuel toh Oct 18 '17 at 04:17
  • It works. But on the email, the view looks a bit strange. Look at this : https://ibb.co/evfDD6. On the email, label `to:` not exist. It should contain email from the user – samuel toh Oct 18 '17 at 05:04
  • @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. – patricus Oct 18 '17 at 05:21
  • 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 (https://laravel.com/docs/5.3/mail) to solve my problem. Because it can use bcc – samuel toh Oct 18 '17 at 05:37
  • See my question. I had find a solution – samuel toh Oct 19 '17 at 01:46
  • @samueltoh I have updated my answer. You can still use the notification system with your `Mailable` object, without having to directly call `Mail::` in your controller (assuming you're on at least Laravel 5.3.7). – patricus Oct 19 '17 at 02:50
  • In my composer.json : `"laravel/framework": "5.3.*",`. I can not be certain detailed version of my laravel – samuel toh Oct 19 '17 at 06:21
  • @samueltoh You can run `php artisan --version` to get your full version number. – patricus Oct 19 '17 at 06:23
  • The result : `Laravel Framework version 5.3.31`. Seems it can not – samuel toh Oct 19 '17 at 06:48
  • @samueltoh Sure you can. The functionality was introduced in 5.3.7. It'll work in 5.3.31. – patricus Oct 19 '17 at 13:47
  • I'm sorry. I previously thought 5.3.31 < 5.3.7 :) – samuel toh Oct 19 '17 at 19:45
  • I try like this. If like that, email `to` not display – samuel toh Oct 20 '17 at 11:04
  • @samueltoh Oops. Forgot to call the `to()` method with the notifiable's address. I have updated my answer. – patricus Oct 20 '17 at 20:04
  • It works, but there is one shortcoming. There is no bcc on the email sender. For example, there are 3 emails of bcc. It should appear in the sender's email (If I click sent mail on the gmail and click the detail, Should there exist email bcc. But there is no email bcc). I setting my sender email in env – samuel toh Oct 20 '17 at 22:50
  • If I send a regular email, in sender email, on the sent mail menu will look like this : https://ibb.co/di7Ffm. If I use your way, bcc email is not exist in the sender email. Should look like image above – samuel toh Oct 20 '17 at 23:05
  • @samueltoh Are you saying there's a difference between sending the mail through the notification vs sending it directly using the `Mail` facade, or are you saying there's a difference between sending the mail from Laravel vs sending the mail through gmail? – patricus Oct 21 '17 at 04:33
  • what I mean is there's a difference between sending the mail from Laravel vs sending the mail through gmail – samuel toh Oct 21 '17 at 05:02
  • @samueltoh Gotcha. That should be asked as a completely separate question. Someone may be able to help you with that. – patricus Oct 21 '17 at 05:04
  • I had make the question. Look at this : https://stackoverflow.com/questions/46858593/why-is-there-no-bcc-in-senders-email-if-combine-laravel-notification-laravel. But no one can answer – samuel toh Oct 21 '17 at 05:06
  • I add detail of my problem on my new question. I just want to make sure whether it can be done or not – samuel toh Oct 21 '17 at 05:25
  • @samueltoh I went ahead and answered that other question. However, I believe this question is done and answered. – patricus Oct 21 '17 at 05:46