简体   繁体   中英

How to send cc and bcc recipients in Laravel 7?

Im trying to send email with cc and/or bcc and only to recipient is getting the email.

I am dispatching job like this:

$user = User::where('email', 'test1@test.com')->first();
$sender = 'test@test.com';
$cc = User::where('email', 'test2@test.com')->first();
$bcc = User::where('email', 'test3@test.com')->first();
$subject = "Test subject";
$text = "Text mail body";
dispatch(new SendEmailJob($user,$sender,$cc,$bcc,$subject, $text))->delay(now()->addSeconds(10));

Then in constructor in SendEmailJob I accept those arguments and send email:

public function handle()
{
    Mail::to($this->user)->cc($this->cc)->bcc($this->bcc)->send(new TestMail($this->sender, $this->subject, $this->text));
}

And the actual TestMail:

public function build()
{
    return $this
        ->from($this->sender)
        ->subject($this->subject)
        ->markdown('emails.test');
}

The markdown:

@component('mail::message')
{!! nl2br($text) !!}
@endcomponent

Both SendEmailJob and TestMail are implementing ShouldQueue and have php artisan queue:work running. The $user gets the email alright, but $cc and $bcc don't. Tried both on Mailtrap and production. One job is queued and precessed and one email is queued and processed.

Anybody got any idea why cc and bcc emails are not sent?

So I figured it out.

First, on localhost the php artisan queue:work NEEDS to be restarted, otherwise it still works with whatever code was there when it started.

On production server the supervisor or other service which keeps php artisan queue:work up NEEDS to be restarted, otherwise the queue wont work.

On localhost, with bcc you get 2 identical emails, one for to recipient and one for bcc recipient - its a mailtrap feature, means it's working.

In production I tried it many times with bad code and office365 antispam blocked server's ip so after trying on personal gmail, it worked.

Here is the code I'm using:

Controller where I dispatch the job (emails can be both string or User model):

$user = User::where('email', 'test1@test.com')->first();
$sender = 'test@test.com'; 
$cc = User::where('email', 'test2@test.com')->first();
$bcc = User::where('email', 'test3@test.com')->first();
$subject = "Test subject";
$text = "Text mail body";
dispatch(new SendEmailJob($user,$sender,$cc,$bcc,$subject, $text))->delay(now()->addSeconds(10));

In the Job class:

public function handle()
{
    Mail::send(new TestMail($this->user, $this->cc, $this->bcc, $this->sender, $this->subject, $this->text));
}

And the actual Mail class:

public function build()
{
    return $this
        ->to($this->user->email, 'Main recipient') //can be $this->user
        ->bcc($this->cc->email, 'Hidden recipient')  //can be $this->cc
        ->from($this->sender)
        ->subject($this->subject)
        ->markdown('emails.test');
}

EDIT after some more digging:

So I was still getting this error "swift_transportexception expected response code 354 but got code 554" after it worked with some emails.

This error basically says that you are not authenticated on the server, or that "from" address doesnt match config('mail.username') address. Because I have multi-tenant app and my goal was to have "from" address dynamically changed by tenant, I was changing config(mail.username') and config('mail.password') by each email. But when the email actually got send after executing the job, config was already changed number of times and never would 100% of emails be sent.

So my solution to this, where I'm getting 100% of emails sent, is to not change config at all, have all the emails sent from "info@myapp.com" and specify "replyTo" by each tenant. Hope this helps someone.

Mail::to($request->user())
    ->cc($moreUsers)
    ->bcc($evenMoreUsers)
    ->send(new OrderShipped($order))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM