Being a Laravel developer i feel like having a tutorials on how to enable and customize Laravel email verification. In this post, i am going to show you how to enable email verification and customize its template to send out a customized email for verifying sign up email.
Laravel is a very powerful framework, which comes with built-in packages which enables sending and verifying email verification when user signup.
- 1. Configure the mail
- 2. Enable Email Verification in Model
- 3. Add
verified
middleware - 4. Email verification with MailHog
- 5. Email Content Customization
- 6. Customizing The Email Template
1. Configure the mail
In first step we will configure mail setting in config/mail.php
path. You will see a config directory in root directory of Laravel project. In this directory, We have all settings Laravel needs to run smoothly. Laravel provides very neat, easy to use and understand email API which is powered by the famous Symfony Mailer component.
Alternatively, we can set configuration values in .env file which stands for envirnoment. Laravel try using values from .env file, but if we hard core values in config/mail.php file, these values superseeds .evn file values. In this tutorial, i will be using .env file to keep it simple and fast.
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=2025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
In Laravel Sail is configured with MailHog. We will see the MailHog after the Email Verification setup.
2. Enable Email Verification in User Model
To make email verification work, we need to enable it by implementing Illuminate\Contracts\Auth\MustVerifyEmail
in user model
Path to User model: app/Models/User.php
<?php
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;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens, HasFactory, Notifiable, HasRoles;
// ...
Now lets try registering a new user after making changes to User model.

When we complete registration process we will be getting the following error.

The reason for above error is our MAIL_FROM_ADDRESS is null. If you are using Laravel 8, you might now see above error, but in Laravel 9, its mandatory to set value for MAIL_FROM_ADDRESS either in .env file or config/mail.php file.
So lets add email address in the .env
MAIL_FROM_ADDRESS="hadaya@scratchcoding.dev"
After setting value for MAIL_FROM_ADDRESS
, we should be able register and redirected to the dashboard.

As you can see, after completing registration process, you are redirecting to home page. But we want user to verify email and then he/she should be able to visit home page. Right?
Lets restrict user from visiting home page untill he/she verify the email address.
3. Add verified
middleware
To restrict user from visiting home page or any landing page, we need to add verified
middleware to all the authenticated routes
Path: routes/web.php
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
Path: routes/admin.php
<?php
Route::group([
'namespace' => 'App\Http\Controllers\Admin',
'prefix' => 'admin',
'middleware' => ['auth', 'verified'],
], function () { //.....
Adding verified middleware to routes will force user to email verification page and show him/her a message to verify your pending email. Default template of this page has the option to resend verification email.

4. Email verification with MailHog

When Sail is running, Access your MailHog on http://localhost:8025/
The MailHog is an email testing tool for developers in local environments.
Check the received emails in MailHog. Now, click the email verification link. After verification user is allowed to verified
pages.


5. Email Content Customization
Laravel provides an option to modify the verification email content. In App\Providers\AuthServiceProvider
need to call toMailUsing
the method from the boot method.
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
// ...
VerifyEmail::toMailUsing(function ($notifiable, $url) {
return (new MailMessage)
->subject('Verify Email Address')
->line('Click the button below to verify your email address.')
->action('Verify Email Address', $url);
});
}
6. Customizing The Email Template
You can modify the HTML and plain-text template by publishing the notification package’s resources. Run the below command to copy the templates to your views.
php artisan vendor:publish --tag=laravel-notifications
The mail notification templates will be copied to the resources/views/vendor/notifications
folder.
The Laravel admin panel is available on https://github.com/balajidharma/basic-laravel-admin-panel. Install the admin panel and share your feedback.
Thank you for reading.
Stay tuned for more!
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Lang;
use Illuminate\Auth\Notifications\VerifyEmail as VerifyEmailBase;
class VerifyEmail extends VerifyEmailBase
{
// use Queueable;
// change as you want
public function toMail($notifiable)
{
if (static::$toMailCallback) {
return call_user_func(static::$toMailCallback, $notifiable);
}
return (new MailMessage)
->subject(Lang::getFromJson('Verify Email Address'))
->line(Lang::getFromJson('Please click the button below to verify your email address.'))
->action(
Lang::getFromJson('Verify Email Address'),
$this->verificationUrl($notifiable)
)
->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
}
}
laravel verification email
here I wroted:
https://medium.com/@axmedov/laravel-email-verification-during-registration-via-secret-key-9464a75be660