I found the answers on php.net in the user comments for
checkdnsrr and getmxrr.
function getmxrr($hostname, &$mxhosts)
{
$mxhosts = array();
exec('nslookup -type=mx '.$hostname, $result_arr);
foreach($result_arr as $line)
{
if (preg_match("/.*mail exchanger = (.*)/", $line, $matches))
$mxhosts[] = $matches[1];
}
return( count($mxhosts) > 0 );
}//--End of workaround
function checkdnsrr($hostName, $recType = '')
{
if(!empty($hostName)) {
if( $recType == '' ) $recType = "MX";
exec("nslookup -type=$recType $hostName", $result);
// check each line to find the one that starts with the host
// name. If it exists then the function succeeded.
foreach ($result as $line) {
if(eregi("^$hostName",$line)) {
return true;
}
}
// otherwise there was no mail handler for the domain
return false;
}
return false;
}
function validateEmail ($email)
{
global $SERVER_NAME;
$return = array (false, "");
list ($user, $domain) = split ("@", $email, 2);
$arr = explode (".", $domain);
$count = count ($arr);
if (($count> 2) and ($arr[$count - 2]=='com' or $arr[$count - 2]=='org' or $arr[$count - 2]=='net' or
$arr[$count - 2]=='edu' or $arr[$count - 2]=='mil' or $arr[$count - 2]=='k12'))
{
$tld = $arr[$count - 3].".".$arr[$count - 2] . "." . $arr[$count - 1];
} else {
$tld = $arr[$count - 2] . "." . $arr[$count - 1];
}
if checkdnsrr($tld, 'MX') {
{
if (getmxrr ($tld, $mxhosts))
{
for ($i = 0; $i < count ($mxhosts); $i++)
{
$fp = fsockopen ($mxhosts[$i], 25);
if ($fp)
{
$s = 0;
$c = 0;
$out = "";
set_socket_blocking ($fp, false);
do
{
$out = fgets ($fp, 2500);
if (ereg ("^220", $out)) {
$s = 0;
$out = "";
$c++;
} else if (($c > 0) && ($out == "")) {
break;
} else {
$s++;
}
if ($s == 9999)
{
break;
}
} while ($out == "");
set_socket_blocking ($fp, true);
fputs ($fp, "HELO $SERVER_NAME\n");
$output = fgets ($fp, 2000);
fputs ($fp, "MAIL FROM: <info@" . $tld . ">\n");
$output = fgets ($fp, 2000);
fputs ($fp, "RCPT TO: <$email>\n");
$output = fgets ($fp, 2000);
if (ereg ("^250", $output))
{
$return[0] = true;
} else {
$return[0] = false;
$return[1] = $output;
}
fputs ($fp, "QUIT\n");
fclose($fp);
if ($return[0] == true) {
break;
}
}
}
}
}
return $return;
}