Laravel 8 Email Notification Send To User When User Email Verified - FreeCodeBlog

Last updated: Dec 11, 2021

In this tutorial we can learn how we can send a email when a user verified her email. We need to send email because email verified success email importance for registered user.

 

Laravel 8 Email Notification Send To User When User Email Verified
Laravel 8 Email Notification Send To User When User Email Verified.

 

Create Send Email Verified Notification.

Now let's start to develop main point. Open user model and edit for user email verification.

 

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens, HasFactory, Notifiable;
    protected $fillable = [
        'name',
        'email',
        'password',
    ];
    protected $hidden = [
        'password',
        'remember_token',
    ];
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

 

Next open web route and edit for email verification.

 

<?php

use Illuminate\Support\Facades\Route;

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

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

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

 

Now use verified middleware in home controller.

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth', 'verified']);
    }
    public function index()
    {
        return view('home');
    }
}

 

Open .env file and edit mail section for email sending. In this tutorial, I will use mailtrap for email notification sending.

 

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your-mailtrap-username
MAIL_PASSWORD=your-mailtrap-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=example@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

 

Copy below url and paste on any browser and hit enter.

 

127.0.0.1:8000/register

 

Connect wifi to your device and register a user. You will get an email verification notification email on mailtrap dashboard after registered.

 

Now I want to send a welcome email for email verification success.

 

Open EventServiceProvider and add verified event and Listener.

 

<?php

namespace App\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Events\Verified;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use App\Listeners\VerifiedUserEmail;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        Verified::class => [
            VerifiedUserEmail::class,
        ],
    ];

    public function boot()
    {
        //
    }
}

 

You need to run event command to generate event and Listener.

 

php artisan event:generate

 

Now make a notification to send after user email verified.

 

php artisan make:notification WelcomeNotification

 

Open VerifiedUserEmail Listener and add WelcomeNotification.

 

<?php

namespace App\Listeners;

use Illuminate\Auth\Events\Verified;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use App\Notifications\WelcomeNotification;

class VerifiedUserEmail
{
    public function __construct()
    {
        //
    }

    public function handle(Verified $event)
    {
        $event->user->notify(new WelcomeNotification());
    }
}

 

User will get two email notification. First when user will register second when user verified her email. 

 

Okay so this is for today. See you again next tutorials. Thanks

Post Tags
Post Writer
masum7323
Masum Biswas (@masum7323)

My name is Masum Biswas. I'm a full stack developer. I live in Bangladesh and I love to write tutorials and tools that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, MySQL, VueJS, Tailwind CSS and Bootstrap from the early stage.

Follow Me On
Related Posts
Use Moment Js In Laravel 8 With Vue 3 And Breeze Inertia

Laravel 8 breeze moment js with vue 3! Want to use moment js in laravel 8 vue 3 with laravel breeze inertia js? Yes! Okay sometimes you may need to... Read More

How To Install And Use Ckeditor 5 Plugins In Laravel 8 With Vue Js 3

You need to use ckeditor 5 (code block, text-alignment, text-highlight, block-quote, image-insert) plugins in laravel 8 with vue js 3 project.... Read More

How To Use Ckeditor 5 In Laravel 8 With Vue 3

Laravel 8, Vue 3, Ckeditor 5. Do you really want to use ckeditor 5 on laravel 8 with vue 3? Yes. Do you know about ckeditor 5? A little bit. Ckedit... Read More

How To Make A Laravel 8 Vue 3 Pagination To Use Multiple Pages

Sometimes you should make a pagination that will be use multiple pages in laravel 8 vue 3. Today I will make a laravel 8 vue 3 pagination for multi... Read More

Laravel 8 Jetstream Vue 3 File Download Example

Are you using laravel 8 jetstream vue 3 to download a file? Yes. Well, want to make a file download system in laravel 8 with jetstream vue 3? Yes.... Read More