Sending e-mail messages within your web application is an important function and is often the only way to get in contact with your customer. How often have you got some email message from a potential customer with an invalid email address? Sure you can use advanced regular expression patterns, but these patterns are not smart and didn’t check anything what is possible. In this short tutorial I will show you how to use the Email Address Validator provided by Mailgun. Using this tool each email address is validated by:

  • Syntax checks (RFC defined grammar)
  • DNS validation
  • Spell checks
  • Email Service Provider (ESP) specific local-part grammar (if available)

Where to start?

Email Address ValidatorThe Email Address Validator is a free service provided by Mailgun, a product from Rackspace. To run this tool you need an API key. Register a free Mailgun account on their website. With this account you can use the free Email Address Validator and you can send 10.000 email for free using their transactional email service. If you provide your credit card details (optional) you will get additional free features. They will charge your credit card only for usage above the 10.000 emails you get every month.

Setup your PHP example script

You can use their email validation service within different libraries: PHP, Java, Python, Ruby, C# and curl. There is also a jQuery plugin if you need email validation in your client side application. For this tutorial we will use the PHP library. If you follow the instructions from the documentation you have to use Composer to install the Mailgun PHP library. If you don’t like to use Composer, there is link to another version on this download page. In that case you need to upload the directory named “vendor” into your site’s root directory (this is where your site’s index.html file is located). Create a new file and add the following code:

The link to the autoload.php script need to be valid and you have to enter your API key. Safe your script and run the script in your browser. You will see this response:

You may have noticed that the validator service has provided a suggestion for email addresses hosted by popular email service providers. If you enter “[email protected]”, the response will suggest “[email protected]” as a possible valid email address. Play a bit with the example to see what kind of response you get for different entries. The following example will show different message on success or failure:

You can download the code from this blog post here. If you have any questions or comments, please post them using the comment form below. 

Pin It on Pinterest

Share This

Share this post with your friends!