Grayscale profile picture

Patrique Ouimet


Adding Custom Headers to Laravel Mail Notifications

Mon, Feb 18, 2019 3:24 PM


We'll need a few things to get this going. First we'll add a notification class:

php artisan make:notification UserRegistered
Notification created successfully.

Which should create the file app/notifications/UserRegistered.php

Within this file we need to make sure the via method looks like this (or at the very least has 'mail'):

public function via($notifiable)
    return ['mail'];

And that the toMail method looks like this:

public function toMail($notifiable)
    return (new UserEmailConfirmation);

NOTE: don't forget to add the use statement for UserEmailConfirmation after you've created the next class

Next we'll need a Mailable class:

php artisan make:mail UserEmailConfirmation
Mail created successfully.

Which should create the file app/mail/UserEmailConfirmation.php

Adding Custom Headers

Here's where we can set custom headers, within the build method within UserEmailConfirmation:

public function build()
    $this->withSwiftMessage(function ($message) {
        $message->getHeaders()->addTextHeader('X-Custom-Header', 'A Value For It');

    return $this->view('');


This seems like a complicated solution unfortunately it's the best solution I've found for setting custom headers on mail notifications. There is a solution where you can custom headers on every email sent (solution with Laravel's built-in MessageSending event), but the above solution gives you the flexibility to set it on a per mailable instance. Hope you liked the tip, thanks for reading!


You can also accomplish this with Mail::send()

Mail::send('', $data, function ($message) {
    $message->getHeaders()->addTextHeader('X-Custom-Header', 'A Value For It');