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()
Related
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?
I want to extend Magic Forms plugin to send Slack notifications at the same time with mail notification but I don't know if it is possible.
For my own plugin I'm using Slack for PHP library.
Yes I Guess you can, Just use composer for plugin add your package to plugin using composer.
then read this document https://skydiver.github.io/october-plugin-forms/docs/advanced/events/#docsNav
you need to use add Event Listeners and fire up your notification
public function boot() {
# Listen for Record after saved
Event::listen('martin.forms.afterSaveRecord', function (&$formdata, $component) {
// here use your slack lib and send notification
// $formData has all data
});
}
if any doubts please comment.
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.
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
I am trying to use Azure Notification Hubs to send push notifications to a client. I read this article which uses tags to identify each user.
https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-aspnet-backend-windows-dotnet-notify-users/
It does the work, but the number of tags is limited. I was thinking to store and use the Registration ID that the Hub returns.
Is there any way to send notifications using this ID?
Another way would be using the Channel.URI that is returned by WNS. Can this be implemented somehow?
Actually NH limits only number of tags per single registration but per hub you may have as many registrations as you need and each registration may have unique tag which you can use to route the notifications.
Also there is new Installation API for Notification Hubs which I believe fits better for you. It is still not well-documented but well-done and ready to use. Here you can find short description of how to use that API. Readme is about Java but .NET SDK has pretty much the same capabilities (in the end both call same REST API).
Keyword is TAG ! If you use any spesific tag for any registered device which is Android,IOS,Windows OS etc, you can send notification to any specific device.
To do these, you should follow below steps one by one ;
As Client side, register device using a spesific tag to selected Azure Notification Hub
Client Example for Android :
`/*you don't have to use Firebase infrastructure.
You may use other ways. It doesn't matter.*/`
String FCM_token = FirebaseInstanceId.getInstance().getToken();
NotificationHub hub = new NotificationHub(NotificationSettings.HubName,
NotificationSettings.HubListenConnectionString, context);
String registrationID = hub.register(FCM_token, "UniqueTagForThisDevice").getRegistrationId();
Like you see, we have used a unique tag call "UniqueTagForThisDevice" for selected Android device.
As Server Side, you should send notification using that TAG call "UniqueTagForThisDevice".
Server Example using Web API to send push selected Android device :
[HttpGet]
[Route("api/sendnotification/{deviceTag}")]
public async Task<IHttpActionResult> sendNotification(string deviceTag)
{
//deviceTag must be "UniqueTagForThisDevice" !!!
NotificationHubClient Hub = NotificationHubClient.CreateClientFromConnectionString("<DefaultFullSharedAccessSignature>");
var notif = "{ \"data\" : {\"message\":\"Hello Push\"}}";
NotificationOutcome outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif,deviceTag);
if (outcome != null)
{
if (!((outcome.State == NotificationOutcomeState.Abandoned) ||
(outcome.State == NotificationOutcomeState.Unknown)))
{
return Ok("Push sent successfully.");
}
}
//Push sending is failed.
return InternalServerError();
}
As last, you should call above Web API Service method using "UniqueTagForThisDevice" tag from any helper platform (Postman, Fiddler or anothers.).
Note : TAG doesn't have to be deviceToken or similar things. It just have to spesific for each devices. But I suggest you that, if you use WebAPI and it is related with Owin midlleware, you may prefer username as unique tag. I think, this is more available for application scenarios. In this way, you can carry sending notifications from unique devices to unique users ;)
That's all.