Laravel 7 Custom Email Verification Template

Categories: Development, General

I will be assuming that you have already got Laravel installed, setup and tour authentication is working. Otherwise, why would you be looking into customizing the email templates? 😁

The Laravel documentation for Email Verification will cover the setup by reading the documentation. But it stops just short of actually explaining how to customize the email template. I had spent a while trying to figure this out and found that I made it much harder on my self than it needed to be.

Much like customizing your Password Reset emails, we will start off by overwriting the sendEmailVerificationNotification method in our Users model.

public function sendEmailVerificationNotification()
{
    $this->notify(new VerifyEmailNotification());
}

Next create a new notification called VerifyEmailNotification.

php artisan make:notification VerifyEmailNotification

Opening up VerifyEmailNotification and rather than extending Notification, we will extend VerifyEmail.

Now toMail in our new notification will overwrite the default method allowing us to change the template.

In order for an email verification to work, we are going to need a unique URL for each user, this luckily can be returned from the parent VerifyEmail notification by calling $this->verificationUrl($notifiable);

Our completed toMail method will look like this.

public function toMail($notifiable)
{
    $verificationUrl = $this->verificationUrl($notifiable);

    return (new MailMessage)->view('emails.user.verify', ['url' => $verificationUrl]);
}

I opted to use the Blade templates for my emails, so I can output the URL on a CTA in the mail simply by writing {{ $url }}.

As I mentioned previously, I had trouble getting this to work as I expected. I wad incorrectly trying to trigger sendEmailVerificationNotification on the User model, which resulted in null values for the tokens.

The only way I found to trigger the notification was to create new test accounts.

I hope this helps you out, and if you know how to manually trigger the Verification notification then please let me know!


Adam Patterson

Adam Patterson

User Interface Designer & Developer with a background in UX. I have spent 5 years as a professionally certified bicycle mechanic and ride year-round.

I am a husband and father of two, I enjoy photography, music, movies, coffee, and good food.

You can find me on Twitter, Instagram, and YouTube!