Verify User in Laravel (send verification mail efficiently)

Dikshant Rajput
4 min readJun 23, 2021

In my previous blog, I have discussed about the optimisation of Laravel mails using Queue. In this blog, I will be telling about how you can verify a user by sending him verification mail through Laravel default Auth.

So Laravel provides a beautiful and a simple way to send verification mail and it itself generates the signature and everything so you don’t need to implement it b yourself. So let’s get started….

Laravel by default saves a field in users table named email_verified_at which is filled only when the user verifies his/her mail. If this field is null that means the account is not verified and if it have some date that means the user’s account is verified.

User Model

You just need to implement an interface for enabling the email verification when a user signs up. The User class blueprint will looks like this :

class User extends Authenticatable implements MustVerifyEmail{
}

Routes

You need to create 3 routes for verification purpose :

  1. Verification View :- This route will be a get route and as the name suggests it is the view or the html that the user will see after registration or if he tries to access other parts of application.

This route has to be named as “verification.notice”. It will look something like this :

Route::get('/email/verify',function(){    return view('email.verification-notice');})->middleware('auth')->name('verification.notice');

2. Verification Logic :- This route will also be a get route and as the name suggests all the logic for making user verify from checking the payload signature from the url and updating the email_verified_at field in db.

This route has to be named as “verification.verify” and it will look something like this :

Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {    $request->fulfill();    return redirect('/home');})->middleware(['auth', 'signed'])->name('verification.verify');

The callback function will validate the request and for that purpose a separate validation class is made by Laravel named EmailVerificationRequest. It will check for any error and if there is no error the fulfill() method will be called and the field in the db will be updated.

3. Verification Mail Resend :- If by-chance in any case the user misplaces the url or the email gets deleted you can give user a second option to verify their mail.

This route has to be named as “verification.send” and it will look something like this :

Route::post('/email/verification-notification', function (Request $request) {
$request->user()->sendEmailVerificationNotification();
return back()->with('message', 'Verification link sent!');
})->middleware(['auth', 'throttle:6,1'])->name('verification.send');

This will resend the verification mail with new signature.

Now whenever a user signs up (register) for your application a mail will be send automatically.

verification email example mail

There’s an issue with this approach that your application will take a lot of time to send the mail so as I told in my previous blog that we can use queue to save time and the mail will be sent in background using queue worker.

For this you have to create a job by using :

php artisan make:job VerifyEmailJob

Now you have to overwrite the sendEmailVerificationNotification() method for dispatching the VerifyEmailJob with user forsending verification email like this :

public function sendEmailVerificationNotification(){    VerifyEmailJob::dispatch($this);}

Inside App/Jobs/VerifyEmailJob, create a private variable named user and initialize the user variable in __construct() method.

use App\Models\User;private $user;public function __construct(User $user){    $this->user = $user;}

In handle method, send the mail using Laravel default VerifyEmail notification.

use Illuminate\Auth\Notifications\VerifyEmail;
public
function handle()
{ $this->user->notify(new VerifyEmail);}

Now the email will be pushed to queue and will be placed in jobs db table.

To execute them, run

php artisan queue:work 

Now the email will be sent in the background to the registered user and the execution time will be reduced and the efficiency of the application will improve and optimised.

You can find all the code here :

Hope you like the blog and please hit that clap icon and follow me for more such Laravel basics blogs…..

--

--