Created
June 16, 2012 17:09
-
-
Save MikeRogers0/2941972 to your computer and use it in GitHub Desktop.
How to validate an email address with PHP
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 | |
function validEmail($email){ | |
// Check the formatting is correct | |
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){ | |
return FALSE; | |
} | |
// Next check the domain is real. | |
$domain = explode("@", $email, 2); | |
return checkdnsrr($domain[1]); // returns TRUE/FALSE; | |
} | |
// Example | |
validEmail('[email protected]'); // Returns TRUE | |
validEmail('[email protected]'); // Returns FALSE | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function mxrecordValidate($email){
list($user, $domain) = explode('@', $email);
$arr= dns_get_record($domain,DNS_MX);
if($arr[0]['host']==$domain&&!empty($arr[0]['target'])){
return $arr[0]['target'];
}
}
$email= '[email protected]';
if(mxrecordValidate($email)) {
echo('This MX records exists; I will accept this email as valid.');
}
else {
echo('No MX record exists; Invalid email.');
}