How to use MailboxValidator CakePHP Email Validation Plugin to validate email during registration

CakePHP Email Validation Plugin to validate email during registration
CakePHP Email Validation Plugin to validate email during registration

Overview

The MailboxValidator CakePHP Email Validation Plugin is an easy to use CakePHP plugin which enables CakePHP users to validate emails with just a few lines of code. This package is using the MailboxValidator API to do the validation, therefore you must sign up for a free API key.  Just go to  https://www.mailboxvalidator.com/plans#api to sign up for the FREE API plan and you’ll be given a free API key. After you get your API key, open your config/bootstrap.php and add the following line:Configure::write('MBV_API_KEY','PASTE_YOUR_API_KEY_HERE');

Installation

Once you have the API key, the next step is to install the plugin. If you prefer to manage the plugin by using Composer, open your terminal, navigate to your project root directory and type the following command :composer require mailboxvalidator/mailboxvalidator-cakephp

Composer will then install the plugin into your web application. If you want to manually install the plugin, clone the plugin repository from here first. Then extract and copy to the ‘plugins’ folder under your web project root directory. After that, open your config/bootstrap.php file and add this line:Plugin::load('MailboxValidatorCakePHP', ['bootstrap' => false, 'routes' => true, 'autoload' => true]);

Usage

Include this line in any form controller that handles validation:use MailboxValidatorCakePHP\Controller\MailboxValidatorController;

In any form validation method, before the $validator declare this line:$MBV = new MailboxValidatorController();

Add the below line right after the $validator:->provider('mbv', $MBV)

After that, add a new rule to your form field and edit the error message. For example, if you want to validate the disposable email, your rule will be like this:->add('email', 'disposable', [
'rule' => 'disposable',
'provider' => 'mbv',
'message' => 'Invalid email address. Please enter a non-disposable email address.',
])

The plugin provides three different validation method: disposable, single and free. Single Validation will validate the email address based on several factors, such as whether the email address contains high risk keywords or whether the email address is in our blacklist. Disposable validation will validate whether the email address belongs to a disposable email service provider or not. Free validation will validate whether the email address belongs to a free email service provider or not.

Now you can try to open your registration form and enter a disposable email address. The validator should return an error message once the submit button is clicked.

Was this article helpful?

Related Articles