Laravel 5.3 Notification Vs Mailable
Asked Answered
M

2

54

I am a little confused about whether to use Laravel's Notification or Mailable class. From what I understand, Mailables are used to send only emails whereas Notifications can be used to send emails and sms. In my application, I dont have plans to send sms notifications for now, so I am confused if I should just use the Mailable class in this case. My questions are:

  1. If I am only going to be sending emails notifications, is it better for me to use Mailables instead of Notifications?

  2. If each emails have different html layout, then would Mailable be the better option?

  3. Even if all emails are Notification emails in nature, does it still make sense to send them using Mailables instead of Notifications?

Can someone tell me the main difference between these 2 and how should we decide on which method to choose when sending emails in Laravel 5.3.

Melmela answered 18/11, 2016 at 18:38 Comment(0)
A
22

Yes, definitively, if each email layout is different, you should use Mailable

Mailable is the new way to send emails, easier than before. More customizable than Notifications.

Notification is very nice if you want to send a predefined layout in differents channel ( Mail, SMS, Slack, etc )

You can customize notifications layout, but having 1 layout by notification is going to get more difficult... it is just not the use case for notifications

Apocrine answered 18/11, 2016 at 18:46 Comment(4)
I'd also like to mention that Notifications can be stored in the database automatically. If you're looking to keep a log of emails and don't want to write the extra logic, that's where Notifications would come in handy. But, storing an email in the database is not that hard.Nainsook
Thank you @Apocrine That clarifies my dilemma. I will accept your answer in 5 mins once it allows me.Melmela
It is true, and you can create your own driver to make your own notification, so it is quite powerful. Just depends on your use caseApocrine
@Apocrine It's quite late, but can you explain the last statement in your answer?Riobard
J
62

Although it is not in the documentation, as of Laravel 5.3.7, the Notifications mail channel can work with Mailable objects in addition to the notification MailMessage objects.

Therefore, you can create all your emails as Mailable objects, and if you decide to send them via Notifications, you would just have your toMail() method return the Mailable objects you've already made.

Jezebel answered 18/11, 2016 at 20:32 Comment(2)
that is great. This will also make changing over from Mailable to Notification in future date if required instead of a rewrite of those class.Melmela
one thing to keep in mind here is that you'll also have to pass the to address explicitly since it won't automatically pull it off the notifiable, like it does with MailMessageSingleton
A
22

Yes, definitively, if each email layout is different, you should use Mailable

Mailable is the new way to send emails, easier than before. More customizable than Notifications.

Notification is very nice if you want to send a predefined layout in differents channel ( Mail, SMS, Slack, etc )

You can customize notifications layout, but having 1 layout by notification is going to get more difficult... it is just not the use case for notifications

Apocrine answered 18/11, 2016 at 18:46 Comment(4)
I'd also like to mention that Notifications can be stored in the database automatically. If you're looking to keep a log of emails and don't want to write the extra logic, that's where Notifications would come in handy. But, storing an email in the database is not that hard.Nainsook
Thank you @Apocrine That clarifies my dilemma. I will accept your answer in 5 mins once it allows me.Melmela
It is true, and you can create your own driver to make your own notification, so it is quite powerful. Just depends on your use caseApocrine
@Apocrine It's quite late, but can you explain the last statement in your answer?Riobard

© 2022 - 2024 — McMap. All rights reserved.