This simple function validates an e-mail address first by checking against a regular expression and second that the mail host exists. The DNS check fails if there is no MX record for the tested e-mail address. This function is an optimum validation for contact forms, CMS and member systems. Don't forget that the function "checkdnsrr" will not work on windows servers.
<?php
function check_email($mail_address) {
$pattern = "/^[\w-]+(\.[\w-]+)*@";
$pattern .= "([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/i";
if (preg_match($pattern, $mail_address)) {
$parts = explode("@", $mail_address);
if (checkdnsrr($parts[1], "MX")){
echo "The e-mail address is valid.";
// return true;
} else {
echo "The e-mail host is not valid.";
// return false;
}
} else {
echo "The e-mail address contains invalid charcters.";
// return false;
}
}
check_email("INFO@google.co.uk");
?>