Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

squibby's avatar

How to add custom headers to Notification emails?

Since upgrading to Laravel 5.4 I have been making use of notifications. I send some emails via mailgun and previously added a custom header so I could receive webhooks from mailgun and track mail delivery etc.

I would do something like below:

Mail::send('emails.broadcast', array('recipient' => $this->recipient , 'broadcast' => $this->broadcast), function($message) use ($data) {
    $message->getHeaders()->addTextHeader('recipient_token', $data['token']);
    $message->to($data['email']);
    $message->subject($data['title']);
});

So.. how can I add the custom header I need to send in the email? I don't want to send the header in all emails, just some specific emails from my app.

Thanks.

0 likes
6 replies
squibby's avatar

When sending notifications a Illuminate\Mail\Events\MessageSending event is fired. If I create a listener for this event I can get access to the Swift_Message object. I can then append a header to the object like this:

 public function handle(MessageSending $event)
    {


        // Get Swift_Message obj
        $message = $event->message;

        // Get Message Headers
        $headers = $message->getHeaders();

        // Append custom header
        $headers->addTextHeader('Custom-Header-Name', 'some-value');


    }

Problem now.. is that the header value I want to pass will be dynamic. I have no way of passing it to the listener and everything is getting muddier...

squibby's avatar
squibby
OP
Best Answer
Level 8

ok. I found a better way.

Moving from 5.2 to 5.4 threw me as there are quite a few changes making me think I needed to use notifications. Instead of using notifications I have used Mail class and been able to add the custom headers no problem. I can still access all the notification markdown email boilerplate so this is suitable for me to make all emails consistent.

Charrua's avatar

Hello @squibby I have the same problem, I want to send notifications and attach a custom header with model id, how do you solve this? I have my code for mail notification but I cant find a way to attach the header.

public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->greeting('Hello ' . $this->appointment->customer)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }
debbiev's avatar

https://github.com/laravel/ideas/issues/475

Add this code to ./app/Notifications/MyNotification.php, in the __construct() function:

public function __construct()
{
    $this->callbacks[] =( function ($message) {
        $message->getHeaders()->addTextHeader('Header', 'Value');
    });
}
2 likes
Dreda5's avatar

That's how I did it

            return (new MailMessage)
                ->subject('Subject')
                ->markdown('mail.inquiry.status_canceled')
                ->withSwiftMessage(function ($message) use ($hash) {
                    $message->getHeaders()->addTextHeader('X-Mailgun-Variables', '{"log_id": "' . $hash . '"}');
                });
2 likes

Please or to participate in this conversation.