Skip to content

Instantly share code, notes, and snippets.

@MikeRogers0
Created June 16, 2012 17:09
Show Gist options
  • Save MikeRogers0/2941972 to your computer and use it in GitHub Desktop.
Save MikeRogers0/2941972 to your computer and use it in GitHub Desktop.
How to validate an email address with PHP
<?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('real@hotmail.com'); // Returns TRUE
validEmail('fake@fakedomain.com'); // Returns FALSE
?>
@panpic
Copy link

panpic commented Sep 8, 2020

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= 'contact@panpic.com.vn';

if(mxrecordValidate($email)) {
echo('This MX records exists; I will accept this email as valid.');
}
else {
echo('No MX record exists; Invalid email.');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment