How to Use Laravel Notification to Send Notification Mail
If you want to send any kind of Notification Mail Using Laravel Notification then this article is for you . In this article we will learn how can your Laravel Notification to send Mail with proper example .
Laravel makes it very easy to send a notification mail on completion of a perticular task or you can use according to your requirement . You just need to follow the step i have given and i'm again telling it's very easy but you just need to follow the steps carefully. So let's see how Laravel Notification works .
Laravel makes it very easy to send a notification mail on completion of a perticular task or you can use according to your requirement . You just need to follow the step i have given and i'm again telling it's very easy but you just need to follow the steps carefully. So let's see how Laravel Notification works .
Steps we will go Through :
- Setting Up Mail Configuration
- Using Laravel Notification
- Setting up Controller
- Testing whether working or not
Step 1 : Setting up Mail Configuration :
The very first step is to set up your mail configuration in your " .env " file . For this example i will be using Mailtrap.io For sending Mails . Make sure your " .env " mail configuration is same your Mailtrap configuration as shown in the following .
Project_name\.env.php :
MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=34d7af640c9818 MAIL_PASSWORD=d07708a6993e38 MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=0e3aa00606-f48da0@inbox.mailtrap.io MAIL_FROM_NAME="${APP_NAME}"
Mailtrap.io
Download Notification File :
First thing we need to create a notification class to use Laravel Notification . Use the following command to create a Laravel Notification file .
After using this command it will create your file ( TestNotification.php ) at " app\Notifications\TestNotification.php "
To use the above code you need to call your notification class as shown below
That's it now whenever the function of your controller will be called it will send a mail to your mailtrap .
You can the message detail in your " project_name\app\Notifications\FileName.php "
This is all about how can you use Laravel Notification to send Mail . Hope it Helped You .
Thank your for reading this article 😊
For any query do not hesitate to comment 💬
Types of Migration in Laravel
How to use multiple where condition in Laravel
how to Use Laravel 7 Auth
How to make Covid-19 Tracker
How to integrate Vue JS in Laravel
php artisan make:notification FileName
php artisan make:notification TestNotification
After using this command it will create your file ( TestNotification.php ) at " app\Notifications\TestNotification.php "
Project_name\app\Notifications\TestNotification.php :
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; class TestNotification extends Notification { use Queueable; /** * Create a new notification instance. * * @return void */ public function __construct() { // } /** * 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) ->line('The introduction to the notification.') ->action('Notification Action', url('/')) ->line('Thank you for using our application!'); } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ // ]; } }
Step 3 : Setting up Controller :
Now open your controller where you want to call the Notification . For example , in a review management system , you want send a mail to the user whenever the admin reply to a user's review. On your controller use the following code to call the notification to send emailUser::find(1)->notify(new TestNotification);
To use the above code you need to call your notification class as shown below
use App\Notifications\TestNotification;
Example :
responseController.php :
public function response(Request $req,$id) { $this->validate($req,[ 'response' => 'required|min:2|max:500', ],[ 'response.required' => ' Enter any response to comment ', ]); $review_detail=Review::find($id); $rid=$review_detail->id; $uid=$review_detail->uid; $cid=$review_detail->cid; $data=new Response; $data->rid=$rid; $data->uid=$uid; $data->cid=$cid; $data->reply=$req->response; $data->replyuid=Auth::user()->id; $data->save(); User::find($uid)->notify(new TestNotification); return redirect(url('/adminreviewdetail',$rid)); }
That's it now whenever the function of your controller will be called it will send a mail to your mailtrap .
Output :
You can the message detail in your " project_name\app\Notifications\FileName.php "
public function toMail($notifiable) { return (new MailMessage) ->line('Hello User') ->line('The Business admin has replied to your review'); }
This is all about how can you use Laravel Notification to send Mail . Hope it Helped You .
Thank your for reading this article 😊
For any query do not hesitate to comment 💬
Also Read :
Conversion of Array to String in LaravelTypes of Migration in Laravel
How to use multiple where condition in Laravel
how to Use Laravel 7 Auth
How to make Covid-19 Tracker
How to integrate Vue JS in Laravel