Consuming the MailboxValidator API using PHP

What is the MailboxValidator API?

The MailboxValidator API is an easy to use RESTful API which allows a registered user to validate a single email address to make sure that the email is valid. Programmers can leverage on this API to validate form entries whenever users sign up for an account to make sure the supplied email address is not fake or a disposable email. There are many uses for email validations as the MailboxValidator API can also determine if an email is from a free email provider or if it is a role-based email address among other things.

 

Getting started

First of all, an API key is needed to be able to utilize this API. So head on to the API plans page and sign up for the free API plan. Once you have done this, you’ll be assigned an API key. You’ll need to pass this key to the API during every query which we will demonstrate in our sample PHP code below.

 

API calling method via GET

Example of how to validate a single email address:

GET https://api.mailboxvalidator.com/v1/validation/single?email=test@test.com&key=<your API key>&format=<json or xml>

There are 3 parameters that the API will accept. The email parameter is mandatory and should be the email address you wish to validate. The key parameter is also mandatory and contains the API key you were assigned when you signed up for an API plan above. The last parameter format is optional so if you omit it, the default setting is to return the validation results in the form of a JSON string. You can also include this parameter with xml as the value if you want the results in an XML string.

 

Result fields

Some of the important fields that you should be aware of are listed below along with some explanations of their meanings:

  • is_free This will be True if the email is from a free email provider like Gmail. Otherwise it will be False.
  • is_syntax This will be True if the email syntactically valid. Otherwise it will be False.
  • is_domain This will be True if the email domain has a valid MX record in its DNS entries. Otherwise it will be False.
  • is_smtp This will be True if the mail servers specified in the MX record respond to connection attempts. Otherwise it will be False.
  • is_verified This will be True if the mail server indicates that the email address is valid. Otherwise it will be False.
  • status This is the most important field as a True value here means our system has deemed the email address to have passed all of our validations. If it’s False, you should not send any emails to that email address as there will be a high chance of bounce.

There are many other fields but we won’t cover them here. You can read more about them at the API documentation page.

 

Sample usage

Copy & paste the below PHP codes into your webpage and insert your api key as well as your test email address. Then you can try your page to call the API and display the results.

 

<?php
$apiKey = 'Enter_License_Key';
$params['format']           = 'json';
$params['email']     = 'Enter_Email';

$query = '';

foreach($params as $key=>$value){
    $query .= '&' . $key . '=' . rawurlencode($value);
}

$try = 0;
do {
    ////////////
    //For https request, please make sure you have enabled php_openssl.dll extension.
    //
    //How to enable https
    //- Uncomment ;extension=php_openssl.dll by removing the semicolon in your php.ini, and restart the apache.
    //
    //In case you have difficulty to modify the php.ini, you can always make the http request instead of https.
    ////////////
    $result = file_get_contents('https://api.mailboxvalidator.com/v1/validation/single?key=' . $apiKey . $query);
} while(!$result && $rty++ < 3);

$data = json_decode($result);

print_r($data);
?>

Was this article helpful?

Related Articles