Table of contents

queue the laravel email verification

In my previous post, I shared about Laravel 8, and 9 email verification but the problem is that when registering an account is too low because we are not using queue on sending email for verification. Now we will implement the queuing on the Laravel 8 email verification so that the user experience is fast.

 

Step 1: Setup Laravel Queues

If you don't set up your Laravel queue follow these steps.

 

Run this command to your terminal project:

php artisan queue:table

 

Then once done. Run this command also.

php artisan migrate

 

Then update your .env file and look for QUEUE_CONNECTION then change the value from sync to database.

QUEUE_CONNECTION=database

 

Step 2: Create a notification named VerifyEmailQueued

Then run this command:

php artisan make:notification VerifyEmailQueued

 

Once done, navigate the generated class to app/Notifications/ then update the code with the following:

<?php

namespace App\Notifications;

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

class VerifyEmailQueued extends VerifyEmail implements ShouldQueue
{
    use Queueable;
}

 

Step 3: Create a custom method for your User.php model

We will add this method sendEmailVerificationNotification() to your User model so that we can customize the notification process. See the following code below:

/**
* Send the queued email verification notification.
*
* @param  string  $token
* @return void
*/
public function sendEmailVerificationNotification()
{
  $this->notify(new VerifyEmailQueued);
}

 

Then that's pretty much it. Now we run the following command to your terminal:

php artisan queue:work

 

queue the laravel email verification

 

Note: We only run queue:work manually when developing on local but in production, you must set up the supervisor to handle the queueing.

 

For more about Laravel queuing just visit their documentation.

 

Download Source Code

 

I hope it helps. Thank you for reading :)