Search code examples
phplaravelsendmailsymfony-mailerlaravel-9

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


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?


Solution

  • use ->to()

    return $this->from('[email protected]', 'Me')
                ->to($email, $name)
                ->view('emails.myemailview')
                ->with([
                    'contact' => $this->contact
                ]);