30 Sep 2013

Email validation with Mailgun's API [PHP]

I wrote this quick email validation class using Mailgun's API like a month ago. You can get it on github here: github.com/kehers/MG_Email. You will need a public API key from Mailgun though. Simply signup to get one.

What makes it interesting are the checks:

  • Syntax checks (RFC defined grammar)
  • DNS validation
  • Spell checks (gives you a did you mean suggestion)
  • Email Service Provider (ESP) specific local-part grammar (if available).

Usage is simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
include_once 'MG_Email.php';
$mailgun_public_api_key = 'your_mailgun_public_key';
$mg_email = new MG_Email($mailgun_public_api_key);

$email = 'someemail@yhoo.com';
if ($mg_email->is_valid($email)) {
    echo $email.' is valid';
}
else {
    echo $email.' is invalid.';
    if ($mg_email->spell_check)
        echo ' Do you mean '.$mg_email->spell_check.'?';
}
?>

My name is Opeyemi Obembe. I build things for web and mobile. You should follow me on Twitter (@kehers).