Laravel 9 SymfonyMailer error - An email must have a "To", "Cc", or "Bcc" header
Asked Answered
G

5

7

I've got problems with sending emails after updating a Laravel 8 project using the Metronic 8 theme to Laravel 9. I didn't change any of my code related to emails, but now I got this error using the Sendmail driver :

An email must have a "To", "Cc", or "Bcc" header. {"userId":6,"exception":"[object] (Symfony\Component\Mime\Exception\LogicException(code: 0): An email must have a "To", "Cc", or "Bcc" header. at /home/myhome/public_html/myproject.com/vendor/symfony/mime/Message.php:128)

Controller

class MyController extends Controller
{
    public function activate($id)
    {
        $mymodel = MyModel::find($id);

        $contact = Contact::where('mymodel_id', $mymodel->id)->first();
        Mail::to($contact->email)->send(new MyMail($contact));
    }
}

Mailable

class MailActive extends Mailable
{
    use Queueable, SerializesModels;

    protected $contact;

    public function __construct(Contact $contact)
    {
        $this->contact = $contact;
    }

    public function build()
    {
        return $this->from('[email protected]', 'Me')
            ->view('emails.myemailview')
            ->with([
                'contact' => $this->contact
            ]);

        // $this->withSymfonyMessage(function (Email $message) {
        //     $message->getHeaders()->addTextHeader(
        //         'addAddress', '[email protected]'
        //     );
        // });

        // return $this;
    }
}

I've tried to render the mail alone, and it works. Using the log driver, it works, and as you can see, I even tried to bypass the Mail facade using the withSymfonyMessage method in the mailable class, but nothing works.

Is there a config or a part I'm missing? Could I go back using SwiftMailer without much trouble for Laravel 9 to work?

Granulite answered 17/2, 2022 at 10:28 Comment(2)
In your upgrade process can you install these packages? composer require symfony/mailgun-mailer symfony/http-clientBlew
What have you tried to resolve the problem? As far as I see, you are not setting any receiver in the given code?Homestead
M
5

use ->to()

return $this->from('[email protected]', 'Me')
            ->to($email, $name)
            ->view('emails.myemailview')
            ->with([
                'contact' => $this->contact
            ]);
Merrythought answered 17/2, 2022 at 11:25 Comment(2)
It works like that yes ! The Mail facade doesn't take the to() anymore and we have to specify the address in the Mailable class ?Granulite
I am using Laravel 10. where to include this snippet ? I don't have any build()Handwork
S
2

An email must have a "To", "Cc", or "Bcc" header. {"userId":6,"exception":"[object] (Symfony\Component\Mime\Exception\LogicException(code: 0): An email must have a "To", "Cc", or "Bcc" header. at /home/myhome/public_html/myproject.com/vendor/symfony/mime/Message.php:128)

You will get the above exception if to() is missing or if the to() method receive an empty collection in your Mailable class

Spiky answered 18/3, 2022 at 17:16 Comment(0)
H
1

I am using Laravel 10+ and doing just this while calling Mail facade, worked for me:

\Mail::to('[email protected]')->send(new MyMailableClass());

P.S. I am using MailTrap to test my emails for this.

Hope it helps!

Handwork answered 11/5, 2023 at 17:2 Comment(0)
M
-2

to() mail address is null given or cache issue

cache issue: php artisan optimize:cl php artisan config:cache php artisan config:cl

Morelli answered 28/3, 2023 at 12:35 Comment(2)
Please add some explanation to your answer such that others can learn from it. What makes you think that this is a caching issue?Homestead
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Velleman
C
-3

go to .env file and just add:

MAIL_TO_ADDRESS="[email protected]"

Comment answered 14/8, 2023 at 6:6 Comment(1)
Please add some explanation to your answer such that others can learn from it. Shouldn't the mail be sent to a varying mail address instead of a static one?Homestead

© 2022 - 2024 — McMap. All rights reserved.