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

DhPandya's avatar

How to pass custom variables in Laravel notifications?

I'm using Laravel notifications to send email notification now I've to change the logo of the header dynamically so I need to pass variable from the notification class.

return (new MailMessage)
            ->subject($this->emailContent['subject'])
            ->line(new HtmlString($this->emailContent['email_body']))
            ->action('Pay Invoice', url('/'))
            ->line('Thank you for using our application!')
            ->attachData(base64_decode($this->pdf), 'invoice.pdf', [
                'mime' => 'application/pdf',
            ])
            ->with(['logo' => getEmailTemplateLogo($this->invoice->business)]);

I see many blogs suggesting with() but it also not working. Am I missing here? Could someone please guide?

I'm using the default header file which I get by publishing the vendor:publish resources\views\vendor\mail\html\header.blade.php

0 likes
12 replies
tykus's avatar

You need to pass the logo as a prop to the header component in resources/views/vendor/mail/html/message.blade.php

<x-mail::header :url="config('app.url')" :logo="$logo ?? 'some default'">

and update the resources/views/vendor/mail/html/header.blade.php component to accept the logo prop:

@props(['url', 'logo'])
<tr>
<td class="header">
<a href="{{ $url }}" style="display: inline-block;">
@if (trim($slot) === 'Laravel')
<img src="{{ $logo }}" class="logo" alt="Logo">
@else
{!! $slot !!}
@endif
</a>
</td>
</tr>
DhPandya's avatar

@tykus I did it in the same you mentioned but still the header file is not getting this $logo variable. Attaching full code for all 3 files.

SendInvoiceToCustomerNotification.php

message.blade.php

<x-mail::layout>
{{-- Header --}}
<x-slot:header>
<x-mail::header :url="config('app.url')" :logo="$logo ?? Vite::asset('resources/images/logo.svg')">
{{ config('app.name') }}
</x-mail::header>
</x-slot:header>

{{-- Body --}}
{{ $slot }}

{{-- Subcopy --}}
@isset($subcopy)
<x-slot:subcopy>
<x-mail::subcopy>
{{ $subcopy }}
</x-mail::subcopy>
</x-slot:subcopy>
@endisset

{{-- Footer --}}
<x-slot:footer>
<x-mail::footer>
© {{ date('Y') }} {{ config('app.name') }}. {{ __('All rights reserved.') }}
</x-mail::footer>
</x-slot:footer>
</x-mail::layout>

header.blade.php

@props(['url', 'logo'])
{{-- @dd($logo) --}}
<tr>
<td class="header">
<a href="{{ $url }}" style="display: inline-block;">
<img src="{{ $logo }}" class="logo" alt="{{ config('app.name') }}" style="width:100px;">
</a>
</td>
</tr>

Thanks

tykus's avatar

@DhPandya are you getting the old (Laravel) logo, or are you seeing an undefined variable error message?

DhPandya's avatar

@tykus Seeing resources/images/logo.svg this always If removes this from the message.blade.php will see the undefined variable error.

tykus's avatar

@DhPandya I cannot reproduce this error; are you sure you are working with the correct message.blade.php template?

DhPandya's avatar

@tykus Yes, I'm sure. That is why I pasted the code above. So we can check we are on the same page. Image

tykus's avatar

@DhPandya I don't know... are your views cached???

php artisan view:clear

Also, can you try setting the default value in the header component; just to prove that it will use the prop (even if it believes it is not being passed from the message template

@props(['url', 'logo' => Vite::asset('resources/images/logo.svg')])

EDIT just saw the image and the undefined variable is in the message, not the header. Are you mitigating for notifications/mails that do not pass the $logo at all???

DhPandya's avatar

@tykus No. I'm not using view caching and I tried clearing it too. I passed the default path as mentioned by you. It is taking the default logo but still not taking the logo which I'm passing my notification class.

tykus's avatar

@DhPandya okay, let's reset here... how many notifications are being sent in this one Request, and do all of the Notification classes pass a logo to the MailMessage?

Because, in the image, your message.blade.php template has no fallback

<x-mail::header :url="config('app.url')" :logo="$logo">

like you were showing earlier above:

<x-mail::header :url="config('app.url')" :logo="$logo ?? Vite::asset('resources/images/logo.svg')">
DhPandya's avatar

@tykus Yes, some of the notifications will use the default website logo and some of the notification will use their own business logo while sending invoice to the customer.

@props(['logo'=>Vite::asset('resources/images/logo.svg')])
@dd($logo)
<x-mail::layout>
{{-- Header --}}
<x-slot:header>
<x-mail::header :url="config('app.url')" :logo="$logo">
{{ config('app.name') }}
</x-mail::header>
</x-slot:header>

{{-- Body --}}
{{ $slot }}

{{-- Subcopy --}}
@isset($subcopy)
<x-slot:subcopy>
<x-mail::subcopy>
{{ $subcopy }}
</x-mail::subcopy>
</x-slot:subcopy>
@endisset

{{-- Footer --}}
<x-slot:footer>
<x-mail::footer>
© {{ date('Y') }} {{ config('app.name') }}. {{ __('All rights reserved.') }}
</x-mail::footer>
</x-slot:footer>
</x-mail::layout>

Notification

return (new MailMessage)
            ->subject($this->emailContent['subject'])
            ->line(new HtmlString($this->emailContent['email_body']))
            ->action('Pay Invoice', url('/'))
            ->line('Thank you for using our application!')
            ->attachData(base64_decode($this->pdf), 'invoice.pdf', [
                'mime' => 'application/pdf',
            ])
            ->with([
                'logo' => getEmailTemplateLogo($this->invoice->business)
            ]);

So I did this in the message.blade.php so header.blade.php will always have the logo. But the problem is that the logo custom variable I'm passing from the notification is not accessible in the message.blade.php. I even accepted the props.

btw thanks for guiding me.

tykus's avatar
tykus
Best Answer
Level 104

@DhPandya really sorry... I was wrong above! with is not the correct method - it adds a line; my test of the resulting markup was badly implemented!

Here is what you actually need to do:

// app/Notifications/SendInvoiceToCustomerNotification.php

    public function toMail(object $notifiable): MailMessage
    {
        $message = (new MailMessage)
            ->subject($this->emailContent['subject'])
            ->line(new HtmlString($this->emailContent['email_body']))
            ->action('Pay Invoice', url('/'))
            ->line('Thank you for using our application!')
            ->attachData(base64_decode($this->pdf), 'invoice.pdf', [
                'mime' => 'application/pdf',
            ]);

           // Set the viewData on the MailMessage instance

           $message->viewData([
                'logo' => getEmailTemplateLogo($this->invoice->business)
            ]);

            return $message;
    }
// resources/views/vendor/notifications/email.blade.php
+ <x-mail::message :logo="$logo">

This, along with the earlier changes to message and header should now work!

DhPandya's avatar

@tykus Thank you so much man, Respect for you. However the viewData() is not a method. See image.

I implemented it like this.

  $message = (new MailMessage)
            ->subject($this->emailContent['subject'])
            ->line(new HtmlString($this->emailContent['email_body']))
            ->action('Pay Invoice', url('/'))
            ->line('Thank you for using our application!')
            ->attachData(base64_decode($this->pdf), 'invoice.pdf', [
                'mime' => 'application/pdf',
            ]);
        $message->viewData['logo'] = getEmailTemplateLogo($this->invoice->business);
        return $message;

It gave me clue from your reply. Marking your answer as Best answer. Thanks a lot! I was working on this from the morning.

1 like

Please or to participate in this conversation.