I found a few things on various sites and combined them into one function that kinda works for me. I want a function that will check an e-mail address in the following ways:
1) Check that it’s in the correct format
2) Check that the MX record exists
3) Test that the alias/username is valid
So here is the function I came up w/:
[code]
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.”rn”);
$Out = fgets ( $Connect, 1024 );
fputs ($Connect, “MAIL FROM: <{$email}>rn”);
$From = fgets ( $Connect, 1024 );
fputs ($Connect, “RCPT TO: <{$email}>rn”);
$To = fgets ($Connect, 1024);
fputs ($Connect, “QUITrn”);
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