
Laravel 5.8 Email Verification Example
The Laravel 5.8 has email verification can be implemented to ensure that users’ email addresses are valid and verified before they can access certain parts of your application. Here’s a step-by-step example of how to set up email verification in a Laravel 5.8 application:
- Create a New Laravel Project: If you haven’t already, create a new Laravel project using Composer:
composer create-project --prefer-dist laravel/laravel laravel_email_verification
- Configure Environment Variables: Open the
.env
file in your project’s root directory and configure your mail settings. You should specify your SMTP settings to allow Laravel to send email verification links:
MAIL_DRIVER=smtp
MAIL_HOST=your-smtp-host
MAIL_PORT=587
MAIL_USERNAME=your-email@example.com
MAIL_PASSWORD=your-email-password
MAIL_ENCRYPTION=tls
- Generate Authentication Scaffolding: Laravel provides a command to generate authentication scaffolding. Run the following command to generate the necessary files for registration and login:
php artisan make:auth
- Enable Email Verification: In Laravel 5.8, email verification is built into the authentication scaffolding. Open the
app/Http/Controllers/Auth/RegisterController.php
file and add theMustVerifyEmail
interface to theRegisterController
class:
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Auth\MustVerifyEmail; // Add this line
class RegisterController extends Controller implements MustVerifyEmail // Add this line
{
// ...
}
- Configure the User Model: Open the
app/User.php
file and make sure yourUser
model implements theMustVerifyEmail
interface:
use Illuminate\Contracts\Auth\MustVerifyEmail; // Add this line
class User extends Authenticatable implements MustVerifyEmail // Add this line
{
// ...
}
- Update the
web
Middleware Group: In theapp/Http/Kernel.php
file, add theverified
middleware to theweb
middleware group. This middleware ensures that users are redirected to the email verification notice if they haven’t verified their email address:
protected $middlewareGroups = [
'web' => [
// ...
\Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, // Add this line
],
// ...
];
- Run Migrations: Run the database migrations to create the necessary tables for email verification:
php artisan migrate
- Customize Email Templates (Optional): If you want to customize the email templates for email verification, you can publish them using the following command:
php artisan vendor:publish --tag=laravel-mail
This command will copy the email templates to the resources/views/vendor/mail
directory, where you can modify them as needed.
- Start the Development Server: Start the Laravel development server:
php artisan serve
Your application should now be running, and users will be prompted to verify their email addresses upon registration. Laravel will send a verification link to the user’s email address, and they can click on it to confirm their email.
That’s it! You’ve set up email verification in a Laravel 5.8 application. Users will now need to verify their email addresses before gaining access to protected parts of your application.