How to queue the user verification email in Laravel

Background jobs in Laravel are a powerful feature that I tend to use a lot. Ten years ago, that would have sounded impossible. But Laravel makes is so easy.

From external API calls to sending emails, importing files… By delegating long processes to a side runner, it helps smoothing the user experience.

Laravel 5.7 added a nice feature which is user email verification. Pretty much every project that I’ve been working on implement this feature. User email verification is a requirement for many web apps, so I think it’s a good addition to the framework.

Enough said, let’s move on to the actual code!

First off, let’s start by generating a notification class by running:

artisan make:notification Auth/VerifyEmail

Then, trim out the boilerplate code, as we won’t need it, and import the core Laravel class (which we are going to extend in order to add the Queuable trait and the $queue property).


namespace App\Auth\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Auth\Notifications\VerifyEmail as VerifyEmailNotification;
use Illuminate\Contracts\Queue\ShouldQueue;

class VerifyEmail extends VerifyEmailNotification implements ShouldQueue
    use Queueable;
    public function __construct()
        $this->queue = 'email';

You may not need to specify on which queue the email would be sent upon, but I personally prefer to separate concerns as adding more queueable jobs will eventually make you do that anyway. As always, it all depends on the scope of your project.

Then, overwrite this method on your User model:

 * Send the email verification notification.
 * @return void
public function sendEmailVerificationNotification()
    $this->notify(new \App\Notifications\Auth\VerifyEmail);

Now, you may need to run a worker to check if everything’s playing out.

php artisan queue:work --queue=email

That’s it, happy hacking!

Published by Ben Villiere

Software Developer since 2007. I enjoy solving complex problems with efficient code. My mission is to help people improve their professional life by automating mundane tasks. This blog is my technical journal where I write about my work.

Follow me on: | |