PHP Classes
elePHPant
Icontem

PHP Email Verification using the MailboxLayer API - PHP Email Validation Quality package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Email Validation Quality PHP Email Validation Quality   Blog PHP Email Validation Quality package blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP Email Verificatio...  
  Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)  

Author: Dave Smith

Posted on:

Package: PHP Email Validation Quality

Checking an e-mail address for the proper format is the first and most common method used to determine if an address is valid.

To really put it to the test, we need to take it a step further using real time verification to check DNS records and SMTP servers while excluding known disposable e-mail providers.

While PHP provides some functions for the basic tasks, there is a need to implement more advanced verification to avoid using addresses that are not valid.

Read this article to learn how to implement more advanced verification methods using the MailboxLayer API and the PHP Email Validation Quality package.




Contents

Introduction

Checking for Proper Format

Verify that the Domain Can Receive Mail

Communicate with the Mail Server

Is this a Disposable E-mail Address?

Other Useful Package Features

Detecting a Catch-all Mail Server

Conclusions

mailboxLayer

Introduction

In this article, we will be exploring the various methods to validate an e-mail address. We will show the pure PHP solution, if available, along with a solution using the PHP Email Validation Quality package which uses an API provided by mailboxlayer.com.

The following code will be used for the PHP Email Validation Quality package which will return a response object that contains the data. This is the code to instantiate the package and verify an address, using the response will be discussed in the following chapters.

<?php

include( 'mailboxlayer.class.php' );
$mbox = new mailBoxLayer();
$email = 'user@domain.com';
if( $mbox->verifyMail($email) ===  false ) {
    //code to manage returned error
    die('Error code ' . $mbox->errorCode . ': ' . $mbox->errorText);
}
//use api response to validate email address

?>

Checking for Proper Format

The most basic validation check is to make sure the e-mail address supplied is properly formatted. There are several tests which can be performed using regular expressions, however, PHP standardized this starting with version 5.2 using the filter_var function. Using pure PHP, you could code something like this...

 $email = 'user@domain.com';
if( filter_var( $email, FILTER_VALIDATE_EMAIL) === false ){
    //code to manage improperly formatted e-mail address
    die('E-mail is not properly formatted');
}

Here we check the e-mail address using filter_var and the FILTER_VALIDATE_EMAIL constant which will return false if not properly formatted. PHP uses the RFC 822 standard, with the exception of comments and white space folding to determine proper formats.

Using the package, we would check the API response object like this...

if( empty($mbox -> response -> format_valid ) ){
    //code to manage improperly formatted e-mail address
    die('E-mail is not properly formatted');
}

It is important to note that the API is doing much more than just checking the format, so the response time can seem longer than expected. If you only need to check the format, you can speed up the process by turning off the advanced checking with a simple change to the verifyMail method...

$mbox -> verifyMail( $email, false);

The API uses the extended standards contained in RFC 5321 and RFC 5322 to determine proper formats.

Verify that the Domain Can Receive Mail

The DNS (Domain Name System) MX (Mail eXchange) record indicates if a particular domain is configured to receive mail. Using pure PHP, we can check this using the checkdnsrr function like this...

$email = 'user@domain.com';
list($user, $host) = explode('@', $email);
if( checkdnsrr($host, 'MX') === false ){
    //code to manage MX record not found
    die('DNS MX record not found');
}

Here we extract the host from the e-mail address and then check that the DNS MX record is configured. The checkdnsrr function will return false if the domain is not configured to accept mail at a specific address.

Note that the absence of a MX record from a domain does not mean cannot receive email. In that case the domain could receive messages in the address pointed A record but this is not usual, so it is normal that when a domain does not have MX records, it may not be configured to receive email.

Using the package, we would check the API response object like this...

if( empty($mbox -> response -> mx_found) ) {
    //code to manage MX record not found
    die('DNS MX record not found');
}

Communicate with the Mail Server

SMTP (Simple Mail Transfer Protocol) is used to transfer email across a network. PHP does not offer a simple function to communicate and test a domain's SMTP server, however we can check the API response object like this:

if( empty($mbox -> response -> smtp_check) ){
    //code to manage failed SMTP communication
    die('SMTP communication failed');
}

It is important to understand that a true response means that there was a successful communication with the SMTP server, however a false response does not necessarily mean that the e-mail address is not valid.

Some servers have the SMTP configured in a way that the communication test will fail while actual e-mails will get delivered, so trust a true response and treat a false response as just suspicious.

Is this a Disposable Email Address?

There are services which provide e-mail addresses that are used only once or very few times and are then removed. These are disposable e-mail addresses which allow prospective users of your site to receive a validation e-mail without providing a valid e-mail address.

Using the package, we would check the API response object to see if the service is a known disposable provider like this:

if( empty( $mbox -> response -> disposable) ) {
    //code to manage disposable e-mail address
    die('E-mail address is from a disposable provider');
}

Other Useful Package Features

Even if an e-mail address is valid, there may be cases that you want to exclude certain addresses from marketing campaigns for example.

You can check if an address is considered generic and used for a certain department or 'role'. Some examples of 'role' addresses would be support@domain.com, info@domain.com, abuse@domain.com and so on. The response will be true or false.

$mbox -> response -> role

You can check if an address is from a known free e-mail provider. The response will be true or false.

$mbox -> response -> free

You can check the quality of the address based on the API's quality score algorithm. The response will be a decimal percentage between 0.01 and 1 from lowest quality to highest.

$mbox -> response -> score

You can check for a common suggestion to an address in the event of a misspelling or typo. The response will be the suggested address.

$mbox -> response -> did_you_mean

You can retrieve the user part and domain part of the address provided to the API, respectively.

$mbox -> response -> user
$mbox -> response -> domain

Detecting a Catch-all Mail Server

Some mail servers are configured to accept e-mail regardless of the user part supplied, making notauser@domain.com a valid e-mail address even though there is no mailbox for it.

The feature to detect these catch-all servers is offered as part of the premium API accounts. It can be time consuming to perform the check, so the verifyMail method used in the package must explicitly turn on the catch all feature like this:

$mbox->verifyMail( $email, false, false, '', true);

Then we can check the API response to determine if the mail server is one of these catch-all servers.

$mbox -> response -> catch_all

Conclusion

The most common reason to verify an e-mail address is to ensure our members are real people or are hiding behind an identity that they do not want to reveal. If it is important that we have good and valid e-mail addresses, then we must take the necessary steps and perform validation beyond proper formatting.

Even though we have a valid e-mail address on file, we may have a need to exclude certain types of e-mail addresses from marketing campaigns to boost the chance of responses. Using quality scores and server types gives us the ability to better target our audience.

If you liked this article or you have questions about email verification with the PHP Email Validation Quality package, post a comment here.




You need to be a registered user or login to post a comment

1,411,262 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

2. Proper email is not proof of life - Chris (2016-03-17 14:17)
Return verification needed for an improved proof of validity.... - 4 replies
Read the whole comment and replies

1. mailboxlayer.com - Norman Ball (2016-03-17 10:19)
Requires an API Key... - 0 replies
Read the whole comment and replies




  Post a comment Post a comment   See comments See comments (6)   Trackbacks (0)  
  All package blogs All package blogs   PHP Email Validation Quality PHP Email Validation Quality   Blog PHP Email Validation Quality package blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP Email Verificatio...