/    Sign up×
Community /Pin to ProfileBookmark

validating / testing e-mail address

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 ‘&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;’.$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 ‘&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;’.$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;
}
[/code]

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

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 06.2006 — I think this all points to the only 100% way to validate an email address is to send an email with a validation link. You can prove that an address is in an invalid format, but you cannot prove that a properly formatted address does not exist, other than by sending an email to it.
Copy linkTweet thisAlerts:
@bokehMar 06.2006 — Have you actually looked at the error message from AOL. AOL does not accept SMTP connects from dynamic IPs. I don't know about the Yahoo error. Try it on my script in my signature. This prints out the whole exchange between my server and the remote SMTP server so you see absolutely everything.
Copy linkTweet thisAlerts:
@bokehMar 06.2006 — Also your MAIL FROM: and RCPT TO: are both using the same email address. My mailserver would definately reject that test based on SPF for a start. In efect what you are doing is pretending to be the authoritative mail server while connecting to the authoritative mail server. Even the stupidest mailserver should pick up on that.
×

Success!

Help @perpetualshaun spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...