PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 7, 2022

[FIXED] Adding headers to email sent as a Laravel Notification

 March 07, 2022     email, laravel, laravel-notification, php     No comments   

Issue

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!


Solution

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
);


Answered By - Kedves Hunor
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing