I've built up various libraries of code over the years, always improving and changing. This bit of code should attempt to validate an email address, it first checks the format of it is correct then does an mx check on the domain name, I built the function based on various bits and bobs from around the Internet, or from the www.php.net website which has some excellent examples in the documentation. If I don't give credit it's because I can't remember where I got the code from!
-
function check_email_address($email)
-
{
-
// First, we check that there's one @ symbol, and that the lengths are right
-
{
-
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
-
return false;
-
}
-
-
// Split it into sections to make life easier
-
-
for ($i = 0; $i <sizeof($local_array); $i++)
-
{
-
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i]))
-
{
-
return false;
-
}
-
}
-
-
{
-
// Check if domain is IP. If not, it should be valid domain name
-
-
{
-
return false; // Not enough parts to domain
-
}
-
-
for ($i = 0; $i <sizeof($domain_array); $i++)
-
{
-
{
-
return false;
-
}
-
}
-
}
-
-
// mx check
-
$strDot = '.';
-
-
$strDomain = $chunks[($cntChunks-1)] . $strDot . $chunks[$cntChunks];
-
-
{
-
return false;
-
}
-
-
return true;
-
}
to call just use:
-
$email = "email@domain.com";
-
if (check_email_address($email))
-
{
-
// email looks valid
-
}
-
else
-
{
-
// email not valid
-
}