Simple Email Confirmation with Laravel

Here is a simple tutorial to introduce and an email verification to your site. This reduces spam users and makes sure the data you are receiving is correct.

.env File

I will be sending my emails through gmail.


php artisan make:migration add_confirmation_email_to_users
Created Migration: 2017_11_17_103232_add_confirmation_email_to_users
public function up()
Schema::table('users', function($table) {
public function down()
Schema::table('users', function($table) {
php artisan migrate
Migrating: 2017_11_17_103232_add_confirmation_email_to_users
Migrated: 2017_11_17_103232_add_confirmation_email_to_users


//At the top add 
use App\User;
//Then add this route
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;
//Change this view to something you have
return view('welcome');


// Create a confirmation code in the user create method
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'confirmation_token' => str_random(64),
'password' => bcrypt($data['password']),

User Model

//Add the fields to your protected
protected $fillable = [
'id', 'name', 'email', 'password', 'confirmation_token', 'confirmed_at'

The Email

php artisan make:mail ConfirmEmail --markdown=confirm-email
Mail created successfully.
//In App/Mail/ConfirmEmail
namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ConfirmEmail extends Mailable
use Queueable, SerializesModels;

public $subject = "Please confirm your email address";
protected $user;

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

* Build the message.
* @return $this
public function build()
return $this->markdown('confirm-email', ['user' => $this->user]);


//At the top of the page add 
use Illuminate\Support\Facades\Mail;
use App\Mail\ConfirmEmail;
use Illuminate\Http\Request;
//With the functions add this
// This is a method that gets called automatically after a user is registered
protected function registered(Request $request, $user)
Mail::to($user->email)->send(new ConfirmEmail($user));


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

Further Reading

Using email can be a time consuming task that can slow down the end user experience, if you want to learn how to use Laravel Queues to delay the email until a less time consuming time read my other tutorial here.