DEV Community

Cover image for How To Queue the Laravel 8 Email Verification?
codeanddeploy
codeanddeploy

Posted on

How To Queue the Laravel 8 Email Verification?

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-queue-the-laravel-8-email-verification

In my previous post, I shared about Laravel 8 email verification but the problem from 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
Enter fullscreen mode Exit fullscreen mode

Then once done. Run this command also.

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

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

QUEUE_CONNECTION=database
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a notification named VerifyEmailQueued

Then run this command:

php artisan make:notification VerifyEmailQueued
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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

php artisan queue:work
Enter fullscreen mode Exit fullscreen mode

how-to-queue-the-laravel-8-email

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.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-queue-the-laravel-8-email-verification if you want to download this code.

Happy coding :)

Discussion (0)