PDA

View Full Version : Validating an e-mail address via PHP


pimenz
05-01-2003, 10:08 AM
How can I validate an e-mail address using PHP.
My code:
function myCheckDNSRR($hostName, $recType = '')
{
if(!empty($hostName)) {
f( $recType == '' ) $recType = "MX";
exec("nslookup -type=$recType $hostName", $result);
foreach ($result as $line) {
if(eregi("^$hostName",$line)) {
return true;
}
}
return false;
}
if (myCheckDNSRR("mydomain.com","MX"))
echo "yup - valid email!";
else
echo "nope - invalid email!";




But this only checks if the domain mydomain.com exists. But say now my e-mail is someone@mydomain.com
How do I check if the box someone exists on the domain.

danielt
05-01-2003, 11:01 AM
I think you can't test if the box exists somewhere. If it is for a login script you could take a to step registration where the user is only registered, if he replies to a certain mail with a "registration" link. What you can do though is check the correctnes of the typed mail, i.e. if it has the form user@domain.com. This is done by

if (!ereg("^.+@.+\\..+$", $email))
{
echo "EMail not spelled right!<br>"

}
else { // spelled right

}


But there are even stronger tests. Just look at http://www.planetsourcecode.com for example.