Archives
Categories
Laravel

Make reset password email queue in laravel 5.7

By on November 27, 2018

Sending direct emails takes more time compared using a queue in Laravel. A queue is a feature integrated within Laravel which has its authentication feature which is generated by using the following command.

php artisan make:auth

After running this command, it created all scaffolding necessary for the authentication like register, login and forgot the password. For forgotten password email it’s use default mail feature which takes more time while sending an email. To solve this problem, we use Laravel queue feature in our application.

For this first of all create the reset password notification.

php artisan make:notification ResetPassword

And then place this code:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class ResetPassword extends Notification implements ShouldQueue
{
     use Queueable;

     public $token;
      /**
       * Create a new notification instance.
       *
       * @return void
       */
      public function __construct($token)
      {
          $this->token = $token;
      }
     /**
      * 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('Reset Password Notification')
             ->line('You are receiving this email because we received a password reset request for your account.')
             ->action('Reset Password', route('password.reset', $this-&amp;gt;token))
             ->line('If you did not request a password reset, no further action is required.');
      }

     /**
       * Get the array representation of the notification.
       *
       * @param mixed $notifiable
       * @return array
       */
     public function toArray($notifiable)
     {
        return [
          //
        ];
     }
}

After that import this class in User.php

use App\Notifications\ResetPassword as ResetPasswordNotification;

and place this method also

public function sendPasswordResetNotification($token)
{
     $this->notify(new ResetPasswordNotification($token));
}

Now every time while you request for new password. The reset password is in queue.

TAGS
RELATED POSTS