There are two ways to customize your laravel email notification template, let’s discuss them one by one.
By default laravel has a package to handle the notifications and the notification channel can be anything, like database, email, slack and many more.
The database notifications are used to show on the same platform, like I have a website and I want to show the notifications on the notification page to my user’s account.
Other notification channels are sending the data to the specified channel like email notification sends an email to the users.
There are some online tools to generate your expected markdown template by using WYSIWYG editors like stackEdit, Dillinger and onlinemarkdowneditor.dev etc.
Now, the fundamentals are clear, let’s start customizing the notification markdown template by following below steps.
You need to generate the notification class first with the below command.
php artisan make:notification NewMessageNotification
The name of the notification class can be anything, there is no specific rule to choose the notification name but it can be something self-documented which describes its usage.
After generating the notification class, it’s time to publish the notification resources with the below artisan commands.
php artisan vendor:publish --tag=laravel-notifications
php artisan vendor:publish --tag=laravel-mail
The first command publishes the notification which copies the notification files from vendor to resources/views/vendor/notifications/email.blade.php
. This file only contains the final draft of the notification which means it uses the message, buttons and sub-copy components.
To get the full control over the template customization we need to run the second command. Which will copy the all blade files like buttons, header, footer, message and sub-copy to resources/views/vendor/mail/html
and to resources/views/vendor/mail/text
.
You can customize the css from resources/views/vendor/mail/html/themes/default.css file
.
To use the blade file as a notification email template, you can use view()
method of the MailMessage
class. The blade can contain plain html or text or markdown as per your requirement. Just update the toMail()
method of your notification class as given below in the code.
public function toMail($notifiable) { return (new MailMessage) ->subject("New Notification") ->view('mail.template', [ 'content' => $this->content ]); }
As you can see in the code above, I am passing the content variable to the view template which I can use to fill the data with other html code. We have all required methods available on the MailMessage
class like, from, to, cc, bcc, subject and view etc.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…