Normally when sending notifications in Laravel you send to a class with a Notifiable trait that defines "who" the notification is going to by the instance of the class. Such as with the User model.
I have a use case to send a notification to myself (the admin) and I don't have an entry in the User model.
Laravel does allow the use of the route() method using the Notification facade such as
Notification::route('email', 'admin#myapp.example')->notify(new SomeLaravelNotification());
However imo that's kinda verbose, is there a way to wrap the routing logic into the Notification class so I can just call something like
Notification::send(new SomeLaravelNotification());
Then I could have the routing logic as part of the SomeLaravelNotification class?
Related
As you know many e-mail providers have the feature to use e-mail templates. Through their API you can send e-mails, just by setting the template id and passing through the variables. The predefined template will be send to the user.
For my project I want to use Mailjet API for transactional e-mails. What is the best way to call their email API in my project. Because laravel already has lots of email and notification features so I am looking for the best practice to integrate this in my project.
For example I want so make use of notifications. This notification has to call the mailjet email api and pass through the template id and the needed variables. What is the best way to archive this. With a custom notification channel maybe?
Or are there other good alternatives?
You are right, you can use an alternative mailing service while using Laravel's neat notifications feature. You will need to create a custom notification channel where you can define sending logic by hitting Mailjet's API endpoints. When the notification channel is all set up, you can specify it inside a notification itself where you can define the exact request body.
To make things easier, you may use Mailjet's official PHP wrapper so you don't have to work with raw HTTP requests.
Personally I used the same technique for Twilio SMS and Nexmo (Vonage) SMS Verification codes. I will add some of my code so you can see how I organised things:
class PhoneChannel
{
public function send($notifiable, Notification $notification)
{
if ($notification->resend) {
$this->repeatCall();
} else {
$this->initCall();
}
}
...
class VerifyPhone extends Notification
{
use Queueable;
public bool $resend;
public function __construct(bool $resend)
{
$this->resend = $resend;
}
public function via($notifiable): array
{
return [PhoneChannel::class];
}
}
The point is that you can define a request body inside the notification itself in order to access it later in the notification channel. Just add a method called toMailjet inside a notification, specify your variables, and then access it inside a channel via $notification->toMailjet()
I'm building a SAAS and I want each tenant to have their own queue for notifications. I have a notification class that implements Illuminate\Contracts\Queue\ShouldQueue and I send the notification like this
$user->notify($notification);
But I haven't found a way to specify the queue that I want the notification to be pushed to. I know that jobs can be pushed to specific queues with onQueue:
ProcessPodcast::dispatch($podcast)->onQueue('tenant1');
But is it possible to do something like this for queueable notifications as well?
Since your notification should use the Illuminate\Bus\Queueable trait you can simply set the $queue property of the object. There's a helper function for it:
$notification->onQueue('tenant1');
$user->notify($notification);
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));
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
In a MVC web application, I often send emails.
I usually do it in the controller, as I load all my views from the controller (including email views).
Now, however, I have some code where the email sends from the model.
Which tier is email generally sent from? Does it matter? Does it need to be consistent?
A controller should ideally be like an operator that connects a view to a model. This either belongs in the model or service layer.
I would argue that this belongs in the Model layer only if you have a model object that is solely responsible for sending e-mails. You don't want to comingle presentation and logic, that's the whole point of separation of concerns in Model-View-Controller.
This type of logic should reside in a service layer. You could then use dependency injection to inject the service into the controller and call EmailSenderService.sendEmail();