PHP Email Address Validation

Posted on Sunday 25 February 2007

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!

PHP:
  1. function check_email_address($email)
  2. {
  3.     // First, we check that there's one @ symbol, and that the lengths are right
  4.     if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email))
  5.     {
  6.         // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
  7.         return false;
  8.     }
  9.  
  10.     // Split it into sections to make life easier
  11.     $email_array = explode("@", $email);
  12.     $local_array = explode(".", $email_array[0]);
  13.  
  14.     for ($i = 0; $i <sizeof($local_array); $i++)
  15.     {
  16.         if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i]))
  17.         {
  18.             return false;
  19.         }
  20.     }
  21.  
  22.     if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))
  23.     {
  24.         // Check if domain is IP. If not, it should be valid domain name
  25.         $domain_array = explode(".", $email_array[1]);
  26.  
  27.         if (sizeof($domain_array) <2)
  28.         {
  29.             return false; // Not enough parts to domain
  30.         }
  31.  
  32.         for ($i = 0; $i <sizeof($domain_array); $i++)
  33.         {
  34.             if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i]))
  35.             {
  36.                 return false;
  37.             }
  38.         }
  39.     }
  40.  
  41.     // mx check
  42.     $strDot      = '.';
  43.     $strAfterAt  = substr(strstr($email, '@'), 1);
  44.     $chunks      = explode($strDot, $strAfterAt);
  45.     $cntChunks  = count($chunks) - 1;
  46.  
  47.     $strDomain = $chunks[($cntChunks-1)] . $strDot . $chunks[$cntChunks];
  48.  
  49.     if (!getmxrr( $strDomain, $mxhosts ))
  50.     {
  51.        return false;
  52.     }
  53.  
  54.     return true;
  55. }

to call just use:

PHP:
  1. $email = "email@domain.com";
  2. if (check_email_address($email))
  3. {
  4.     // email looks valid
  5. }
  6. else
  7. {
  8.     // email not valid
  9. }


No comments have been added to this post yet.

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI