laravellaravel-mail

Sending emails without mailables in Laravel works except for the cc/bcc recipients


I need to send some test emails from Laravel and want to do it, without Mail classes, etc. I usually use the Mail::send() function for this and it works fine. However, I've just realized that cc/bcc recipients don't seem to work when using this form.

In case useful to know, I'm using sendmail.

$result=Mail::send('site.emails.empty-template', ['msg'=>"Hi this is a test msg"], function ($message) {
    $message->from('noreply@me.com');
    $message->subject("A test email");
    $message->to("to@test.com");
    $message->cc("cc@test.com");
    $message->bcc(["bcc1@test.com","bcc2@test.com]);
});

Solution

  • You can use Mail::send method, and by using this, you can customize your subject and body.

    $options = [
        'cc' => 'cc@test.com',
        'bcc' => ['bcc1@test.com', 'bcc2@test.com'],
    ];
    
    Mail::send([], [], function ($message) use ($email, $options) {
        $message->to($email)
                ->cc($options['cc'])
                ->bcc($options['bcc'])
                ->subject('A test emai')
                ->setBody('This is the email content.', 'text/html');
    });
    
    

    OR

    you may use this one

    Create a new mailable class

     php artisan make:mail MyMail
    
    class MyMail extends Mailable
    {
        use Queueable, SerializesModels;
    
        public function __construct()
        {
            //
        }
    
        public function build()
        {
            return $this->view('emails.my-email')
                        ->cc(['cc@test.com']);
        }
    }
    

    Use class to send the email

     Mail::to($email)->send(new MyMail());