Laravel Notification – Customize markdown email header and footer

Laravel is one of the most popular PHP Framework. Today we are going to talk about its built in Email Notification functionality. We are using make:notification artisan command to create notification. The Notification only available on Laravel 5.3.x and above. I am using Laravel 5.7.x and hope this will works with the supported versions too.

Generate Notification

If you haven’t set up your project yet, then follow this official documentation like to set up your project.

Creating Notification is very simple with the handy command.

php artisan make:notification WelcomeNotification

The above command will create a folder Notifications in app directory app/Notifications/WelcomeNotification.php.

Laravel Notification supports various features like sending email, SMS, Slack etc. We are not going to discuss all of these features in this post. We are going to customize its default email template, specially header and footer.

Default Notification Email Template

Customize Markdown Email Header and Footer

Email notification header and footer changes with APP_NAME configuration. Find .env file in your projects root directory that has this APP_NAME parameter with the value Laravel. Lets change it to the Mailing App instead of Laravel.

Laravel Env Configuration

If we change the APP_NAME like the above, the output of the email should be like the below image.

Customize Email Header Footer

But this is not what we want! We want to set the Logo on the email header section and Support Team of something else on the footer.

Laravel provides a more better way to customize the email notification more robust. Let’s digging it more deeper.

php artisan vendor:publish --tag=laravel-mail

The above command will publish the Markdown mail components to resources/views/vendor/mail.

Markdown Mail Template

Now open the file message.blade.php from resources/views/vendor/mail/html/messages.blade.php

@component('mail::layout')
    {{-- Header --}}
    @slot('header')
        @component('mail::header', ['url' => config('app.url')])
            {{ config('app.name') }}
        @endcomponent
    @endslot

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

    {{-- Subcopy --}}
    @isset($subcopy)
        @slot('subcopy')
            @component('mail::subcopy')
                {{ $subcopy }}
            @endcomponent
        @endslot
    @endisset

    {{-- Footer --}}
    @slot('footer')
        @component('mail::footer')
            © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.')
        @endcomponent
    @endslot
@endcomponent

The above file contains the header and footer slots that you can modify according to your need. If you want to set the logo on the Markdown email template then modify this block.

{{-- Header --}}
@slot('header')
    @component('mail::header', ['url' => config('app.url')])
        {{ config('app.name') }}
    @endcomponent
@endslot

Replace {{ config(‘app.name’) }} with img tag and set the logo here.

{{-- Footer --}}
@slot('footer')
    @component('mail::footer')
        © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.')
    @endcomponent
@endslot

But here is one thing to notice about the footer. This footer slot only modifies the very bottom section on the footer. Not the Regards one.

Markdown Email bottom footer

So what we have to do to change the Regards section? Run the following command.

php artisan vendor:publish --tag=laravel-notifications

The above command will generate another directory notifications on resources/views/vendor/. Now open email.blade.php from the notifications directory.

@component('mail::message')
{{-- Greeting --}}
@if (! empty($greeting))
# {{ $greeting }}
@else
@if ($level === 'error')
# @lang('Whoops!')
@else
# @lang('Hello!')
@endif
@endif

{{-- Intro Lines --}}
@foreach ($introLines as $line)
{{ $line }}

@endforeach

{{-- Action Button --}}
@isset($actionText)
<?php
    switch ($level) {
        case 'success':
        case 'error':
            $color = $level;
            break;
        default:
            $color = 'primary';
    }
?>
@component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
@endcomponent
@endisset

{{-- Outro Lines --}}
@foreach ($outroLines as $line)
{{ $line }}

@endforeach

{{-- Salutation --}}
@if (! empty($salutation))
{{ $salutation }}
@else
@lang('Regards'),<br>{{ config('app.name') }}
@endif

{{-- Subcopy --}}
@isset($actionText)
@component('mail::subcopy')
@lang(
    "If you’re having trouble clicking the \":actionText\" button, copy and paste the URL below\n".
    'into your web browser: [:actionURL](:actionURL)',
    [
        'actionText' => $actionText,
        'actionURL' => $actionUrl,
    ]
)
@endcomponent
@endisset
@endcomponent

This file holds the Regards section and the Greeting section too. So if you want to set the Greeting from Hello! to Hi or something else, then you can change it from here.

So here is the Regards section that you can change according to your need.

{{-- Salutation --}}
@if (! empty($salutation))
{{ $salutation }}
@else
@lang('Regards'),<br>{{ config('app.name') }}
@endif

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.