It’s a common task but one that I haven’t looked at in as much detail as this devshed article. There’s a few bugs in the code, and a function was posted to the comments that’s all over the place.. here’s the same PHP function, cleaned up, to verify an email address. Note that it won’t work in Windows as-is because “checkdnsrr()” isn’t available on Windows platforms. (Remove the \ characters from before the ” characters surrounding the regex.. WordPress adds them when I post this. *grrr*)
function checkEmail($email)
{
// checks proper syntax
if( !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
{
return false;
}
// gets domain name
list($username,$domain)=split('@',$email);
// checks for if MX records in the DNS
$mxhosts = array();
if(!getmxrr($domain, $mxhosts))
{
// no mx records, ok to check domain
if (!fsockopen($domain,25,$errno,$errstr,30))
{
return false;
}
else
{
return true;
}
}
else
{
// mx records found
foreach ($mxhosts as $host)
{
if (fsockopen($host,25,$errno,$errstr,30))
{
return true;
}
}
return false;
}
}







