Skip to content

Instantly share code, notes, and snippets.

@kostasdizas
Created May 7, 2011 00:48
Show Gist options
  • Save kostasdizas/960089 to your computer and use it in GitHub Desktop.
Save kostasdizas/960089 to your computer and use it in GitHub Desktop.
advanced e-mail validator
<?php
/**
* Checks if the given e-mail is valid
* Checks Done:
* - Email has correct number of '@' and lengths
* - Domain is valid
* - Domain has too little parts
* - Domain has valid parts
* - Domain is IP
* - Domain has a DNS MX record
*
* @return boolean
*/
private function checkEmailValidity($email)
{
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) return false;
list($username, $domain) = explode("@", $email);
if (ereg("^\[?[0-9\.]+\]?$", $domain)) return false;
$domain_parts = explode(".", $domain);
if (sizeof($domain_parts) < 2) return false;
foreach ($domain_parts as $d) {
$reg1 = "^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$";
if (!ereg($reg1, $d)) return false;
$reg2 = "^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$";
if (!ereg($reg2, $d)) return false;
}
if (!checkdnsrr($domain, 'MX')) return false;
return true;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment