[UPHPU] Advanced e-mail validation
Timothy Humphrey
timothy.humphrey at gmail.com
Tue Feb 6 17:13:35 MST 2007
Doug Roy wrote:
> Does anyone on this list use anything like this? or know of any existing PHP class that adds that extra level of e-mail checking?
I use this [1]. I didn't write it. In fact, I believe I got it from
someone who made it available on this list.
[1] ---------------------
function ValidateEmail($e,$v=-1) {
global $verbose;
/*
Return codes:
0: appears to be a valid email
1: didn't match pattern of a valid email
2: looks valid by the pattern, but no DNS records found
Try several things to make sure it is most likely valid:
1. preg_match it to make sure it looks valid
2a. If that passes, check for an MX entry for the domain
2b. If no MX, check for any DNS entry for the domain
*/
if ($v==-1) { $v=$verbose; }
if (!preg_match("/^[a-z0-9.+-_]+@([a-z0-9-]+(.[a-z0-9-]+)+)$/i",
$e, $grab)) {
return 1;
}
# $grab[0] is the whole address
# $grab[1] is the domain
$domain = $grab[1];
$cmds = array("host -t MX $domain 2>&1 ","host $domain 2>&1 ");
foreach ($cmds as $cmd) {
$dns = trim(`$cmd`);
if ($v) { print "n"; }
if (strpos($dns,"$domain mail is handled ")!==false ||
strpos($dns,"$domain has address ")!==false ||
strpos($dns,"$domain is an alias ")!==false) {
# It is valid
return 0;
}
}
# If it didn't return yet, it's invalid, even though it passed the preg.
return 2;
}
------------------------------
More information about the UPHPU
mailing list