martinszeltins's avatar

How to send raw html email with Laravel?

I have successfully sent a raw text email with Laravel Mail but how could I send an HTML email (without using a view)?

\Mail::raw('Text to e-mail', function($message)
{
         $message->from('[email protected]', 'Laravel');
         $message->to('[email protected]');
});
1 like
3 replies
lostdreamer_nl's avatar
Level 53

How about this:

$html = '<b>My email</b>';
\Mail::send([], [], function (Message $message) use ($html) {
            $message->to('[email protected]')
        ->subject('my subject')
        ->from('[email protected]')
        ->setBody($html, 'text/html');
});

That should work

1 like
salamwaddah's avatar

In laravel 12 you can no longer use setBody instead you should use mailable classes in notifications. You also need to return a Mailable instead of a MailMessage, you will need to specify the message recipient using the mailable object's to method

php artisan make:mail YourMailableClass

public function toMail($notifiable): Mailable
    {
        $mail = new YourMailableClass;

        return $mail->to($notifiable->email)->html($this->html);
    }

see using mailables documentation

Please or to participate in this conversation.