Laravel use MailMessage in Mailable

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel MailMessage in Mailable: Crafting Dynamic and Clean Emails

As developers working with Laravel, one of the most frequent tasks we face is sending transactional emails—registration confirmations, password resets, order receipts, and welcome messages. While Laravel provides robust tools for this via Mailable classes, customizing the final HTML output often leads to repetitive, messy code involving manually writing CSS or duplicating boilerplate HTML structures across different notification types.

You’ve hit upon a very insightful point: why build everything from scratch when Laravel already provides mechanisms designed for structured email content? The answer lies in understanding how the MailMessage class fits into the Mailable ecosystem.

This post will dive deep into using MailMessage within your Mailable to inject dynamic, structured content directly into your emails, allowing you to leverage existing templates while keeping your data clean and highly customizable.


Understanding MailMessage in Laravel

When sending an email in Laravel, the process goes beyond just rendering a Blade view. It involves structuring the message itself—handling headers, preheader text, and overall context. This is where Illuminate\Notifications\Messages\MailMessage comes into play.

The MailMessage object acts as a container for specific, structured components of an email, allowing you to define content that can be used by underlying mail services (like SMTP) or template engines. By utilizing this class, you separate the data you want to send from the presentation of the email.

The Problem with Manual Templating vs. MailMessage

Your initial approach—manually building lines within the Mailable constructor and passing them via $this->view('message')—is functional, but it forces you to manage raw text content that must then be manually integrated into your Blade view logic. If you want to reuse complex HTML structures (like those used in default Laravel notifications), you need a way to inject that structure alongside your dynamic data.

The goal isn't just sending text; the goal is sending a fully structured, context-aware email.

Implementing Dynamic Content with MailMessage

Instead of manually concatenating strings, we can leverage MailMessage to prepare the content that will eventually populate your template. This allows you to define specific parts of the email (like greetings or line items) in a standardized way.

Here is how you can refactor your Mailable to use MailMessage more effectively for dynamic content injection:

php
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

    public $user;
    public $message;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;

        // Initialize the MailMessage object with dynamic content
        $this->message = (new MailMessage)
            ->greeting('Bonjour ' . $user->name)
            ->line('Nous vous remercions de votre inscription.')
            ->line('Pour rappel voici vos informations :')
            ->line('Mail: ' . $user->email)
            ->line('Password: ' . $user->password);
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        // Pass the structured MailMessage object to the view
        return $this->view('message')
            ->with([
                'slot' => $this->message // Make the structured data available to the view
            ]);
    }
}

How This Improves Your Workflow

By passing the fully constructed $this->message object to your view (e.g., view('message')), you are no longer just passing raw strings. You are passing a structured object that contains predefined lines and greetings.

In your Blade file (resources/views/mail/message.blade.php), instead of manually writing:

html
<h1>{{ $user->name }}</h1>
<p>Bonjour {{ $user->name }}</p>
...

You can access the structured content directly, making your template cleaner and easier to maintain. You are essentially leveraging Laravel’s built-in notification structure, which is a core principle of good application design, much like principles discussed in frameworks like Laravel.

Conclusion

Using MailMessage within your Mailable is the recommended, idiomatic way to handle dynamic content injection for emails in Laravel. It moves you away from manually constructing raw strings and towards a structured approach. By treating $this->message as a container for email components, you ensure that your welcome emails are not only personalized but also align with the robust structure provided by the framework. Embrace these built-in classes to write cleaner, more maintainable, and more professional transactional emails.