Last active
August 16, 2019 06:44
-
-
Save leMaur/e2df33954f36e98bd9de9cbcab34ee58 to your computer and use it in GitHub Desktop.
Validate an email address by checking its syntax and if the given domain exists
This file contains hidden or 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 | |
if (function_exists(real_email)) { | |
/** | |
* Validate an email address by checking its syntax | |
* and if the given domain exists | |
* | |
* @param string $email The email address | |
* @return bool | |
*/ | |
function real_email(string $email, bool $debug = false): bool | |
{ | |
preg_match("/^(?:.*)@(.*)$/", $email, $matches);§ | |
$host = $matches[0]; | |
if ($host && ! filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
return false; | |
} | |
if ($debug) { | |
return true; | |
} | |
return checkdnsrr(idn_to_ascii($host), 'MX'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment