• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Email Validation Helper

#1
[eluser]kevinprince[/eluser]
Hey All,

Ive been building a new mailing list application for a client and have hit a snack on an import module. The email helper seems to be failing seemingly valid email addresses.

Was wondering if anyone could explain why its happening or any suggestions have to solve it. Here is an example email address format (redacted obviously).

[email protected] also [email protected]

Suggestions?

Kevin

#2
[eluser]kevinprince[/eluser]
Bumping this any suggestions ?

#3
[eluser]MadZad[/eluser]
FWIW, here's some pre-CI code that I had lying around:

(note, ilovejd now at http://code.google.com/p/php-email-address-validation/ you may just wish to use that code and forget the rest of this post)

Code:
<?
  /*
   * Check the formatting of an email address to see if it looks valid.
   * No checking is done on the domain, or if the address is active.
   *
   * Sources: http://www.php.net
   *          http://www.ilovejackdaniels.com/php/email-address-validation
   *          http://www.spoono.com/php/tutorials/tutorial.php?id=41
   */
  function validEmail ($email, $check_domain = TRUE) {
    // First, we check that there's one @ symbol, and that the lengths are right
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
      // 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
    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);
    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;
      }
    }
    if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
      $domain_array = explode(".", $email_array[1]);
      if (sizeof($domain_array) < 2) {
        return FALSE; // Not enough parts to domain
      }
      for ($i = 0; $i < sizeof($domain_array); $i++) {
        if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
          return FALSE;
        }
      }
    }

    //Short-circuit, don't check domain name validity
    if (!$check_domain) return TRUE;
    
    // Verify the domain name
    // Note, getmxrr and checkdnsrr are not implemented on Windows  
    list($username, $domain) = split("@",$email);
    $result = array();
    if (function_exists('getmxrr') and getmxrr($domain, $result)) {
      return TRUE;
    }

    if (function_exists('checkdnsrr') and checkdnsrr($domain, "ANY")) {
      return TRUE;
    }

    if (! (function_exists('checkdnsrr') or function_exists('getmxrr'))) {
      exec("nslookup -type=MX $domain", $result);
      // Check each result line to find the one that starts with the host name.
      foreach ($result as $line) {
        if (eregi("^$domain",$line)) {
          return TRUE;
        }
      }
    }

    return FALSE;
  }
  
/*
  Some notes on alternate or extended approaches to email validation

  Could try PEAR's Net_DNS package for validation on Windows
  
  Simplified one-line regex
    if (!eregi('^[a-z0-9_\.\-][email protected][a-z0-9\-]+\.[a-z0-9\-\.]+$', $email)) return FALSE;


  Domain name checking - note, getmxrr is not implemented on Windows  
    $MXHost = array();
    if(getmxrr($Domain, $MXHost)) {
      return TRUE;
    }
    else {
      return fsockopen($Domain, 25, $errno, $errstr, 30);
    }

  For Windows, could use "custom" version of checkdnsrr to verify domain,
  but be aware is calls exec
  if(!function_exists('checkdnsrr')) {
    function checkdnsrr($hostName, $recType = '')
    {
     if(!empty($hostName)) {
       if( $recType == '' ) $recType = "MX";
       exec("nslookup -type=$recType $hostName", $result);
       // check each line to find the one that starts with the host
       // name. If it exists then the function succeeded.
       foreach ($result as $line) {
         if(eregi("^$hostName",$line)) {
           return true;
         }
       }
       // otherwise there was no mail handler for the domain
       return false;
     }
     return false;
    }
  }
*/
?&gt;

#4
[eluser]kevinprince[/eluser]
Thanks for this Smile Its not perfect as im getting some problem emails still.

Investigating and tweaking now.

#5
[eluser]MadZad[/eluser]
I most certainly take "not perfect" as a kindness to my code. :-)
Best of luck, and for what it's worth, back when that was written, we were using SimpleTest for unit testing. This kind of function just screams out for unit testing, and your reply reminded me to look for it:

Code:
class test_emailValidation extends UnitTestCase {

  //occasionally useful
  //echo "<pre>var: " . var_export($post_out) . "</pre><br />\n";

  function __construct() {
    require ("../emailValidation.php");
    echo get_class($this) . "<br />\n";
  }


  function test_email_address () {
    $this->assertTrue(validEmail("[email protected]"));
    $this->assertTrue(validEmail("[email protected]", TRUE));
    $this->assertTrue(validEmail("[email protected]"));
    $this->assertTrue(validEmail("[email protected]"));
    $this->assertTrue(validEmail("[email protected]"));
    $this->assertTrue(validEmail("[email protected]"));

    $this->assertFalse(validEmail("[email protected]@example.com"));
    $this->assertFalse(validEmail("[email protected]"));
    $this->assertTrue(validEmail("[email protected]", FALSE));
    $this->assertFalse(validEmail("test"));
    $this->assertFalse(validEmail("TeSt"));
    $this->assertFalse(validEmail("[email protected]"));
    $this->assertFalse(validEmail("[email protected]"));
    $this->assertFalse(validEmail("[email protected]"));
    $this->assertFalse(validEmail("[email protected]"));
    $this->assertFalse(validEmail("[email protected]", FALSE));

    $this->assertTrue(validEmail("[email protected]"));
    $this->assertTrue(validEmail("[email protected]", FALSE));
    $this->assertTrue(validEmail("[email protected]"));
  }

}

#6
[eluser]kevinprince[/eluser]
So turns out its not CI' email validation or the 3 others i tried which are wrong. The data contains hidden characters which cant usually be seen.

Im trying now to find php code to remove them. Suggestions welcome

#7
[eluser]phantom-a[/eluser]
[quote author="nextgengames" date="1221187940"]So turns out its not CI' email validation or the 3 others i tried which are wrong. The data contains hidden characters which cant usually be seen.

Im trying now to find php code to remove them. Suggestions welcome[/quote]

Maby its Whitespace


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.