{"id":16018,"date":"2023-11-17T06:00:00","date_gmt":"2023-11-17T06:00:00","guid":{"rendered":"https:\/\/www.directimpactsolutions.com\/?p=16018"},"modified":"2025-05-01T03:08:38","modified_gmt":"2025-05-01T03:08:38","slug":"laravel-notifications-for-email","status":"publish","type":"post","link":"https:\/\/www.directimpactsolutions.com\/en\/laravel-notifications-for-email\/","title":{"rendered":"Laravel Notifications for Email"},"content":{"rendered":"<p>If you\u2019re looking into sending email from Laravel, you will most likely find information about <a href=\"https:\/\/laravel.com\/docs\/10.x\/mail\" target=\"_blank\" rel=\"noreferrer noopener\">Laravel Mail<\/a>, and rightly so, as it\u2019s a powerful way to send emails. In our experience, though, there\u2019s a simpler way to go about sending email, and it\u2019s the method we\u2019ll reach for first\u2013<a href=\"https:\/\/laravel.com\/docs\/10.x\/notifications\" target=\"_blank\" rel=\"noreferrer noopener\">Laravel Notifications<\/a>.<\/p><p>Not only are notifications simple, but using them will also help you standardize your system\u2019s outgoing communications. In my opinion, it\u2019s 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\u2019s dive right in.<\/p><h2 class=\"wp-block-heading\" id=\"What-is-a-Notification?\" style=\"font-style:normal;font-weight:400\">What is a Notification?<\/h2><p>To keep things simple, Laravel notifications are just basic messages.<\/p><p>We\u2019ll typically make a notification for each type of message that can be produced from a system (ex: UserRegistered, InvoicePaid, etc.).<\/p><h2 class=\"wp-block-heading\" id=\"Why-Use-Laravel-Notifications?\" style=\"font-style:normal;font-weight:400\">Why Use Laravel Notifications?<\/h2><p>Beyond their simplicity, there are a number of reasons to consider using notifications when you need to send an email, including the following:<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"Template-Driven-Designs\">Template-Driven Designs<\/h3><p>Your Laravel deployment already has built-in templates for mail notifications.<\/p><p>These templates cover both HTML emails and plain-text emails, and they look good without any modifications. I\u2019ve viewed some of these basic notifications on a number of different email clients, and I am impressed across the board.<\/p><p>If you\u2019re going to use standard templates, you don\u2019t 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.<\/p><p>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.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"Standardization-of-Communications\">Standardization of Communications<\/h3><p>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 <a href=\"https:\/\/laravel-notification-channels.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Laravel Notification Channels<\/a> website.<\/p><p>In my humble opinion, this makes notifications all the more valuable.<\/p><p>Once your project starts making use of Laravel notifications, they can standardize the way that all system communications are handled.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"Queueable\">Queueable<\/h3><p>Laravel notifications are designed to work with the <a href=\"https:\/\/laravel.com\/docs\/10.x\/queues\" target=\"_blank\" rel=\"noreferrer noopener\">Laravel Queues<\/a> 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\u2019t be stuck waiting for one of your processes to finish before they can move on.<\/p><p>For example, site administrators might want to be notified every time a new user registers. If you\u2019re the new user who just registered on a site, you shouldn\u2019t 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.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"Storable\">Storable<\/h3><p>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!<\/p><h2 class=\"wp-block-heading\" id=\"How-to-Use-Laravel-Notifications\" style=\"font-style:normal;font-weight:400\">How to Use Laravel Notifications<\/h2><p>Although the breadth of this topic could cover quite a bit, I want to illustrate how simple these notifications can be, so I won\u2019t go into a lot of depth.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"Creating-a-New-Notification\">Creating a New Notification<\/h3><p>Running the following from your command line (in the context of your project directory):<\/p><pre class=\"wp-block-code\"><code><code>php artisan make:notification YourNotificationName -m<\/code><\/code><\/pre><p>will create a new file <strong>YourNotificationName.php<\/strong> within <strong>app\/Notifications<\/strong> that has all of the basic scaffolding for you.<\/p><p>The structure of this file is relatively straightforward; it is comprised of two main types of functions. One of these function types is called <strong>via<\/strong>, and the others are named according to their notification channel type.<\/p><p>The <strong>via<\/strong> 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:<\/p><pre class=\"wp-block-code\"><code><code>return &#91;'mail'];<\/code><\/code><\/pre><p>Or perhaps you want your notification to be mailed out and logged to the database:<\/p><pre class=\"wp-block-code\"><code><code>return &#91;'mail','database'];<\/code><\/code><\/pre><p>Whatever channels you decide on should have a corresponding <strong>to<\/strong> function.<br>For example, if I\u2019m going to send this particular notification as an email, then I would need a <strong>toMail<\/strong> function:<\/p><pre class=\"wp-block-code\"><code><code>public function toMail($notifiable) { return (new MailMessage) -&gt;subject('Subject of the Email') -&gt;line('The introduction to the notification.') -&gt;action('Notification Action', url('\/')) ; }<\/code><\/code><\/pre><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"Sending-a-Notification\">Sending a Notification<\/h3><p>Laravel ships with a notifiable trait, and chances are, your users table is probably already using it.<br>If that\u2019s 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.<\/p><p>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:<\/p><pre class=\"wp-block-code\"><code><code>$user-&gt;notify((new YourNotificationName())<\/code><\/code><\/pre><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color\" id=\"Customizing-Templates\">Customizing Templates<\/h3><p>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:<\/p><pre class=\"wp-block-code\"><code><code>php artisan vendor:publish --tag=laravel-notifications<\/code><\/code><\/pre><p>Running this command will expose the templates inside your <strong>resources\/views\/vendor\/notifications<\/strong> directory.<\/p><h2 class=\"wp-block-heading\" id=\"Conclusion\" style=\"font-style:normal;font-weight:400\">Conclusion<\/h2><p>If you\u2019re like me, and try to keep your emails (and other notifications) short and sweet, then I doubt you\u2019ll need any more power than what Laravel notifications have to offer. I suggest you try them since they\u2019re so simple, and help spread the word about this under-appreciated feature of Laravel.<\/p>","protected":false},"excerpt":{"rendered":"<p>If you\u2019re looking into sending email from Laravel, you will most likely find information about Laravel Mail, and rightly so, as it\u2019s a powerful way to send emails. In our experience, though, there\u2019s a simpler way to go about sending email, and it\u2019s the method we\u2019ll reach for first\u2013Laravel Notifications. Not only are notifications simple, &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.directimpactsolutions.com\/en\/laravel-notifications-for-email\/\"> <span class=\"screen-reader-text\">Laravel Notifications for Email<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":7,"featured_media":16020,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","footnotes":""},"categories":[32],"tags":[],"class_list":["post-16018","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-applications"],"uagb_featured_image_src":{"full":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-scaled.jpg",2560,1706,false],"thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-150x150.jpg",150,150,true],"medium":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-768x512.jpg",768,512,true],"large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-1024x682.jpg",1024,682,true],"1536x1536":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-1536x1024.jpg",1536,1024,true],"2048x2048":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-2048x1365.jpg",2048,1365,true],"woocommerce_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-300x300.jpg",300,300,true],"woocommerce_single":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-600x400.jpg",600,400,true],"woocommerce_gallery_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2023\/11\/laravel-notifications-100x100.jpg",100,100,true]},"uagb_author_info":{"display_name":"Matt Leering","author_link":"https:\/\/www.directimpactsolutions.com\/en\/author\/matt-leering\/"},"uagb_comment_info":1,"uagb_excerpt":"If you\u2019re looking into sending email from Laravel, you will most likely find information about Laravel Mail, and rightly so, as it\u2019s a powerful way to send emails. In our experience, though, there\u2019s a simpler way to go about sending email, and it\u2019s the method we\u2019ll reach for first\u2013Laravel Notifications. Not only are notifications simple,&hellip;","_links":{"self":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/16018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/comments?post=16018"}],"version-history":[{"count":2,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/16018\/revisions"}],"predecessor-version":[{"id":19982,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/16018\/revisions\/19982"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media\/16020"}],"wp:attachment":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media?parent=16018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/categories?post=16018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/tags?post=16018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}