Laravel 5.8 Email Verification Step by Step

Laravel 5.8 Email Verification Step by Step

Learn how to implement email verification in Laravel 5.8 with detailed step-by-step instructions.

Introduction

Email verification is a crucial security feature that ensures users provide valid email addresses. This guide will show you how to implement email verification in Laravel 5.8 using the built-in verification features.

Prerequisites

  • Laravel 5.8 installed
  • Configured mail settings
  • Database setup
  • Basic understanding of Laravel authentication

1 Update User Model

Modify your User model to implement the verification contract:

use Illuminate\Contracts\Auth\MustVerifyEmail;

class User extends Authenticatable implements MustVerifyEmail
{
    // ... existing code ...
}

2 Add Verification Routes

Add the verification routes to your `routes/web.php`:

Auth::routes(['verify' => true]);

This adds the following routes:

GET /email/verify
GET /email/verify/{id}/{hash}
POST /email/resend

3 Configure Mail Settings

Update your `.env` file with mail settings:

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=hello@example.com
MAIL_FROM_NAME="${APP_NAME}"

4 Create Verification Notice View

Create a new view file `resources/views/auth/verify.blade.php`:

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Verify Your Email Address') }}</div>

                <div class="card-body">
                    @if (session('resent'))
                        <div class="alert alert-success" role="alert">
                            {{ __('A fresh verification link has been sent to your email address.') }}
                        </div>
                    @endif

                    {{ __('Before proceeding, please check your email for a verification link.') }}
                    {{ __('If you did not receive the email') }},
                    <form class="d-inline" method="POST" action="{{ route('verification.resend') }}">
                        @csrf
                        <button type="submit" class="btn btn-link p-0 m-0 align-baseline">
                            {{ __('click here to request another') }}
                        </button>.
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

5 Protect Routes

Add the verified middleware to routes that require email verification:

Route::get('/home', 'HomeController@index')->name('home')->middleware('verified');

6 Customize Email Template

Publish the notification views to customize the email template:

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

Edit the template at `resources/views/vendor/notifications/email.blade.php`

Key Features Covered

  • User model verification implementation
  • Verification routes setup
  • Mail configuration
  • Verification notice view
  • Route protection
  • Email template customization

Best Practices

  • Use queue for sending verification emails
  • Implement rate limiting for resend requests
  • Add proper error handling
  • Test email delivery in different environments
  • Keep email templates responsive

Common Issues

  • Mail configuration errors
  • Missing verification routes
  • Incorrect middleware usage
  • Email delivery failures
  • Template rendering issues