You may use Laravel events. Just have a look here https://laravel.com/docs/5.7/events
How to send emails / notifications from an laravel api
Hi there, How can we send emails/ notification from an laravel api to users in any event ( registration , new update, etc. ? I have used them in laravel web app before and it worked just fine but from laravel api , emails / notification wont be sent . Also my front end is (vue js) and mobile ( react native). Thanks.
Thanks for response. Using Event is another way of sending notifications . But you're saying by using this way when the api method gets hit, then it will trigger such mail event?
by this way when the api method gets hit, then it will trigger such mail event?
Yes, if you tell it to. You just need to trigger the event, and then the listener will do the work.
Event is another way of sending notifications
Event isn't just for sending notifications. You can do whatever you want in the event listener. If you're using queued events, then the work is done in the background and you can return the response immediately. Like instead of making the user have to wait until the email actually gets sent before they receive a response back (which can take a few seconds or longer), they receive the response immediately and the email is sent in the background.
Take the example in the docs that were linked to:
public function ship($orderId)
{
$order = Order::findOrFail($orderId);
// Order shipment logic...
event(new OrderShipped($order));
return $order;
}
So when the ship method is called, it finds an order and sends the order to an event. The event is then handled separately and the $order is returned.
Queued events are really best as those get processed by a queue worker in the background. Let's say you're allowing a user to upload a video, and as part of that process you want to do something to the video, like change the dimensions. That could take 20 minutes. You don't want your user to sit there waiting for that, so you use a queued event. User uploads the video and the video processing gets done in the background, and in the event listener you can send an email to the customer to let them know their video is finished processing (for example). So they're able to continue on and use the site (or leave it) while the video is being processed.
@Cronix tnx for the response, I meant above we can use event for sending notifications. Of course event is not just for one task. Now the question is why notification wont be sent without using event in an laravel api? I meant just create a email/ notification and call them in the the class/ method?
I have used them in laravel web app before and it worked just fine but from laravel api , emails / notification wont be sent .
Show your code that won't send a notification when using the api.
my Controller:
protected function registered(Request $request, $user)
{
$user->notify(new UserRegisterd($user));
}
my Notification class:
class UserRegisterd extends Notification
{
use Queueable;
public $user;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Welcome | To My App')
->markdown('emails-notifications.registered', ['user' => $this->user]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
@Cronix any thought?
Please or to participate in this conversation.