Friday, March 13, 2020

How to send Email Notification in Laravel

How to send Email Notification in Laravel


Hi Buddies

In this tutorial, I'm going to show you how to send email notifications in Laravel 6. We create notifications from Laravel 6 to the email address. We are sending an email to notify the user through the Laravel 6 notification system.

Laravel 6 notifications allow you to send emails and SMS and send loose message notifications to the user. In this example, I'm giving you a very simple way to create an initial email sending notification in Laravel 6. You can easily create notifications with the Laravel Artisan command. We can easily customize notifications such as email subject, email text, main action, etc. We almost have to use notifications when we are working on many projects like e-commerce. You may need to send a payment receipt notification, order receipt, invoice, etc.

In this example, we create an email notification and send it to a specific user about what we store in the database. Therefore, there are a few steps you need to take to create a basic example of a notification.

Step 1: install Laravel 6


I'm going to explain how to send email notifications in Laravel 6 step by step from scratch, so we need to download a new Laravel 6 application using the following command. So open your terminal OR prompt and run the following command:

composer create-project --prefer-dist laravel/laravel blog


Step 2: create a database table


In this step, we need to create a "notification table" using the craft laravel 5 command. So run the following command:

php artisan notifications:table

php artisan migrate


Step 3: create notification


In this step we need to create "notification" using the artisan laravel 5 command. So run the following command. We create MyFirstNotification.

php artisan make:notification MyFirstNotification

You can now see that a new folder is created as "Notifications" in the application folder. You need to make the following changes as in the following class.

app/Notifications/MyFirstNotification.php
<?php

   

namespace App\Notifications;

   

use Illuminate\Bus\Queueable;

use Illuminate\Notifications\Notification;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Notifications\Messages\MailMessage;

   

class MyFirstNotification extends Notification

{

    use Queueable;

  

    private $details;

   

    /**

     * Create a new notification instance.

     *

     * @return void

     */

    public function __construct($details)

    {

        $this->details = $details;

    }

   

    /**

     * Get the notification's delivery channels.

     *

     * @param  mixed  $notifiable

     * @return array

     */

    public function via($notifiable)

    {

        return ['mail','database'];

    }

   

    /**

     * Get the mail representation of the notification.

     *

     * @param  mixed  $notifiable

     * @return \Illuminate\Notifications\Messages\MailMessage

     */

    public function toMail($notifiable)

    {

        return (new MailMessage)

                    ->greeting($this->details['greeting'])

                    ->line($this->details['body'])

                    ->action($this->details['actionText'], $this->details['actionURL'])

                    ->line($this->details['thanks']);

    }

  

    /**

     * Get the array representation of the notification.

     *

     * @param  mixed  $notifiable

     * @return array

     */

    public function toDatabase($notifiable)

    {

        return [

            'order_id' => $this->details['order_id']

        ];

    }

}


Step 4: create route


In this step, we need to create routes to send notifications to a user. Open your "route / web.php" file and add the following route.

routes/web.php
Route::get('send', 'HomeController@sendNotification');


Step 5: create controller


Here we have to create a new HomeController controller that processes the route method generatePDF. So let's enter the following code.

app/Http/Controllers/HomeController.php
<?php

  

namespace App\Http\Controllers;

  

use Illuminate\Http\Request;

use App\User;

use Notification;

use App\Notifications\MyFirstNotification;

  

class HomeController extends Controller

{

    /**

     * Create a new controller instance.

     *

     * @return void

     */

    public function __construct()

    {

        $this->middleware('auth');

    }

  

    /**

     * Show the application dashboard.

     *

     * @return \Illuminate\Contracts\Support\Renderable

     */

    public function index()

    {

        return view('home');

    }

  

    public function sendNotification()

    {

        $user = User::first();

  

        $details = [

            'greeting' => 'Hi Artisan',

            'body' => 'This is my first notification from ItSolutionStuff.com',

            'thanks' => 'Thank you for using ItSolutionStuff.com tuto!',

            'actionText' => 'View My Site',

            'actionURL' => url('/'),

            'order_id' => 101

        ];

  

        Notification::send($user, new MyFirstNotification($details));

   

        dd('done');

    }

  

}


We are now ready to send the first notification to the user. So let's run our example to run the following command for quick execution:

php artisan serve

You can run the following URL:

http://localhost:8000/send

You can also send the following notifications:

$user->notify(new MyFirstNotification($details));

You can get notifications by sending the following command:

dd($user->notifications);

I hope this tutorial how to send email notifications in Laravel 6 could help you ...



0 comments:

Post a Comment

Please don't enter any spam link in the comment box.