How to use MailboxValidator CodeIgniter library to validate email?

Overview

In this tutorial, we will demonstrate how to use MailboxValidator CodeIgniter library to validate email addresses. MailboxValidator CodeIgniter library provides an easy way to use MailboxValidator API to validate email addresses. As this library is using the MailboxValidator API, you must obtain a free API key to use this library. 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 application/config/config.php and add this line:$config['mbv_api_key'] = 'PASTE_YOUR_API_KEY_HERE';

Installation

The installation of this library is very easy, just grab the library repository from here first. Then upload the libraries folder to your CodeIgniter application folder.

Usage

This library is for validating the email address. For example,  validate an email address called “testing@example.com” and get the validation result for that email address. To do so, first load the library in your controller and initiate it:$this->load->library('mailboxvalidator');

After that, you can retrieve the validation result for the email address like this:$result = $this->mailboxvalidator->get_single_result('test@example.com');

To pass the result to the view, just simply add the $result to your view loader like this:$this->load->view('YOUR_VIEW_NAME',$result);

And then in your view file, call the validation results. For example:echo $email_address;

You can refer the full list of response parameters available at here: https://www.mailboxvalidator.com/api-single-validation.

The library can also help with form validation. How to do so? First, create a new file called MY_Form_validation.php under application/libraries. Then, open the file and add the following codes:


<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

protected $ci;
public function __construct(){

$this->ci = &get_instance();
$this->ci->load->library('mailboxvalidator');
}

public function disposable($email) {
if ($this->CI->mailboxvalidator1->is_email_disposable($email) == true) {
// If is_email_disposable return true, means the email is disposable email
return false;
} else {
return true;
}
}

public function free($email) {
if ($this->CI->mailboxvalidator1->is_email_free($email) == true) {
// If is_email_free return true, means the email is free email
return false;
} else {
return true;
}
}

}

?>

Next, in your form controller, add the function name into the set_rules array. For example, if you want to use the disposable function to validate an email, just add the disposable into the set_rules array like this:$this->form_validation->set_rules('email', 'Email', 'required|disposable', array('disposable' => 'A disposable email address is detected.'));

Please note that you will be required to edit the custom error message for it. Now you can open your form and try to enter a disposable email address to see the outcome. The form should return the error message for the disposable email.

Now you can open your form and try to enter an invalid email address. An error message should be returned by the form.

Was this article helpful?

Related Articles