Laravel isn't sending to my CC's and BCC contacts when standard PHP mail() code does

794 Views Asked by At

I raised a question a week or two back about the syntax for sending emails using the standard Laravel mail conventions, specifically including cc/bcc recipients.

My assumption was that my code was incorrect and for this reason whenever I tried to email with cc/bcc recipients it didn't work. When I say it didn't work, I mean that "to" recipients received the email, but nobody specified as a "cc" / "bcc" recipient did.

However, having tried all the code samples given in that post and having undertaken further testing it seems that this assumption was incorrect.

By more testing, I mean that I've now made simple test pages of PHP that use the default PHP mail() function. Using these, the cc/bcc recipients DO work.

So this suggests something in the Laravel setup must be at fault. However it isn't something fundamental such as my mail config because sending to normal recipients that are not cc/bcc works reliability with no issue.

I've also varied my test cc/bcc recipients quite a lot and the mails never arrive for any of them. So this isn't specific to a certain email platform or receiving server that is rejecting them.

So what could be wrong here? Any ideas?

What options do I have to enable verbose logging to the mail mechanisms in Laravel (v9) ?

Example code for sending to cc/bcc recipients:

$options = [
    'cc' => '[email protected]',
    'bcc' => ['[email protected]', '[email protected]'],
];

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

There are 1 best solutions below

9
rozsazoltan On

From version 9.x of Laravel, the previously used SwiftMailer became outdated, and its support has been discontinued. Instead, Symfony Mailer has been introduced. (For more information, see: https://laravel.com/docs/9.x/upgrade#symfony-mailer)

The syntax of Symfony Mailer is slightly different, so source codes related to Message::class before 2022 may not be 100% reliable.


NOW

If you are using Laravel 9.x or higher, use the following code:

$options = [
    'cc' => '[email protected]',
    'bcc' => ['[email protected]', '[email protected]'],
];

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

OUTDATED (from 9.x)

If you are using Laravel 8.x or lower, you should always try to pass an array to the cc and bcc functions:

$options = [
    'cc' => ['[email protected]'],
    'bcc' => ['[email protected]', '[email protected]'],
];

Mail::send([], [], function ($message) use ($email, $options) {
    $message
            ->setTo($email)
            ->setCc($options['cc'])
            ->setBcc($options['bcc'])
            ->setSubject('A test emai')
            ->setBody('This is the email content.', 'text/html');
});