Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding headers to email sent as a Laravel Notification

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!

like image 587
Gustavo Bissolli Avatar asked Apr 19 '17 20:04

Gustavo Bissolli


People also ask

How do I change my email header in laravel?

php artisan vendor:publish --tag=laravel-mail and go to the resources/views/vendor/mail/html/message. blade. php and modify the header and footer slot. For changing Hello to Hello {user_name}, there is markdown called greeting() method that holds Hello! , you can change it whatever you want.

What is the difference between mail and notification in laravel?

From what I understand, Mailables are used to send only emails whereas Notifications can be used to send emails and sms. In my application, I dont have plans to send sms notifications for now, so I am confused if I should just use the Mailable class in this case.

What is notify () in laravel?

Laravel Notify is a package that lets you add custom notifications to your project. A diverse range of notification design is available.


2 Answers

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
);
like image 58
Kedves Hunor Avatar answered Sep 20 '22 02:09

Kedves Hunor


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

like image 41
Debbie V Avatar answered Sep 21 '22 02:09

Debbie V