Skip to content

Instantly share code, notes, and snippets.

@leMaur
Last active August 16, 2019 06:44
Show Gist options
  • Save leMaur/e2df33954f36e98bd9de9cbcab34ee58 to your computer and use it in GitHub Desktop.
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
<?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