6

Someone knows how to add headers to emails sent through Laravel Notification System?

I am not talking about Mailable classes where I can set header through the withSwiftMessage() method!

I also would like to keep using the MailMessage once I have a lot of emails built using the line, greetings methods!

Any one has any clue?

There is my code in case somebody needs to see anything!

<?php

namespace PumpMyLead\Notifications\Tenants\Auth;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class AccountActivation extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('My email subject')
            ->greeting('Just a greeting')
            ->line('Line 1')
            ->line('Line 2')
            ->action('CTA wanted', 'http://www.pumpmylead.com')
            ->line('Byebye');
    }
}

Thanks in advance!

miken32
  • 42,008
  • 16
  • 111
  • 154
Gustavo Bissolli
  • 1,551
  • 3
  • 22
  • 36

4 Answers4

9

Actually I've found 2 ways to append headers.

When the notification is sent via mail channel an Illuminate\Mail\Events\MessageSending event is fired.

Append a listener to it. In handle() you will get the Swift_Message object.

Or in the AppServiceProvider's register() method override the MailChannel with your own and append the header in the send() method.

$this->app->bind(
    \Illuminate\Notifications\Channels\MailChannel::class,
    MyMailChannel::class
);
Attila Fulop
  • 6,861
  • 2
  • 44
  • 50
Kedves Hunor
  • 815
  • 1
  • 11
  • 19
2

In ./app/Notifications/myNotification.php, add this code in your __construct() function:

$this->callbacks[]=( function($message){
    $message->getHeaders()->addTextHeader('x-mailgun-native-send', 'true');
});

Replace the "x-mailgun-native-send" with any header you wish to add, and 'true' with the desired value.

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

Debbie V
  • 314
  • 3
  • 5
2

Here's a modern 2022 alternative if the above doesn't work, tested in Laravel 8.

using withSwiftMessage()

public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject($subject)
        ->greeting($greeting)
        ->line($line1)
        ->line($line2)
        ->withSwiftMessage(function($message) use ($value1, $value2) {
            $message->getHeaders()->addTextHeader('X-MJ-CustomID', json_encode([
                'key1' => $value1,
                'key2' => $value2,
            ]));
        })
}
MikeyBeLike
  • 813
  • 1
  • 9
  • 15
0

Debbie V has a pretty close answer but not quite right. The issue she is referencing makes it clear, but she missed the necessary context that solution provided.

By default a Notification in Laravel uses MailMessage, however you can also have it return a Mailable instead. Only if you: a) create a custom mailable, and b) use that instead of MailMessage will the callbacks be applied.

A more complete solution would be:

  1. Create a custom mailable class php artisan make:mail MyMailable
  2. Update your public function toMail($notifiable) method to utilize a the new Mailable.
  3. Add the callbacks to the constructor of your MyMailable class.

And after that you should be all good. The hardest part of this is just adapting the current MailMessage you use to fit the API of a Mailable.