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:
Navigate to your project directory:
✅ Step 2: Set Up Authentication Scaffolding
If you are using Laravel 12 or above, use Laravel Breeze or Laravel UI.
For Laravel Breeze:
For Laravel UI (Bootstrap-based UI):
✅ Step 3: Enable Email Verification in User Model
Open app/Models/User.php
and make sure it implements the MustVerifyEmail
interface:
✅ Step 4: Protect Routes with Middleware
You must restrict routes so that only verified users can access them.
In routes/web.php
:
✅ 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
.
✅ Step 6: Setup Mail Configuration (Important!)
Edit .env
file to configure your mail settings:
✅ Step 7: Test the Email Verification Flow
-
Register a new user via
/register
route. - Check your inbox for the verification email.
- Click the verification link in the email.
- 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:
In app/Http/Middleware/EnsureEmailIsVerified.php
:
Register in app/Http/Kernel.php
:
Use it in routes:
🎨 Optional: Customize Email Verification Template
To customize the email content, publish the notifications:
Edit this file:
🧪 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