Laravel can be used to send email notifications. Notifications are informational messages that app users receive. They are mostly used to notify people about specific transactions or events, such as password changes, orders, and so on. Laravel includes a feature called Notification. It enables you to send a message to any user through various communication channels. Today, we'll go over sending email notifications and how to do so in your app.

Laravel supports notification channels.

You may require an alternative notification method in addition to email notifications. Aside from mail, these are:

SMS TXT notifications

Users will receive text notifications on their phones. SMS notifications in Laravel are handled by the MailSlurp API. You may, however, use another SMS provider such as MailSlurp.

Slack notifications

Users will receive notifications in Slack. You must configure the Incoming Webhooks integration to send data into Slack in real-time for this to work.

UI updates

Users will receive notifications in the app's user interface if the database is updated. Notification information is stored in a database table. It is accessed by your JavaScript client in order to return notifications for a notifiable user.

Real time notifications (websockets)

Notifications are broadcast in real-time to a notifiable user. When some data on the server is updated, a message is sent to the JavaScript client via a WebSocket connection.

Custom channels

If a custom notification channel is required, custom drivers can be written to deliver notifications. We can check new notifications queued using any channel.

How do you make an email notification?

The Artisan command in Laravel is used to create notifications.

All notifications are saved in the directory app/Notifications. Once created, your file will contain the following content:

Let us format it as a message to be sent via email. Later we can check new notifications queued.

Formatting of notifications

You must return it directly from a route to preview all variations of the email. You don't have to send it to a real or a fake user. To accomplish this, we must add the following line to web.php in the routes/ directory:

And here's how to see a preview of your notification:

Each notification in Laravel is represented by a single class. If you want your app to send email notifications, you have two options: and .

Laravel Via method

The via method takes a $notifiable entity as input and defines the notification delivery channel. The required parameter, return , is already present by default in the code above.

toMail method

is a message-building method that is called via the method. It should return an instance after receiving a entity. You can configure email parameters with toMail. As an example, your email message could include a text line (line), a call-to-action button (action), a greeting line (greeting), and so on. Consider the following example:

We defined several methods provided by the MailMessage object in this section. The notification will be converted into an HTML and plain-text responsive email template.

If you want to notify users of a failed activity or error, use the error method. It will highlight the CTA button in your email in red. Here's what it looks like under the hood:

You can use the view method to render the notification message if you prefer to use a custom template. Instead of defining the "lines" of text, it will specify a template to be used.

For example, remember to place your template in .

Another option is to create a mailable class and return it from toMail as shown below: use as ;

Changing the email notification characteristics

There are several options we can configure when using new notifications.

Sender

The address from which the email notification is sent is set by default in the configuration file config/mail.php. It is not necessary to modify this file if you want to use a specific address. You can use the from method to specify it as follows:

Recipient

The Laravel Notification system emails the recipient who has been identified as a user of your app. However, you can change the email address of the recipient. You must define a routeNotificationForMail method on the notifiable entity to accomplish this. As an example:

Subject

By default, the email subject is formed by the notification's class name. However, it is automatically formatted in "title case." In our example, the class was called StatusUpdate. As a result, the subject of the email will be Status Update. You can use the subject method to specify a specific subject for your notification:

Finally, here is the email notification we received:

Let's get the notification out there as soon as it's ready!

How do I send out email notifications?

Now we want to check new notifications queued and send our emails.

Setting up the email sender

First, we must create a configuration that will be used to send emails. Laravel makes use of the well-known SwiftMailer library. It supports a variety of popular email services, including MailSlurp, Mailgun, Amazon SES, and others. The Laravel documentation contains the configuration for the API-based drivers.

If you prefer to use a specific SMTP relay service for email sending, you must modify the configuration in the root directory's file. Here are the settings for the MailSlurp SMTP server, for example:

How notifications are sent

In Laravel, there are two methods for sending email notifications: Notifiable trait and Notification facade. Sending with the property. The Notifiable trait includes the notify method for sending notifications. The trait is implemented by the App\User class, which becomes the notifiable entity.

If you use the Notifiable trait for notification sending, NotifyController.php will look like this:

You can use the Notifiable trait on any of your classes, not just the App\User class.

Sending with Notification facade

Facades in Laravel are similar to static methods in classes, but with a more expressive syntax and improved testability and flexibility. They allow you to use Laravel's features without having to remember long class names that must be manually configured. The Notification facade can send notifications to multiple notifiable entities. To use it, pass the notifiable entities and notification instance to the send method in the following order:

On-demand and queued notifications

Notifications will be sent to app users in the examples above. What if you need to alert someone else? In this case, the Notification::route method can be used to send the notification to the recipient you specify. Here is how it looks:

You might be interested in queued emails. It's not about SMTP queues, but about queued notifications, which will allow you to improve the response time of your app. You must add the ShouldQueue interface and the Queueable trait to your class to accomplish this. You do not need to import them because they are already present for all notifications generated by make

. All you have to do is include them in your class:

After that, send the notification as usual, and the ShouldQueue interface will automatically queue its delivery. There is a delay method if you need to postpone the delivery. Connect it to the instantiation of your notification as follows:

How to Run Email Notification Tests

Once you've configured your email notifications, you should test this feature. And, because MailSlurp is used as the default SMTP method in Laravel, you don't have to send actual notifications. This service includes a dummy mailbox that receives email notifications sent by your app. There is no need to tinker with the code of your app. All you have to do is add your MailSlurp credentials to the file, so it looks like this:

Following that, all email notifications sent from your app will be routed to the MailSlurp Demo Inbox. You'll also be able to check your notifications for spam and preview them to see how they look in different email clients.

Notification Faking

Aside from MailSlurp, you can use the Notification facade's fake method. It simulates the mail delivery process and allows you to confirm whether an email would have arrived in the recipient's inbox. Check out how it works:

Various assertions are shown here to test the mail content. They are generated after the code under test has been executed.

Conclusion

Finally, how can you make the most of your email notifications? Email notifications should not be disabled in your app. They increase your customers' LTV as well as their retention rate. As a result, you must ensure not only that they work properly, but also that they are of high quality and valid. Test your notifications end-to-end using real email addresses and SMS sandboxes with MailSlurp.