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;
}