📧Laravel Email Verification Tutorial Example (Step-by-Step)

Email verification is a crucial part of user registration for modern web applications. Laravel provides a built-in solution for verifying user emails during the registration process. In this tutorial, you’ll learn how to implement email verification in Laravel using the default authentication system.

📌 Prerequisites

Before starting, make sure you have the following:

  • Laravel project (v8+ recommended)
  • Composer installed
  • Database connection (MySQL, SQLite, etc.)
  • Mail configuration (SMTP, Mailtrap, Gmail, etc.)


✅ Step 1: Create a New Laravel Project

If you haven't created a project yet, create one using Composer:

composer create-project laravel/laravel email-verification-example

Navigate to your project directory:

cd email-verification-example

✅ Step 2: Set Up Authentication Scaffolding

If you are using Laravel 12 or above, use Laravel Breeze or Laravel UI.

For Laravel Breeze:

composer require laravel/breeze --dev php artisan breeze:install npm install && npm run dev php artisan migrate

For Laravel UI (Bootstrap-based UI):

composer require laravel/ui php artisan ui bootstrap --auth npm install && npm run dev php artisan migrate

✅ Step 3: Enable Email Verification in User Model

Open app/Models/User.php and make sure it implements the MustVerifyEmail interface:

use Illuminate\Contracts\Auth\MustVerifyEmail; class User extends Authenticatable implements MustVerifyEmail { // ... }

✅ Step 4: Protect Routes with Middleware

You must restrict routes so that only verified users can access them.

In routes/web.php:

Route::get('/dashboard', function () { return view('dashboard'); })->middleware(['auth', 'verified']);

✅ Step 5: Customize Email Verification Notice Page

Laravel automatically redirects unverified users to /email/verify.

You can customize the page in resources/views/auth/verify.blade.php.

<x-guest-layout> <div class="mb-4 text-sm text-gray-600"> {{ __('Thanks for signing up! Before getting started, please verify your email address.') }} </div> @if (session('status') == 'verification-link-sent') <div class="mb-4 font-medium text-sm text-green-600"> {{ __('A new verification link has been sent to your email address.') }} </div> @endif <form method="POST" action="{{ route('verification.send') }}"> @csrf <div> <x-primary-button> {{ __('Resend Verification Email') }} </x-primary-button> </div> </form> </x-guest-layout>

✅ Step 6: Setup Mail Configuration (Important!)

Edit .env file to configure your mail settings:

MAIL_MAILER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=your_username MAIL_PASSWORD=your_password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=noreply@example.com MAIL_FROM_NAME="Your App Name"


✅ Step 7: Test the Email Verification Flow

  1. Register a new user via /register route.
  2. Check your inbox for the verification email.
  3. Click the verification link in the email.
  4. Try accessing /dashboard, it will now be accessible only after email verification.


🔐 Optional: Force Logout If Email Not Verified

If you want to immediately logout unverified users, you can use middleware:

Create middleware:

php artisan make:middleware EnsureEmailIsVerified

In app/Http/Middleware/EnsureEmailIsVerified.php:

public function handle($request, Closure $next) { if (! $request->user() || ! $request->user()->hasVerifiedEmail()) { auth()->logout(); return redirect('/login')->with('error', 'Please verify your email before accessing the application.'); } return $next($request); }

Register in app/Http/Kernel.php:

'verified.custom' => \App\Http\Middleware\EnsureEmailIsVerified::class,

Use it in routes:

Route::middleware(['auth', 'verified.custom'])->group(function () { Route::get('/dashboard', function () { return view('dashboard'); }); });

🎨 Optional: Customize Email Verification Template

To customize the email content, publish the notifications:

php artisan vendor:publish --tag=laravel-notifications

Edit this file:

resources/views/vendor/notifications/email.blade.php

🧪 Troubleshooting

  • Mail not sending? Check SMTP credentials in .env
  • Email not verified error? Ensure MustVerifyEmail is implemented
  • No route error? Check middleware on your routes

Post a Comment

Previous Post Next Post

Contact Form