Laravel 5.7 Email Verification Tutorial With Example

install laravel, laravel install mysql, laravel get all data records, insert form data into database using laravel, laravel crud example, laravel custom 404, laravel registration and login, laravel 5.7 email verification, laravel 5.8 crud, laravel email verification, laravel facebook login, laravel google login, laravel file upload, laravel image upload, laravel image upload validation, laravel file upload validation, laravel form validation, laravel one to one relationship

Read about Laravel 5.7 Email Verification Tutorial With Example.

In this tutorial, you will learn to create registration process with email verification feature in laravel 5.7.

Laravel 5.7 comes with rich built-in authentication feature which requires very few tasks from developer to create high security level authentication for any laravel web application.

We will use mail trap to manage verification emails.

If you need an authentication system without email verification feature, read Laravel Registration and Login Example.

First of all, see the below video which contains the output of this laravel 5.7 email verification example

Now follow all the below steps.

Move 1. New Project

Create a new project with the below command

laravel new laraMailVeri

Move 2. Enable Authentication

In the terminal trigger the following command

php artisan make:auth

Above command will generate the authentication scaffolding means that it will generate necessary files to enable authentication (registration, login, logout etc) in our laravel app.

  • Navigate to Http->Controller->Auth directory.
  • You will find some php files like RegisterController.php, LoginController.php, ForgotPasswordController.php etc. inside Auth folder.
  • These are programming logic related files.
  • To see view blade files navigate to resources->views->auth directory.
  •  Inside auth folder, you will find some blade files like login.blade.php, register.blade.php etc.

Move 3. Making a Database

Second task is to create a database for our project.

  • I am using sequel pro for database management. If you are using phpMyAdmin or other one than don’t worry, process will be the same.
  • Make a new database and give it a name like “laraMailVeri” in sequel pro or phpMyAdmin or any other tool.
  • Now we need to connect this newly created database with our laravel project.

So in your project, open the .env file. In this file, update the below code lines as per your configuration.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laraMailVeri
DB_USERNAME=root
DB_PASSWORD=

I have not set any password so I have set it as empty. Similarly, edit other fields as per your machine.

Now run the below command in terminal.

php artisan migrate

Above command will create necessary tables in our database.

So you just need to create database manually, tables and their fields will be created automatically.

Move 4. Changing User.php file

  • Open the app->User.php file.
  • By default this file do not implements MustVerifyEmail class. We need to do it manually.
  • So just add “implements MustVerifyEmail” at the first line of class definition.

Final source code for User.php file is as the below

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

Move 5. Adding Routes

Time to add some routes in the web.php file.

  • Open the routes->web.php file. Here, you may find something like ” Auth::routes(); “
  • Replace it with the below line
Auth::routes(['verify' => true]);
  • Now there is also another line like ” Route::get(‘/home’, ‘HomeController@index’)->name(‘home’) ” 
  • Replace this line with the following
Route::get('/home', 'HomeController@index')->name('home')->middleware('verified');

So the final source snippet for web.php file is like the below

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

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

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

Move 6. Mail Demo

Finally, let us send the verification e-mail. I am using mailtrap to manage verification mails.

  • Sign in to mail trap with this link https://mailtrap.io
  • After signing in to mail trap, we need to connect it with our laravel app. You can find information like username, password, port etc. on the mail trap dashboard.
  • We need to put this information in .env file.

Open your .env file and find the below lines

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=116ff5545b3a68
MAIL_PASSWORD=1e27cb61c6ca56
MAIL_ENCRYPTION=null
  • Here, in the above snippet, update information as per your mail trap dashboard.
  • After this muck of work, run your application using “php artisan serve” command.

Now you should be able to run your authentication with email verification feature.

Download Source Code for Laravel 5.7 Email Verification

Visit me to download Full Source Code