Part of the EllisLab Network
   
 
valid_email RFC specification FAIL!!! [SOLVED]
Posted: 28 December 2009 10:42 AM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-23-2008

These are the results of the function valid_email:

- email@example.com : TRUE, correct.
- email@èxàmplé.com : FALSE, but it’s valid!
- {|email|}@example.com : FALSE, but it’s valid!

See RFC_specification

 Signature 

Linux / html / css / javascript trick and tips

Profile
 
 
Posted: 28 December 2009 11:58 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1090
Joined  08-06-2006

that should be listed under “known issues”... that function will never be fixed - just treat it like a 90% ok function.

i did some work to get it to be better. basically, the only way to know if an email is good or not is to *send* an email to the address and if it doesn’t bounce, it’s probably a real email address - still not definitely a real email address until you get a return email from that email address.

FWIW:
http://codeigniter.com/forums/viewthread/89516/

cheers.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile
 
 
Posted: 28 December 2009 12:47 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-23-2008

In this case 99% is not enough.  blank stare

 Signature 

Linux / html / css / javascript trick and tips

Profile
 
 
Posted: 29 December 2009 05:42 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-23-2008

This is the correct function:

/**
    * Valid Email
    *
    * @access   public
    * @param    string
    * @return   bool
    *
    * Original author:  Sandeep V. Tamhankar (stamhankar@hotmail.com)
    * changes by Craig Cockburn to accommodate top level domains .museum and .name
    * PHP translations by Chalda Pnuzig http://blog.chalda.it/?p=11
    */
   
function valid_email($str)
   
{
      $emailPat 
'/^(.+)@(.+)$/';
      
$specialChars '\\(\\)<>@,;:\\\\\\\"\\.\\[\\]';
      
$validChars '[^\\s' $specialChars ']';
      
$quotedUser '("[^"]*")';
      
$ipDomainPat '/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/';
      
$atom $validChars '+';
      
$word '(' $atom '|' $quotedUser ')';
      
$userPat '/^' $word '(\\.' $word ')*$/';
      
$domainPat '/^' $atom '(\\.' $atom ')*$/';

      if (! 
preg_match($emailPat$str$matchArray)) {
         $this
->set_message('valid_email''Email address seems incorrect (check @ and .\'s)');
         return false;
      }

      $user = $matchArray[1];
      $domain = $matchArray[2];

      if (! preg_match($userPat, $user)) {
         $this->set_message('
valid_email', 'The part of your email address before the \'@\' doesn\'t seem to be valid.');
         return 
false;
      
}
      
if (preg_match($ipDomainPat$domain$IPArray)){
         
for ($i 1$i <= 4$i++) {
            
if ($IPArray[$i] 255{
               $this
->set_message('valid_email''Destination IP address is invalid!');
               return 
false;
            
}
         }
         
return true;
      
}

      
if (! preg_match($domainPat$domain$domainArray)){
         $this
->set_message('valid_email''Part of your email address after the \'@\' doesn\'t seem to be valid');
         return false;
      }

      $atomPat = '
/'.$atom.'/';

      if (preg_match_all($atomPat,$domain, $domArr)){
         $domArr = $domArr[0];
         $len = count($domArr);
         $firstDoaminLevel = strlen($domArr[$len - 1]);
         if ( ( $firstDoaminLevel < 2) || ($firstDoaminLevel > 6) ) {
             $this->set_message('
valid_email', 'The address must end in a top level domain (e.g. .com), or two letter country.');
             return false;
         }
         if ($len < 2) {
            $this->set_message('
valid_email', 'This address is missing a hostname!');
            return false;
         }
      }
      return true;
   } 
 Signature 

Linux / html / css / javascript trick and tips

Profile
 
 
Posted: 29 December 2009 07:30 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1090
Joined  08-06-2006

hi chalda,

thank you for participating! this is a really interesting function you’ve proffered. i think i can state unequivocally that anyone using IP addresses in emails is a spammer.

also, you mentioned the RFC but the code does not implement it.

also, as a start, please realize that there are several characters missing from the $specialChars array - specifically the “plus” character. perhaps you did not read the link i sent in my previous post that was discussing the plus character. i have included it here again for your convenience:

http://codeigniter.com/forums/viewthread/89516/

please examine this code side by side with the code you submitted. with a few revisions and some testing, i feel certain you can contribute something complete!

cheers.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile
 
 
Posted: 29 December 2009 07:44 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Avatar
Total Posts:  27
Joined  01-23-2008

Do you try my function?  raspberry

valid_email('!#$%&’*+-/=?^_`.{|}~@example.com'// return TRUE
valid_email('"@ @"@example.com'// return TRUE
valid_email('èmàil@èxamplè.com'// return TRUE 
 Signature 

Linux / html / css / javascript trick and tips

Profile
 
 
Posted: 29 December 2009 08:19 PM   [ Ignore ]   [ # 6 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1090
Joined  08-06-2006

no, i didn’t. wink did you try mine? grin

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile
 
 
Posted: 29 December 2009 09:58 PM   [ Ignore ]   [ # 7 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1090
Joined  08-06-2006

hi chalda,

i admit the first glance i misunderstood the function of the specialChars variable - minus point for me trying to visualize a complicated regex. :-(

so, upon your insistence, i tried your function and found one type of problem: it allows dashes at the start and/or end of domain names and it allows a domain like this: email@——.com

in the thread i referenced, there is an array of crazy emails that i designed to test email string validators; that is how i found the dash problem.

otherwise it looks like it handles some obscure points of the RFC pretty well. anyway, in that same thread there is also a pointer to a well-tested RFC-complete email checker that also does DNS testing and probes the SMTP mailserver for valid/receiving emails.

in my book, that’s the only real way to validate email addresses!

cheers.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile