How to use MailboxValidator Laravel Email Validation Package to validate email during registration

Overview

The MailboxValidator Laravel Email Validation Package is an easy to use Laravel package which enables Laravel users to validate emails with just a few lines of code. This package is using MailboxValidator API to do the validation, therefore you must sign up for the API key.  Just go to  https://www.mailboxvalidator.com/plans#api to sign up for the FREE API plan and you’ll be given an API key.

Installation

Once you have the API key, the next step is to install the package by using Composer. Open your terminal, navigate to your project root directory and type the following command :composer require mailboxvalidator-laravel/validationComposer will automatically install the package into your project. Depending on your network speed, you might need a few minutes to complete the installation. The service provider will be auto discovered. For Laravel versions below 5.5, you might need the additional step to make Laravel discover the service provider. Open the config/app.php and add the service provider manually into the providers section: MailboxValidatorLaravel\Validation\ValidationServiceProvider::class,

In the terminal, type the following command to publish the modified config file:

php artisan vendor:publish --provider=MailboxValidatorLaravel\Validation\ValidationServiceProvider --force

Usage

Next, we will need to modify the RegisterController.php to ensure that Laravel will use this package to validate the email when a user submit the registration. Open the RegisterController.php or any controller that handles the registration, and add ‘disposable’ into the Validator function to become like this: 'email' => 'email|disposable',

Finally, you need to add your MailboxValidator API key into your env file. Open your env file and add a new line with the field name ‘MBV_API_KEY’ and assigned your API key into it.

Now you can try to open your registration form and enter an disposable email address. An error message ‘This is a disposable email and should not been used to register.’ should appear after the submit button was clicked.

Was this article helpful?

Related Articles