Laravel email verification: A step-by-step guide

Register function is considered as one of the most important features among apps. Thus, signing up with email is not an exception and to get email verification functions in Laravel, admins need to implement into their project apps some methods and email verification is one of those.

In the Laravel framework, Rather than forcing you to re-implement this feature by hand for each application you create, Laravel provides built-in services for sending and verifying email verification requests.

There are 5 simple steps to do this so let it start.

Step 1: Model preparation.

Before getting started, Laravel demands users to have some set up.

After you get your project created the first you need to go into your User model by using this path below:

App\Models\User

Then use this method name MustVerifyEmail, you can import this one with this code:

use Illuminate\Contracts\Auth\MustVerifyEmail;

Next, implements this method into your User class by using implements code, you can do it like this:

<?php
 
namespace App\Models;
 
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
 
class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;
}

Remember to use Notifiable to enable Laravel methods.

Step 2: Database preparation and send-email code.

To use this method please go to your Database and check to make sure you got the email_verified_at column ready. If not, please go check how to add another column into a table and migrate the table to create one. You can check in here:

Next, to get the email send you need to add some code into your register function by using this code right below:

use Illuminate\Auth\Events\Registered;
 
event(new Registered($user));

Step 3: Routing and response handle.

To properly implement email verification, three routes will need to be defined.

First, a route will be needed to display a notice to the user that they should click the email verification link in the verification email that Laravel sent them after registration.

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

Second, a route will be needed to handle requests generated when the user clicks the email verification link in the email.

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

Third, a route will be needed to resend a verification link if the user accidentally loses the first verification link.

use Illuminate\Http\Request;
 
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');

Laravel is not forcing you to use all this routing as exact but you can modify it to fit your project.

You can also change the path to head the link wherever you want.

Step 4: Email preparation for Laravel

In the send-email function then of course you need an email to do that stub for you so go ahead and get your email ready. You can do as below:

1/ Register a Gmail account.
2/ Go to to setting part and go to your google profile
3/ Go to securities
4/ Create your apps password to use Laravel email-send method. (you need to have 2 factor security to be able to create this password)
5/ Remember your password and add it into your .env file like below:
MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=Your email username
MAIL_PASSWORD=Your apps password
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME="Name"

Step 5: Re-sending email with Laravel

Sometimes a user may misplace or accidentally delete the email address verification email. To accommodate this, you may wish to define a route to allow the user to request that the verification email be resent. 

To do this you can simply use Laravel default blade, “notice.blade.php” or just call the sending email function abow:

use Illuminate\Auth\Events\Registered;
 
event(new Registered($user));

Step 6: Email pattern customization.

To do this, you simple go to your AuthServiceProvider through this path App\Providers\AuthServiceProvider, then add:

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;

Next, use this code below and then change it to anything you want. The mail will be send to your as exact pattern that you have customized:

VerifyEmail::toMailUsing(function ($notifiable, $url) {
        return (new MailMessage)
            ->subject('Verify Email Address')
            ->line('Click the button below to verify your email address.')
            ->action('Verify Email Address', $url);
    });

That is it for email verification with Laravel, thanks for reading and enjoy the code.

Leave a Comment

Your email address will not be published. Required fields are marked *