Last active
August 29, 2015 14:12
-
-
Save velizarn/c9d190410743ae51b0b3 to your computer and use it in GitHub Desktop.
Email address validation with Mailgun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
function validate_email($email) | |
{ | |
$key = ''; // Setup Mailgun public key | |
if (function_exists('filter_var')) { | |
$validate_email = filter_var($email, FILTER_VALIDATE_EMAIL); | |
if (!$validate_email) { | |
return FALSE; | |
} | |
} | |
try { | |
$url = "https://api.mailgun.net/v2/address/validate?address=".urlencode($email)."&api_key=". $key; | |
$data = call_user_func(function($url){ if (!$data = @file_get_contents($url)) { throw new Exception('URL load failed'); } return $data; }, $url); | |
$res = (boolean) json_decode($data)->is_valid; | |
} catch (Exception $e) { | |
// echo 'Mailgun validation exception: '. $e->getMessage() .' / email: '. $email; | |
$res = TRUE; | |
} | |
return $res; | |
} | |
var_dump( validate_email('[email protected]') ); | |
/** | |
* If you are using PHP version lower than 5.3.0 you should use 'create_function' instead of native anonymous function. In this case you should replace this: | |
* | |
* $data = call_user_func(function($url){ if (!$data = @file_get_contents($url)) { throw new Exception('URL load failed'); } return $data; }, $url); | |
* | |
* with this code: | |
* | |
* $mailgunfunc = create_function('$url', 'if (!$data = @file_get_contents($url)) { throw new Exception(\'URL load failed\'); } return $data;'); | |
* $data = $mailgunfunc($url); | |
* | |
*/ | |
/* end of file */ | |
/* ---------------------------------- */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment