Laravel Mail or Notification? which one should use for email sending? and Why? - laravel

I'm new in laravel. I have to use email feature in my project. In Laravel Docs, I got Mail and Notification options for sending email feature. Now I want to know which one should be used and why?
Please Note: I will use ShouldQueue as well.

If you want to send the information via just one channel, say email only, then use Mailable. If you are likely to send the information as email or SMS, some other medium or multiple(eg. email and SMS), your best bet is to use Notification.

You should use Mailable, if each email you send out has different layout. Mailable is a very flexible way to send emails. And it's more customizable than Notification.
Notification is useful if you have to send a predefined layout in differents channel.
You can customize notifications layout, but it's not advisable as it's out of scope to have more than one layout of notification.

According to this laracasts video
"a general rule of thumb, what's nice about Notifications is you are notifying the user in response to some action that took place on the website, so they made a payment, they close their account they liked something these are all responses to an action."
I use the Mail facade for marketing, for example: to inform the new features in our website, inform some special day is coming soon, etc.

Related

How do I notify users on Slack that aren't in the channel?

What we are trying to do
I am working on automation which posts messages to a Slack channel using Incoming Webhooks on a custom Slack App. The messages mentions people.
What works
We can send a message just fine, it has formatted content, and usernames are correctly resolved using the link-names flag.
What isn't working
The whole point of the notification is to inform a dynamic set of people about something they should care about. The set of people we tag varies hugely (think people who contributed to a pull request) and so not all possible recipients are in the channel these automated messages go to.
We assumed that given the usernames are being directly #-mentioned, they would be notified by Slack. However, two of the users we've tested with and #-mentioned confirm they never received a notification they had been tagged.
This is different to "human" behaviour, where if you #-mention someone in Slack, you get a little message reminding you that person isn't in this channel and offers to invite them or let them know.
As far as we can tell, sending the message programmatically is doing the equivalent of "Do nothing" in the picture above. I want to do either of the other two options, preferably "Let them know".
How can I notify people they've been mentioned? I've looked at all the API documentation and nothing discusses notifying users who aren't in the channel that they are mentioned.
This can't be an uncommon issue.... right?
Notes:
We aren't directly calling chat.postMessage, it's just the only documentation on link_names I could find to link to. We are using Incoming Webhooks, which has minimal documentation on the parameters - it seems to be the same as chat.postMessage.
We would prefer not to move off Incoming Webhooks, but we can do a custom integration with the API if we have to.
You need to invite the user to the channel first, using the Python client that's:
client.channels_invite(
channel=channel_id,
user=user_id
)

is it possible to notify all users using Laravel notification with pusher?

Is it possible to create/send notification(s) to all users when there is new announcement.
I want all users to be notified using the database notification.
Since most of the tutorials I have seen are all email notification, is this possible? Please attach links or any idea on how I could implement this.
Literally in the documentation:
Alternatively, you may send notifications via the Notification facade.
This is useful primarily when you need to send a notification to
multiple notifiable entities such as a collection of users. To send
notifications using the facade, pass all of the notifiable entities
and the notification instance to the send method:
Notification::send($users, new InvoicePaid($invoice));
So yes, you can send to all user by getting them from the database and sending a notification using the facade.
For example:
Notification::send(User::all(), new InvoicePaid($invoice));

Use Laravel Notification for Email Verification

You can send notifications via email like this in Laravel...
<?php
public function toMail($notifiable)
{
$url = url('/invoice/' . $this->invoice->id);
return (new MailMessage)
->greeting('Hello!')
->line('One of your invoices has been paid!')
->action('View Invoice', $url)
->line('Thank you for using our application!');
}
However, is it a good approach (in software design) to use this feature to send verification emails upon user registration?
yes, it is a fast way to send notification, we register a greeting, a line of text, a call to action, and then another line of text. These methods provided by the MailMessage object make it simple and fast to format small transactional emails. The mail channel will then translate the message components into a nice, responsive HTML email template with a plain-text counterpart.
you can also formatting the notification in better way for example:
Error Messages
Customizing The Recipient
Customizing The Subject
Customizing The Templates
reference Laravel Reference
Laravel Notifications is an all new feature coming to Laravel 5.3 that allows you to make quick notification updates through services like Slack, SMS, Email, and more.
This is great. Notifications are so simple and robust, you may no longer find yourself needing to use any other notification tool (mail, Slack SDK directly, etc.)—especially when you see how many custom notification channels the community has created. It's bonkers.
As always, with great power comes great responsibility; make sure you're being careful with your users' time and attention and you don't go overboard with the notifications.
So, go forth. Notify.

Laravel 5.3 Notification Vs Mailable

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:
If I am only going to be sending emails notifications, is it
better for me to use Mailables instead of Notifications?
If each emails have different html layout, then would Mailable be
the better option?
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.
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.
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

How to know mail send or not send in laravel and which recipient is not getting it?

Hi I am using the bellow function, can you please guide me how can I use Mail::failures(); in it
Mail::send('emails.caregiversetprimary', $templateArray, function($message)use($email)
{
$message->to($email, 'username')->subject('my subject');
});
Mail::failures(); < ====== this gives me black array as I have used wrong email
I am using laravel 4.1
tl;dr
You can know which recipients the email has been sent to but no which recipients have received it.
details
You need a better understanding of how mail servers work. From a Laravel point of view, there is no way (or at least not a simple one*) of knowing which recipients got the email. Its a matter of how mail protocol works. You may know which recipients the message has been sent to but no which recipients actually got it.
With Mail::failures() you get a list of recipients to which Laravel tried to send the email but it failed on actually sending it. But again, if it has been sent there is no straightforward way to known if the mail reached their inbox or no.
*If you happend to use Mailgun, Mandrill or any other 3rd party software then you are not dealing with a mail server 'per se' but with an API service. May be you could check the mail service provider documentation to research if they perform any kind of delivery tracking that you can programatically check.

Resources