Simple Email Confirmation with Laravel

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.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=email@gmail.com
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=email@gmail.com
MAIL_FROM_NAME="Rob Askam"

Migration

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) {
        $table->timestamp('confirmed_at')->nullable();
        $table->string('confirmation_token')->nullable();
    });
}
public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('confirmed_at');
        $table->dropColumn('confirmation_token');
    });
}
php artisan migrate
Migrating: 2017_11_17_103232_add_confirmation_email_to_users
Migrated:  2017_11_17_103232_add_confirmation_email_to_users

Web.php

//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;
    $user->save();
    //Change this view to something you have
    return view('welcome');
 });

RegisterController.php

// 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]);
    }
 }

RegisterController.php

//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));
}

confirm-email.blade

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

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.

Categories: Posts