Welcome Guest, Not a member yet? Register   Sign In
valid_email RFC specification FAIL!!! [SOLVED]
#1

[eluser]Chalda Pnuzig[/eluser]
These are the results of the function valid_email:

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

See RFC_specification
#2

[eluser]sophistry[/eluser]
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://ellislab.com/forums/viewthread/89516/

cheers.
#3

[eluser]Chalda Pnuzig[/eluser]
In this case 99% is not enough. :blank:
#4

[eluser]Chalda Pnuzig[/eluser]
This is the correct function:

Code:
/**
    * Valid Email
    *
    * @access   public
    * @param    string
    * @return   bool
    *
    * Original author:  Sandeep V. Tamhankar ([email protected])
    * 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;
   }
#5

[eluser]sophistry[/eluser]
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://ellislab.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.
#6

[eluser]Chalda Pnuzig[/eluser]
Do you try my function? Tongue

Code:
valid_email('!#$%&’*+-/=?^_`.{|}[email protected]') // return TRUE
valid_email('"@ @"@example.com') // return TRUE
valid_email('èmàil@èxamplè.com') // return TRUE
#7

[eluser]sophistry[/eluser]
no, i didn't. Wink did you try mine? :-)
#8

[eluser]sophistry[/eluser]
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 protected]

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.




Theme © iAndrew 2016 - Forum software by © MyBB