PHP E-mail validation
From JumbaWiki
These sample scripts use a regular expression to check for a valid email address. You can define a constant for the regular expression like this:
<? define('EMAIL_REGEX',"^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"); ?>
The first function checks if the string passed to it is the correct format for an email address. It returns true if it looks valid, or false otherwise:
<? function EmailIsValid($EmailAddr) { // Simple email check just looking at the string format if(eregi(EMAIL_REGEX, $EmailAddr)) return true; else return false; } ?>
The next function performs the same test as above, and if the email address looks valid will try to contact the mail server before declaring the address ok.
<? function EmailIsValid2($EmailAddr) { $valid = false; if (eregi(EMAIL_REGEX, $EmailAddr)) { list($username,$domaintld) = split("@",$EmailAddr); if (getmxrr($domaintld,$mxrecords)) $valid = true; } else { $valid = false; } return $valid; } ?>
EmailIsValid2() uses the PHP built-in getmxrr() function. This function is not available on Windows servers, so to get around this you can define your own getmxrr() function:
<? if(!function_exists('getmxrr')) { // Replacement for getmxrr function on Windows server // "unable to fork" error usually means user IUSR_pcname needs read+execute perms on the exe function getmxrr($hostname, &$mxhosts) { $mxhosts = array(); exec('%SYSTEMDIRECTORY%\\nslookup.exe -q=mx '.$hostname, $result_arr); foreach($result_arr as $line) { if (preg_match("/.*mail exchanger = (.*)/", $line, $matches)) $mxhosts[] = $matches[1]; } return( count($mxhosts) > 0 ); } } ?>

