Email Confirmation in 5 Minutes - No Packages!

September 23rd, 2021

Email confirmation is one of the things Laravel lacks out of the box. But it is really easy to set up. Here's How:

Are you confirmed?

First, we need a way to check if the User has confirmed their email. Why? Simply because if they have not we need to keep bugging them until they do.

To handle this super simply you can use one of two methods:
a) Add a confirmed column to the users table or b) add a confirmed_at timestamp to the users table.

We also need a custom token to uniquely identify the confirmation. We will use a 'confirmation_token' field

I prefer the later as it gives you that insight on when they confirmed and how long after they signed up that they confirmed.

So let's add confirmed_at and confirmation_token to the create_users_table_migration:

// create_users_table_migration
// ...

Now we add a route to web.php to update the confirmed_at field:

Route::get('/confirm/{token}', function ($token) {
  $user = User::whereConfirmationToken($token)->firstOrFail();
  $user->confirmed_at = now();
  // We should also set the confirmation_token to null
  $user->confirmation_token = null;
  return view('confirmed');

Now we need to edit the RegistrationController.php to populate the confirmation_token

// In the create method
return User::create([
  'name' => $data['name'],
  'email' => $data['email'],
  'confirmation_token' => str_random(64),
  'password' => bcrypt($data['password']),

Now we just need to send out a confirmation email when a user signs up

Send the Email Confirmation Email

We will use a ConfirmEmail Mailable to send the email:

php artisan make:mail ConfirmEmail --markdown=confirm-email

Now let's go to that file and add public $subject = "Confirm Your Email"; right below use Queueable, SerializesModels; and also add a protected constructor property for the user:

// after public $subject = "Confirm Your Email";
protected $user;

public function __construct($user)
  $this->user = $user;

Then pass the user to the markdown view:

public function build()
  return $this->markdown('confirm-email', ['user' => $this->user]);

That is it for ConfirmEmail.php.

Now we need to add a method to the RegisterController.php:

// RegisterController.php

// This is a method that gets called automatically after a user is registered
protected function registered(Request $request, $user)
  // Don't forget to import both these classes
  Mail::to($user->email)->send(new ConfirmEmail($user));

All that's left is adding the confirmation link to the confirm-email.blade.php file:

# Welcome to Our Service

Please Confirm Your email:

@component('mail::button', ['url' => url('/confirm/' . $user->confirmation_token)])

  {{ config('app.name') }}

And that is it! Go ahead and test it out.


This whole thing can be done in 5 mins or less, so it is a really easy way to add email confirmations to any Laravel app.

If you enjoyed reading this article please share it with your friends. And if you have any questions or topics you would like me to blog about please feel free to email me at sam@podlogar.com or hit me up on Twitter @sampodlogar.

© 2023 Mission-4 LLC