1

I use Laravel 5.3

My controller like this :

auth()->user()->notify(new ConfirmOrder($invoice));

My notification like this :

<?php
...
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 ConfirmOrderMail($this->data, $notifiable))
            ->to($notifiable->routeNotificationFor('mail'))
            ->bcc($mail_myshop)
            ->subject('Thanks');
    }
}

My mailable like this :

<?php
...
class ConfirmOrderMail extends Mailable
{
    use Queueable, SerializesModels;
    public $data;
    public $user;
    public function __construct($data, $user)
    {
        $this->data = $data;
        $this->user = $user;
    }
    public function build()
    {
        return $this->view('vendor.notifications.mail.email-confirm-order',['data'=>$this->data, 'name' => $this->user->name]);
    }
}

I combine notification and mailable, because laravel 5.3 not support bcc

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 like this :

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=****@gmail.com
MAIL_PASSWORD=****
MAIL_ENCRYPTION=tls

How can I display bcc on the sender email(on the sent mail)?

Update :

I will add details of my problem

If I sending the mail through gmail like this :

enter image description here

Then I check on sent mail menu on gmail from email sender, the result like this :

enter image description here

There looks like bcc email

If I sending the mail from Laravel with my code above, the result :

I check on the sent mail menu from email sender, the result like this :

enter image description here

There looks like no email bcc

What I want is: I want to display bcc email on the sender's email on laravel

How can I do it?

samuel toh
  • 6,836
  • 21
  • 71
  • 108
  • 1
    its called *blind* carbon copy for a reason. – Daniel A. White Oct 20 '17 at 23:52
  • @Daniel A. White, What do you mean? I do not understand – samuel toh Oct 20 '17 at 23:55
  • _blind_ as in you cannot see how is BCC'd. However if I understand you correctly, you're sending your emails **via** your Gmail account, so you're the sender and want to see the BCC'd user on the email in the sent items? – fubar Oct 21 '17 at 05:26
  • @fubar, Yes, as I explain to my question. But when using mail laravel, bcc email is not visible on email sender. Btw, I update my question. I add detail of my problem – samuel toh Oct 21 '17 at 05:30
  • @samueltoh, if you click on "Show Original" to see the full message, are the BCC addresses listed in the email headers? – fubar Oct 21 '17 at 06:03
  • @fubar, If I sending the mail from Laravel, the BCC address no listed – samuel toh Oct 21 '17 at 06:38

1 Answers1

2

This won't be possible.

Laravel uses SwiftMailer for SMTP mail. When you send an email with a list of addresses in the BCC field, SwiftMailer actually breaks up that list and sends each address an individual email.

So, instead of sending one email with the header BCC: test1@gmail.com, test2@gmail.com, test3@gmail.com, SwiftMailer will send three separate emails with the headers BCC: test1@gmail.com, BCC: test2@gmail.com, and BCC: test3@gmail.com, respectively.

Since the email you receive in Gmail does not have a list of addresses in the BCC header, it cannot show you the list.

Now, I said it's not possible, but technically, if you wanted, you could fork the SwiftMailer repository, update the code to not split the BCC recipients into separate emails, and then figure out how to get Laravel to use your customized version of SwiftMailer, but I highly doubt that would be worth the effort.

patricus
  • 59,488
  • 15
  • 143
  • 145
  • I'm curious as to why SwiftMailer would do this when the sending mail server is already capable of handling BCC emails. It seems you've dug into the source to find this. Do you have a link to the relevant code/line? – fubar Oct 21 '17 at 06:08
  • @patricus, If you answer that, it means it can not be implemented. If I use laravel 5.4 or 5.5, it can be done or the same? – samuel toh Oct 21 '17 at 06:46
  • 1
    @fubar I think the general idea is that SwiftMailer cannot guarantee that SMTP servers will strip out BCC headers or that clients will not show BCC headers. If neither modify the headers, than the full list of BCC addresses will be available to every recipient, defeating the purpose. So, in order to conform to how BCC should work, it specifically sends out a separate email per BCC address. – patricus Oct 21 '17 at 07:05
  • 1
    @fubar [Tests for the BCC list splitting are here.](https://github.com/swiftmailer/swiftmailer/blob/8388605460335d4c21481eb3be1e60d2bbda3811/tests/unit/Swift/Transport/AbstractSmtpTest.php#L823-L932) The first test includes an explanation as to why it is split. – patricus Oct 21 '17 at 07:06
  • 1
    @fubar [Sending the mail is done here.](https://github.com/swiftmailer/swiftmailer/blob/8388605460335d4c21481eb3be1e60d2bbda3811/lib/classes/Swift/Transport/AbstractSmtpTransport.php#L184-L196) Line 184 saves off the original BCC addresses. Line 186 empties the BCC on the message. Line 190 calls the function (`sendBcc`) to loop through the original BCC list and send each address separately. Line 196 restores the BCC header on the original message after everything has been sent. – patricus Oct 21 '17 at 07:06
  • @samueltoh This is a "restriction" of SwiftMailer, which is used by all Laravel versions. Moving to 5.4 or 5.5 won't change this functionality. – patricus Oct 21 '17 at 07:08
  • @patricus, Yeah. I tried on laravel 5.4, it's the same. If using laravel mail (https://laravel.com/docs/5.3/mail), whether it is the same? – samuel toh Oct 21 '17 at 07:17
  • @samueltoh As long as you're using SMTP, yes, it'll be the same. – patricus Oct 21 '17 at 07:18
  • @patricus thanks for the explanation. I didn’t know SwiftMailer did that. – fubar Oct 21 '17 at 07:19
  • @patricus, Do I need to replace it? for example using mailgun – samuel toh Oct 21 '17 at 07:22
  • @fubar Neither did I. I'd never looked into or paid attention to BCC implementations before this question. :) – patricus Oct 21 '17 at 07:22
  • @samueltoh I don't know how MailGun, SparkPost, or Amazon SES handle BCC. They may send the headers; they may not. You'll need to test them out to see. – patricus Oct 21 '17 at 07:26
  • @patricus, Okay. Thanks for your explanation. I will try it first – samuel toh Oct 21 '17 at 07:44