Add custom property to Mail in Laravel

687 Views Asked by At

I'm trying to get Postmark's new Message Streams working with Laravel.

Is there a way of adding the property + value '"MessageStream": "notifications" to the JSON body of an email sent using Laravel mail? I'm guessing I will need to extend the Mailable class in some way to do this.

Ideally, i'd like to be able to do something like the following in my Mailable class:

DiscountMailable.php

public function build()
    {
        return $this->from('[email protected]')
        ->markdown('emails.coupons.created')
        ->subject(' Your Discount')
        ->with([
          'coupon' => $this->coupon,
        ])
        ->messageStream(
          'notifications',
        );

    }
1

There are 1 best solutions below

0
On

Just to make sure this question has an answer, this is what was necessary to make it work (and it worked for my issue in Laravel 7 today as well, where I wanted to use different Postmark Streams for different mail notification types).

public function build()
{
    return $this->from('[email protected]')
        ->markdown('emails.coupons.created')
        ->subject(' Your Discount')
        ->with([
            'coupon' => $this->coupon,
        ])
        ->withSwiftMessage(function ($message) { 
            $message->getHeaders()
                ->addTextHeader('X-PM-Message-Stream', 'notifications')
        });
}