In this tutorial I’m going to learn how to get email verification when someone register in laravel. Please follow some easy steps mentioned below. and will set up Mailtrap. We will use this service to test and send emails. Mailtrap simulates the actual SMTP server and delivers your emails to a test recipient.
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