Laravel queuing for email verification
The idea of queuing is to dispatch the processing of particular tasks, in our case, email sending, until a later time. This can speed up processing if your app sends large amounts of emails. It would be useful to implement email queues for the built-in Laravel email verification feature. The simplest way to do that is as follows:
- Create a new notification, e.g.,
CustomVerifyEmailQueued, which extends the existing one,VerifyEmail. Also, the new notification should implement theShouldQueuecontract. This will enable queuing. Here is how it looks:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Notifications\VerifyEmail;
class CustomVerifyEmailQueued extends VerifyEmail implements ShouldQueue
{
use Queueable;
}- Then override
sendEmailVerificationNotificationon theUsermodel, just like we did in the Customization block of Set up email verification in Laravel 5.7+.
public function sendEmailVerificationNotification()
{
$this->notify(new \App\Notifications\CustomVerifyEmailQueued);
}We did not touch upon configuration of the queue driver here, which is “sync” by default without actual queuing. If you need some insight on that, check out this Guide to Laravel Email Queues.
Set up email verification in Laravel using the laravel-confirm-email package
The laravel-confirm-email package is an alternative way to set up email verification in 5.8 and older versions of Laravel. It works, however, also for the newest releases. You’re likely to go with it if you’re looking for Laravel to customize verification of emails. For example, the package allows you to set up your own confirmation messages and change all possible redirect routes. Let’s see how it works.
Installation
Install the laravel-confirm-email package, as follows:
composer require beyondcode/laravel-confirm-emailYou also need to add two fields to your users table: confirmed_at and confirmation_code. For this, publish the migration and the configuration file, as follows:
php artisan vendor:publish --provider="BeyondCode\EmailConfirmation\EmailConfirmationServiceProvider"Run the migrations after:
php artisan migrateSetting up
We need to replace the default traits with those provided by laravel-confirm-email in the following files:
app\Http\Controllers\Auth\LoginController.php
- Default trait
use Illuminate\Foundation\Auth\AuthenticatesUsers;laravel-confirm-emailtrait
use BeyondCode\EmailConfirmation\Traits\AuthenticatesUsers;app\Http\Controllers\Auth\RegisterController.php
- Default trait
use Illuminate\Foundation\Auth\RegistersUsers;laravel-confirm-emailtrait
use BeyondCode\EmailConfirmation\Traits\RegistersUsers;app\Http\Controllers\Auth\ForgotPasswordController.php
- Default trait
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;laravel-confirm-emailtrait
use BeyondCode\EmailConfirmation\Traits\SendsPasswordResetEmails;Add the routes to app/routes/web.php:
Route::name('auth.resend_confirmation')->get('/register/confirm/resend', 'Auth\RegisterController@resendConfirmation');
Route::name('auth.confirm')->get('/register/confirm/{confirmation_code}', 'Auth\RegisterController@confirm');Error/confirmation messages
To set up flash messages that show up after a user clicks on the verification link, append the code to the following files:
resources\views\auth\login.blade.php
@if (session('confirmation'))
<div class="alert alert-info" role="alert">
{!! session('confirmation') !!}
</div>
@endif
@if ($errors->has('confirmation') > 0 )
<div class="alert alert-danger" role="alert">
{!! $errors->first('confirmation') !!}
</div>
@endif
resources\views\auth\passwords\email.blade.php
@if ($errors->has('confirmation') > 0 )
<div class="alert alert-danger" role="alert">
{!! $errors->first('confirmation') !!}
</div>
@endifCustomization
Updated the resources/lang/vendor/confirmation/en/confirmation.php file if you want to use custom error/confirmation messages:
<?php
return [
'confirmation_subject' => 'Email verification',
'confirmation_subject_title' => 'Verify your email',
'confirmation_body' => 'Please verify your email address in order to access this website. Click on the button below to verify your email.',
'confirmation_button' => 'Verify now',
'not_confirmed' => 'The given email address has not been confirmed. <a href=":resend_link">Resend confirmation link.</a>',
'not_confirmed_reset_password' => 'The given email address has not been confirmed. To reset the password you must first confirm the email address. <a href=":resend_link">Resend confirmation link.</a>',
'confirmation_successful' => 'You successfully confirmed your email address. Please log in.',
'confirmation_info' => 'Please confirm your email address.',
'confirmation_resent' => 'We sent you another confirmation email. You should receive it shortly.',
];You can modify all possible redirect routes (the default value is route('login')) in the registration controller. Keeping in mind that the app was automatically bootstrapped, the registration controller is at app/Http/Controllers/Auth/RegisterController.php. Just include the following values either as properties or as methods returning the route/URL string:
redirectConfirmationTo– is opened after the user completed the confirmation (opened the link from the email)redirectAfterRegistrationTo– is opened after the user submitted the registration form (it’s the one where “Go and verify your email now”)redirectAfterResendConfirmationTo– is opened when you ask to resend the email
By redefining the redirect routes you can change not only the flash message but also the status page which you show to the user.
I hope our quick guide on how to build Laravel email template and set up email verification in Laravel was useful for you. It was initially published in the Mailtrap Blog by Aleksandr Varnin.