Laravel 8 Email Verification
Hi Developers,
I will explaine you step by step how to send verification email manually in laravel.
Laravel already provide email verification feature by default, but if you want to apply your own logic with manually email verification process then i will help you in this Simple Example.
Step 1: Laravel 8 installation
The very First, we need to get a fresh Laravel 8 version application using below command as we are going from scratch, So open your terminal and run the below command:
composer create-project --prefer-dist laravel/laravel email-verification
Step 2: Configuring database
In this step, we need to add database configuration details on the .env file. So let's create a username, password, etc.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=your_db_username
DB_PASSWORD=your_db_password
After added database configuration, you need to run default migration of Laravel by the following command:
php artisan migrate
Step 3:Email Configuration
Here, we need to add email configuration in the .env file. We are sending emails after user registration so we need to add email SMTP details for sending the email.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=yourpass
MAIL_ENCRYPTION=tls
Step 4: Create Auth
You have to follow a few steps to make auth in your laravel 8 application.First, you need to install laravel/ui package as like bellow:
composer require laravel/ui
Now you can use a quick way to create registration, login, and forgot password with routes by auth command, So simply run the below command to create:
php artisan ui bootstrap --auth
We need to wait for the installation to complete then compile the project’s assets using the command below:
npm install && npm run dev
Step 5: Model Preparation
Laravel application ships with a User model (the data structure in a database). By default, this model class does not implement the Illuminate\Contracts\Auth\MustVerifyEmail contract.
Therefore, our first step is to activate the MustVerifyEmail interface.
<? php
namespaceApp\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'email_verified_at'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Adding this interface allows you to send verification emails to newly registered users. Laravel also comes with a SendEmailVerificationNotification listener.
It is located at the App\Providers\EventServiceProvider and attached to Illuminate\Auth\Events\Registered. This event allows you to send a verification link to the user.
Step 6: Routing
Next, open the routes/web.php file and add:
Auth::routes(['verify' => true]);
This will add routes like email/verify and email/resend to our application.
Step 7: Adding the Email Verification Middleware
Next, open the app/Http/Controllers/HomeController.php file and add the verified middleware as follows:
class HomeController extends Controller{ public function __construct() { $this->middleware( ['auth', 'verified']); } }
That's it! We have now added email verification to our application. Go ahead and register a new account, you should be redirected to the following page that asks you to verify your email address before proceeding;we are ready to run our example so