How to Send Activation Email in Laravel After User Registration

Laravel comes with an authentication flow when we install it. It includes login, registration, and forgot password flow in the Laravel core itself. Using this built-in flow, when users register they directly get access to the system. But what if someone wants to send an email verification code before allowing a user to use the application? In this article, we study how to send an activation email in Laravel.

We are going to send a confirmation email to the user. When a user clicks the verification link sent in an email then only he will able to use the system.

Getting Started

For getting started, we first install the package in our Laravel project. Open the terminal in your project root directory and run the command:

composer require beyondcode/laravel-confirm-email

This package adds email verification to your Laravel projects.

After installing the package, we should run a command to publish the migration and the configuration file.

php artisan vendor:publish --provider=BeyondCode\EmailConfirmation\EmailConfirmationServiceProvider

In the users table, this package adds two columns confirmed_at and confirmation_code. Let’s run the migration command to add these columns.

php artisan migrate

Configuration

To send an activation email we need to replace AuthenticatesUsers, RegistersUsers and SendsPasswordResetEmails traits with the ones provided by this package.

Open the below files in your editor first then we replace traits one by one.

  • app\Http\Controllers\Auth\LoginController.php
  • app\Http\Controllers\Auth\RegisterController.php
  • app\Http\Controllers\Auth\ForgotPasswordController.php

From the above files replace the statements

  • use Illuminate\Foundation\Auth\AuthenticatesUsers;
  • use Illuminate\Foundation\Auth\RegistersUsers;
  • use Illuminate\Foundation\Auth\SendsPasswordResetEmails;

With

  • use BeyondCode\EmailConfirmation\Traits\AuthenticatesUsers;
  • use BeyondCode\EmailConfirmation\Traits\RegistersUsers;
  • use BeyondCode\EmailConfirmation\Traits\SendsPasswordResetEmails;

As we are sending a verification link in an activation email, add the following two routes in a routes/web.php file.

Route::name('auth.resend_confirmation')->get('/register/confirm/resend', 'Auth\RegisterController@resendConfirmation');
Route::name('auth.confirm')->get('/register/confirm/{confirmation_code}', 'Auth\RegisterController@confirm');

When a user clicks on the verification link this package adds flash messages that contain error/information messages for users. To show this flash message to users add the below code in the resources\views\auth\login.blade.php.

.....
<div class="panel-body">
    @if (session('confirmation'))
        <div class="alert alert-info" role="alert">
            {!! session('confirmation') !!}
        </div>
    @endif
 
    @if ($errors->has('confirmation') > 0 )
        <div class="alert alert-danger" role="alert">
            {!! $errors->first('confirmation') !!}
        </div>
    @endif
.....

We also need to add the below code in the resources\views\auth\passwords\email.blade.php

.....
<div class="panel-body">
    @if ($errors->has('confirmation') > 0 )
        <div class="alert alert-danger" role="alert">
            {!! $errors->first('confirmation') !!}
        </div>
    @endif
.....

That’s it! Now when a user makes registration on your website, they will get an activation email to confirm their account. Please share your thoughts in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *