Laravel Mail Notifications: How to Customize the Templates

Laravel has a useful Notification system, where you can notify user about something via email, Slack etc. And there is a quite good default HTML template for emails. But what if you want to customize its design?

This is the template I’m talking about:

laravel notification

Let’s remind ourselves how to send this email.

It will generate class app/Notifications/HelloUser.php – here’s the main part of it:

By default, notification channel is mail, and there is some default email constructed.

You build the email text by using methods like ->line() or ->action(), and actually in the back-end it fills in a beautiful HTML template.

How to fire this notification?

Now, we didn’t edit any Blade template or any HTML, where to find it? It’s not that easy, by default it’s not in resources/views anywhere. You need to publish the templates, like this:

Result:
Copied Directory [/vendor/laravel/framework/src/Illuminate/Notifications/resources/views] To [/resources/views/vendor/notifications]

So before you publish – the template is inside of Laravel core in /vendor folder (which you shouldn’t edit, ever). And now – we can modify our HTML.

In fact, there’s only one published template – file resources/views/vendor/notifications/email.blade.php:

But wait, you will say – where’s all the HTML?? It’s hiding under another layer – Laravel’s notification components based on Markdown language.

There are three components, mentioned in the official Laravel docs:

1. Button:

2. Panel:

3. Table:

And yes, you can write Markdown instead of HTML, it may be a convenient thing for some people (developers, mostly).

But if you do want to get HTML and be able to edit it, run this:

This will happen:
Copied Directory [/vendor/laravel/framework/src/Illuminate/Mail/resources/views] To [/resources/views/vendor/mail]

This is what we get then:

laravel mail components

Now it looks really familiar and we can, for example, go to button.blade.php and add some class or text:

We change {{ $slot }} into Click here: {{ $slot }} and we get this email:

laravel notification

Finally, you can even have THEMES with different CSS styles for each of them – like, for example, different emails for different user groups.

For that, there’s a folder resources/views/mail/html/themes with a CSS file, and you can change the default them in config/mail.php:

So this is how to customize Laravel email notifications. You can find more information in the official documentation.

Liked the article? Maybe you need help with your Laravel project?
Check out my Upwork profile and hire me!

One thought on “Laravel Mail Notifications: How to Customize the Templates

Leave a Reply

Your email address will not be published. Required fields are marked *