Step 1: Set up Mail Configuration

  1. Open your .env file and configure mail settings. Example using SMTP (like Gmail):

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_email_password_or_app_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
  1. Run php artisan config:clear to clear cached config.

Step 2: Create User Model and Migration

Laravel already has a User model. Make sure your users table has an email_verified_at column:

Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable(); // For email verification
$table->string('password');
$table->rememberToken();
$table->timestamps();
});

Step 3: Implement MustVerifyEmail

Laravel has built-in email verification. Update your User model:

use Illuminate\Contracts\Auth\MustVerifyEmail;

class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, Notifiable;

// ...
}

Step 4: Set up Auth Scaffolding

If you don’t have authentication scaffolding yet:

  1. Install Laravel Breeze (simple auth scaffolding):

composer require laravel/breeze --dev
php artisan breeze:install
npm install && npm run dev
php artisan migrate
  1. Breeze already includes registration and login forms.

Step 5: Update Routes for Email Verification

In routes/web.php, wrap the dashboard route with verified middleware:

use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\Request;Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

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

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

// Resend verification email
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');

Step 6: Registration Controller

If using Breeze, the registration controller already exists. When a user registers, Laravel automatically sends a verification email because the User model implements MustVerifyEmail.

Example:

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
public function store(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|confirmed|min:8', ]); > $user = User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => Hash::make($request->password), ]); Auth::login($user); return redirect()->route('verification.notice'); }

Step 7: Create Email Verification Blade

Create resources/views/auth/verify-email.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 by clicking on the link we just emailed to you.
</div>@if (session('message'))
<div class="mb-4 font-medium text-sm text-green-600">
{{ session('message') }}
</div>
@endif

<form method="POST" action="{{ route('verification.send') }}">
@csrf
<button type="submit">Resend Verification Email</button>
</form>
</x-guest-layout>

Step 8: Customize Email (Optional)

You can customize the email by creating a notification:

php artisan make:notification VerifyEmailNotification

Then update the notification in app/Notifications/VerifyEmailNotification.php and attach it to the User model:

public function sendEmailVerificationNotification()
{
$this->notify(new VerifyEmailNotification());
}

Categorized in:

Laravel,