Custom verification email notification, Laravel 5.8

Lordgape Boss
May 14 · 2 min read

Professionalism means going the extra mile in making a difference. Have you ever longed to change the default verification email but don’t know how? In this post, I explained how you can achieve that.

While there are several ways one can achieve this goal, I think Stefan Bauer twitted a more elegant way to achieve our goal. Unlike twits, I aim to go into details.

Step 1: Create a mailable

php artisan make:mail EmailVerification --markdown=emails.verify-email

After running the above command, we get a mailable object in our Mail folder.

Figure 1. Mailable Classes

And also a markdown we can customize to meet our brand taste.

Figure 2. Customizable email template

Now, how do we tell Laravel to use our custom email template instead of its default template? We plug it in from notification callbacks.

Step 2: Tap into the AppServiceProvider hooks.

Figure 3. AppServiceProvider class

And swap in your mailable inside the boot method.

/**
* Bootstrap any application services.
*
*
@return void
*/
public function boot()
{
// Override the email notification for verifying email
VerifyEmail::toMailUsing(function ($notifiable){
$verifyUrl = URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
['id' => $notifiable->getKey()]
);
return new EmailVerification($verifyUrl, $notifiable); });
}

Notice I passed in both $verifyUrl and $notifiable to our EmailVerification mailable that means we have to add both parameters to EmailVerification’s constructor.

public $verifyUrl;
protected $user;
/**
* Create a new message instance.
*
*
@return void
*/
public function __construct($url,$user)
{
$this->verifyUrl = $url;
$this->user = $user;
}

It all makes sense now, our $notifiable was a user object. So we now have the recipient address and other vital info we may intend to use in our custom verify-email template.

That’s all to it!

Now I dub thee with two-star in commanding Laravel custom email notification.

Cheers!

Lordgape Boss

Written by

Experienced Vice President with a demonstrated history of working in the information technology and services industry.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade