I've done some more tweaking / testing on this function and it's getting closer. Here's what I have now:
function checkEmail($email) {
$HTTP_HOST = $_SERVER['HTTP_HOST'];
global $HTTP_HOST;
$result = array();
//echo "\$email: ".$email."<br />\n";
// checks proper syntax
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $email)) {
// gets domain name
list($username,$domain)=split('@',$email);
// checks for if MX records in the DNS
if(!checkdnsrr($domain, 'MX')) {
getmxrr($domain, $mxhosts);
foreach($mxhosts as $mxKey => $mxValue) {
echo ' '.$mxValue.'<br>'."\n";
}
$result[0] = "FAIL";
$result[1] = "Failed the checkdnsrr!";
} else {
if (getmxrr($domain, $MXHost)) {
$ConnectAddress = $MXHost[0];
foreach($MXHost as $mxKey => $mxValue) {
echo ' '.$mxValue.'<br>'."\n";
}
} else {
$ConnectAddress = $domain;
}
}
// attempts a socket connection to mail server, first on port 25
if(fsockopen ($ConnectAddress, 25 )) {
$Connect = fsockopen ($ConnectAddress, 25 );
if (ereg("^220", $Out = fgets($Connect, 1024))) {
fputs ($Connect, "HELO ".$HTTP_HOST."\r\n");
$Out = fgets ( $Connect, 1024 );
fputs ($Connect, "MAIL FROM: <{$email}>\r\n");
$From = fgets ( $Connect, 1024 );
fputs ($Connect, "RCPT TO: <{$email}>\r\n");
$To = fgets ($Connect, 1024);
fputs ($Connect, "QUIT\r\n");
fclose($Connect);
if (!ereg ("^250", $From) || !ereg ( "^250", $To )) {
$result[0] = "FAIL";
$result[1] = "Server rejected address, on port 25";
}
} else {
$result[0] = "FAIL";
$result[1] = "No response from server, on port 25";
}
} else {
$result[0] = "FAIL";
$result[1] = "Cannot connect to the e-mail server.";
}
} else {
$result[0] = "FAIL";
$result[1] = "This is not a valid e-mail address.";
}
return $result;
}
Just a couple problems...
1) It returns an error every time from AOL, even though I know that some of the addresses I'm testing w/ are valid. Does anyone know if there is something unique about testing the fsocket on AOL?
2) It returns a failure from Yahoo... sometimes. It's really strange. I can use my own address and continue to hit the submit button on this form I have out there w/ this function and it'll return an error about half of the time. Why wouldn't it work every time?
Thanks,
Shaun