Laravel Notifications for Email

If you’re looking into sending email from Laravel, you will most likely find information about Laravel Mail, and rightly so, as it’s a powerful way to send emails. In our experience, though, there’s a simpler way to go about sending email, and it’s the method we’ll reach for first–Laravel Notifications.

Not only are notifications simple, but using them will also help you standardize your system’s outgoing communications. In my opinion, it’s a shame that Laravel notifications are not given the spotlight they truly deserve. There are a few other reasons why we suggest using notifications, so let’s dive right in.

What is a Notification?

To keep things simple, Laravel notifications are just basic messages.

We’ll typically make a notification for each type of message that can be produced from a system (ex: UserRegistered, InvoicePaid, etc.).

Why Use Laravel Notifications?

Beyond their simplicity, there are a number of reasons to consider using notifications when you need to send an email, including the following:

Template-Driven Designs

Your Laravel deployment already has built-in templates for mail notifications.

These templates cover both HTML emails and plain-text emails, and they look good without any modifications. I’ve viewed some of these basic notifications on a number of different email clients, and I am impressed across the board.

If you’re going to use standard templates, you don’t have to create a new file to represent the view of your email. You can simply invoke functions that determine what the subject line and content of your email will be. You can also customize the style of these templates.

If you need something more sophisticated than simply specifying a subject line, the text for the body, and an action button, these mail notifications can be expanded on via Markdown. The markdown available is pretty comprehensive, and provides components to handle more advanced tasks, like the display of a table.

Standardization of Communications

While this article is primarily focused on using notifications for sending email, it should be brought to light that notifications can serve purposes far beyond that. Laravel notifications can be used to send SMS text messages through Vonage or Twilio, send notifications into Slack, and accomplish other tasks. In fact, you can make your own notifications channel, or use ones that others from the community have created on the Laravel Notification Channels website.

In my humble opinion, this makes notifications all the more valuable.

Once your project starts making use of Laravel notifications, they can standardize the way that all system communications are handled.

Queueable

Laravel notifications are designed to work with the Laravel Queues system, and this goes a long way to provide a better UX for users of your site. When you take advantage of Laravel Queues, your users won’t be stuck waiting for one of your processes to finish before they can move on.

For example, site administrators might want to be notified every time a new user registers. If you’re the new user who just registered on a site, you shouldn’t have to wait until administrative emails are composed and sent before you can login. We can easily add our notification email to the queue, and it will be sent out as soon as possible.

Storable

This coincides with the Standardization Of Communications topic. Taking advantage of the fact that notifications can go to many different channels, there exists a notification channel for writing notifications to the database. This can come in handy if users of your system need to see which communications have gone out. This feature also makes it possible to resend notifications, too!

How to Use Laravel Notifications

Although the breadth of this topic could cover quite a bit, I want to illustrate how simple these notifications can be, so I won’t go into a lot of depth.

Creating a New Notification

Running the following from your command line (in the context of your project directory):

php artisan make:notification YourNotificationName -m

will create a new file YourNotificationName.php within app/Notifications that has all of the basic scaffolding for you.

The structure of this file is relatively straightforward; it is comprised of two main types of functions. One of these function types is called via, and the others are named according to their notification channel type.

The via function returns an array of all notification channels that it will be sending your notification into. You might keep it simple and just have it:

return ['mail'];

Or perhaps you want your notification to be mailed out and logged to the database:

return ['mail','database'];

Whatever channels you decide on should have a corresponding to function.
For example, if I’m going to send this particular notification as an email, then I would need a toMail function:

public function toMail($notifiable) { return (new MailMessage) ->subject('Subject of the Email') ->line('The introduction to the notification.') ->action('Notification Action', url('/')) ; }

Sending a Notification

Laravel ships with a notifiable trait, and chances are, your users table is probably already using it.
If that’s the case, Laravel will automatically know to check the corresponding database table for an email field to find out how to communicate with that user via email.

Assuming your user has an email address set, and your system is already configured properly to send email, then sending an email to that user should be as simple as:

$user->notify((new YourNotificationName())

Customizing Templates

If you want to change the way that the HTML/plain-text email notifications look, this can be done by simply publishing the corresponding resources:

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

Running this command will expose the templates inside your resources/views/vendor/notifications directory.

Conclusion

If you’re like me, and try to keep your emails (and other notifications) short and sweet, then I doubt you’ll need any more power than what Laravel notifications have to offer. I suggest you try them since they’re so simple, and help spread the word about this under-appreciated feature of Laravel.