Custom Email Verification in Laravel | Send email when register

In this tutorial I’m going to describe how to get email verification when someone register in laravel. Please follow some easy steps mentioned below.

First let’s go to install laravel project

composer create-project laravel/laravel mail-verification "5.8.*"

👇 Step 2 — Database Configuration

Setup database with your installed laravel 8 project . lets go to .env folder and put database name and connect to database.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel-mail
DB_USERNAME=root
DB_PASSWORD=

Next click on create

Next set up to mail trap for getting mail

Go to this URL — MailTrapio

Go to .env and put your mailtrap credentials

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=puthere-----
MAIL_PASSWORD=puthere-----
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

Now migrate the table

php artisan migrate

Now Create Auth

php artisan make:auth

Step 5: Email Verification Setup

Go to App/user.php file and paste below code

<?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',
];
}

Next go to Set-up Routes

Routes/web.php

Replace Auth:Routes to below code

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

Next go to HomeController.php file and paste below function code

public function __construct()
{
$this->middleware(['auth', 'verified']);
}

Now set-up is completed run below code

php artisan serve

Now page is look like this

Now you got successfully got verification email

Now also you’ll get email when register

Login successfully when you verify email

Thanks i hope its helpful for you….. if its working please hit comment and like 🙏🙏

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amit Kumar

Amit Kumar

Hi I am Amit Experienced Web Developer with a demonstrated history of working in the information technology and services industry.