Craig Francis


PHP Email Check

A function to check if an email address is valid, which also includes a DNS look-up to check that the domain name is valid.

function isemail($email) {
if (preg_match('/^\w[-.\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/', $email, $matches)) {
if (function_exists('checkdnsrr')) {
if (checkdnsrr($matches[1] . '.', 'MX')) return true; // If a 'mail exchange' record exists
if (checkdnsrr($matches[1] . '.', 'A')) return true; // Mail servers can fall back on 'A' records
} else {
return true; // For Windows
}
}
return false;
}

Please note that in some environments, like on the Windows OS, you might find that the DNS look-up is ignored, or might take a few seconds to time-out... so please test thoroughly.

Thank you for looking at this code, any feedback would be greatly appreciated. If you would like to use this code, please read the licence it is released under.