For reference: »An email must have a "From" or a "Sender" header.«
Hi! I just upgraded an existing Laravel 8.latest app to Laravel 9 with the new Symfony mailer. I got the following error:
The following exception occurred during the request:
Symfony\Component\Mime\Exception\LogicException: An email must have a "From" or a "Sender" header. in \vendor\symfony\mime\Message.php:132
Before with the old mailer, I did not need to set a sender - it could be empty. And this was the case in testing, because I didn't had it set in .env.testing. It was set to null.
This needs to be set in your .env or .env.testing to pass tests if you use the new mailer:
[email protected]
Maybe this helps for future reference - it debugged it for 45 minutes and felt stupid in the end 🙈
I'm sending notifications using standard Laravel 9.x Notifications.
I have to explicitly call from method in order to avoid exception "An email must have a "From" or a "Sender" header."
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
// TODO notification should work without explicit ->from(...)
// currently if not present it throws exception
return (new MailMessage)
->from(config('mail.from.address'), config('mail.from.name'))
->greeting('Hello ' . $notifiable->name)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}