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

caleb65's avatar

Extending MailMessage class in laravel

I have a situation in which I need to customize or rather add some buttons in emails going to a specific set of users, let's say we have normal users and investors and I have some notification classes that send emails to investors. In these Notification classes, I'll need to add some links they can click on to view some resources for them only. I created a MailMessage class that extends Laravel's MailMessage class with a method target()

<?php

namespace App\Notifications\Messages;

use Illuminate\Notifications\Messages\MailMessage as MessagesMailMessage;

class MailMessage extends MessagesMailMessage
{
    /**
     * The target audience.
     *
     * @var string
     */
    public $target;

    /**
     * Set the target audience for the mail message.
     *
     * @param  string  $target
     * @return $this
     */
    public function target(string $target)
    {
        $this->target = $target;

        return $this;
    }
}

then in the notification class, I used the new MailMessage class which I created that still extends Laravel's MailMessage

use App\Notifications\Messages\MailMessage;
class InvestorNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $investment;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Investment $investment)
    {
        $this->investment = $investment;
    }

    /**
     * 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())
                    ->target('investor')
                    ->line('Thank you for choosing us!');
    }


but in the /resources/view/vendor/notification/email.blade.php I tried to check if $target exists and then display the buttons but the $target is not returning as undefined please help in any way you can

0 likes
4 replies
kevinbui's avatar

I don' really like extending the MailMessage class. I don't know whether the /resources/view/vendor/notification/email.blade.php template is used or not.

A much simpler approach is to create a custom email template for the notification, and inject the $target.

public function toMail($notifiable)
{
    return (new MailMessage)->view(
        'emails.name.html',
        ['target' => 'investor']
    );
}
caleb65's avatar

@kevinbui The thing is, there are many notification classes that are in use right now on the app in which most of them don't have view files. I'm thinking of something that'll be a simple solution without having to create markdown view files for all the notifications'

2 likes
Merklin's avatar

@beitomartinez As said, one option is to create a custom email template and define it with the ->view() in the toMail() function. Another way is to use ->line(new HtmlString() and directly pass the HTML of the content you want to show.

Please or to participate in this conversation.