PHP FILTER_VALIDATE_EMAIL 无法正常工作
Posted
技术标签:
【中文标题】PHP FILTER_VALIDATE_EMAIL 无法正常工作【英文标题】:PHP FILTER_VALIDATE_EMAIL does not work correctly 【发布时间】:2013-10-13 18:00:14 【问题描述】:我使用的是 php 5.3.10。这是代码:
<?php
$email = "test@example.c";
if (filter_var($email, FILTER_VALIDATE_EMAIL))
echo "Email: ".$email." correct";
else
echo "email not correct";
?>
它返回:"Email: test@example.c
正确。
我认为只有一个字符的***域是不正确的(根据此列表,我不知道一个字符长度的 TLD:http://data.iana.org/TLD/tlds-alpha-by-domain.txt
)。
那么,FILTER_VALIDATE_EMAIL
过滤器是否正常工作?
【问题讨论】:
@ 之前的第一部分不能超过 64 个字符...在我的情况下,这导致了几个小时的调试,但FILTER_VALIDATE_EMAIL
是正确的...
【参考方案1】:
FILTER_VALIDATE_EMAIL
只能告诉你地址格式是否正确。
考虑这些例子
// "not an email" is invalid so its false.
php > var_export(filter_var("not an email", FILTER_VALIDATE_EMAIL));
false
// "foo@a.com" looks like an email, so it passes even though its not real.
php > var_export(filter_var("foo@a.com", FILTER_VALIDATE_EMAIL));
'foo@a.com'
// "foo@gmail.com" passes, gmail is a valid email server,
// but gmail require more than 3 letters for the address.
var_export(filter_var("foo@gmail.com", FILTER_VALIDATE_EMAIL));
'foo@gmail.com'
所以FILTER_VALIDATE_EMAIL
只擅长在完全错误的时候告诉你,而不是在正确的时候告诉你。
您将需要使用像Real Email 这样可以进行深入的电子邮件地址验证的服务。
// foo@gmail.com is invalid because gmail require more than 3 letters for the address.
var_export(file_get_contents("https://isitarealemail.com/api/email/validate?email=foo@gmail.com"));
'"status":"invalid"'
// foobar@gmail.com is valid
var_export(file_get_contents("https://isitarealemail.com/api/email/validate?email=foobar@gmail.com"));
'"status":"valid"'
更多信息请参见https://docs.isitarealemail.com/how-to-validate-email-addresses-in-php
【讨论】:
【参考方案2】:验证电子邮件地址有点复杂。 看看这个列表:
有效的电子邮件地址
-
niceandsimple@example.com
very.common@example.com
a.little.lengthy.but.fine@dept.example.com
disposable.style.email.with+symbol@example.com
user@[IPv6:2001:db8:1ff::a0b:dbd0]
“非常不寻常”@example.com
"very.unusual.@.unusual.com"@example.com
"非常.(),:;[]".非常."非常@\
"非常".unusual"@strange.example.com
postbox@com(***域是有效的主机名)
admin@mailserver1(没有 TLD 的本地域名)
!#$%&'*+-/=?^_`|~@example.org
"()[]:,;@\"!#$%&'*+-/=?^_`| ~.a"@example.org
" "@example.org(引号之间的空格)
üñîçøðé@example.com(本地部分的 Unicode 字符)
电子邮件地址无效
-
Abc.example.com(@ 字符必须分隔本地和域
部分)
A@b@c@example.com(引号外只允许有一个@)
a"b(c)d,e:f;gi[j\k]l@example.com(没有特殊字符
在此本地部分中允许在引号之外)
just"not"right@example.com(引用的字符串必须用点分隔,或者
构成本地部分的唯一元素)
this is"not\allowed@example.com(空格、引号和反斜杠可能
仅存在于带引号的字符串中并以反斜杠开头)
this\ still"not\allowed@example.com(即使转义(前面
反斜杠)、空格、引号和反斜杠仍然必须是
包含在引号中)
来源http://en.wikipedia.org/wiki/Email_address
几乎所有的电子邮件验证实现都是“错误的”,但 php 实现很好用,因为它接受所有常见的电子邮件地址
更新:
发现于http://www.php.net/manual/en/filter.filters.validate.php
关于没有 .在域部分,源代码中的注释(在 ext/filter/logical_filters.c 中)证明了这种拒绝:
* The regex below is based on a regex by Michael Rushton.
* However, it is not identical. I changed it to only consider routeable
* addresses as valid. Michael's regex considers a@b a valid address
* which conflicts with section 2.3.5 of RFC 5321 which states that:
*
* Only resolvable, fully-qualified domain names (FQDNs) are permitted
* when domain names are used in SMTP. In other words, names that can
* be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
* in Section 5) are permitted, as are CNAME RRs whose targets can be
* resolved, in turn, to MX or address RRs. Local nicknames or
* unqualified names MUST NOT be used.
这是 Michael Rushton 的课程链接(链接已断开,请参阅下面的源代码) 支持 RFC 5321/5322
<?php
/**
* Squiloople Framework
*
* LICENSE: Feel free to use and redistribute this code.
*
* @author Michael Rushton <michael@squiloople.com>
* @link http://squiloople.com/
* @package Squiloople
* @version 1.0
* @copyright © 2012 Michael Rushton
*/
/**
* Email Address Validator
*
* Validate email addresses according to the relevant standards
*/
final class EmailAddressValidator
// The RFC 5321 constant
const RFC_5321 = 5321;
// The RFC 5322 constant
const RFC_5322 = 5322;
/**
* The email address
*
* @access private
* @var string $_email_address
*/
private $_email_address;
/**
* A quoted string local part is either allowed (true) or not (false)
*
* @access private
* @var boolean $_quoted_string
*/
private $_quoted_string = FALSE;
/**
* An obsolete local part is either allowed (true) or not (false)
*
* @access private
* @var boolean $_obsolete
*/
private $_obsolete = FALSE;
/**
* A basic domain name is either required (true) or not (false)
*
* @access private
* @var boolean $_basic_domain_name
*/
private $_basic_domain_name = TRUE;
/**
* A domain literal domain is either allowed (true) or not (false)
*
* @access private
* @var boolean $_domain_literal
*/
private $_domain_literal = FALSE;
/**
* Comments and folding white spaces are either allowed (true) or not (false)
*
* @access private
* @var boolean $_cfws
*/
private $_cfws = FALSE;
/**
* Set the email address and turn on the relevant standard if required
*
* @access public
* @param string $email_address
* @param null|integer $standard
*/
public function __construct($email_address, $standard = NULL)
// Set the email address
$this->_email_address = $email_address;
// Set the relevant standard or throw an exception if an unknown is requested
switch ($standard)
// Do nothing if no standard requested
case NULL:
break;
// Otherwise if RFC 5321 requested
case self::RFC_5321:
$this->setStandard5321();
break;
// Otherwise if RFC 5322 requested
case self::RFC_5322:
$this->setStandard5322();
break;
// Otherwise throw an exception
default:
throw new Exception('Unknown RFC standard for email address validation.');
/**
* Call the constructor fluently
*
* @access public
* @static
* @param string $email_address
* @param null|integer $standard
* @return EmailAddressValidator
*/
public static function setEmailAddress($email_address, $standard = NULL)
return new self($email_address, $standard);
/**
* Validate the email address using a basic standard
*
* @access public
* @return EmailAddressValidator
*/
public function setStandardBasic()
// A quoted string local part is not allowed
$this->_quoted_string = FALSE;
// An obsolete local part is not allowed
$this->_obsolete = FALSE;
// A basic domain name is required
$this->_basic_domain_name = TRUE;
// A domain literal domain is not allowed
$this->_domain_literal = FALSE;
// Comments and folding white spaces are not allowed
$this->_cfws = FALSE;
// Return the EmailAddressValidator object
return $this;
/**
* Validate the email address using RFC 5321
*
* @access public
* @return EmailAddressValidator
*/
public function setStandard5321()
// A quoted string local part is allowed
$this->_quoted_string = TRUE;
// An obsolete local part is not allowed
$this->_obsolete = FALSE;
// Only a basic domain name is not required
$this->_basic_domain_name = FALSE;
// A domain literal domain is allowed
$this->_domain_literal = TRUE;
// Comments and folding white spaces are not allowed
$this->_cfws = FALSE;
// Return the EmailAddressValidator object
return $this;
/**
* Validate the email address using RFC 5322
*
* @access public
* @return EmailAddressValidator
*/
public function setStandard5322()
// A quoted string local part is disallowed
$this->_quoted_string = FALSE;
// An obsolete local part is allowed
$this->_obsolete = TRUE;
// Only a basic domain name is not required
$this->_basic_domain_name = FALSE;
// A domain literal domain is allowed
$this->_domain_literal = TRUE;
// Comments and folding white spaces are allowed
$this->_cfws = TRUE;
// Return the EmailAddressValidator object
return $this;
/**
* Either allow (true) or do not allow (false) a quoted string local part
*
* @access public
* @param boolean $allow
* @return EmailAddressValidator
*/
public function setQuotedString($allow = TRUE)
// Either allow (true) or do not allow (false) a quoted string local part
$this->_quoted_string = $allow;
// Return the EmailAddressValidator object
return $this;
/**
* Either allow (true) or do not allow (false) an obsolete local part
*
* @access public
* @param boolean $allow
* @return EmailAddressValidator
*/
public function setObsolete($allow = TRUE)
// Either allow (true) or do not allow (false) an obsolete local part
$this->_obsolete = $allow;
// Return the EmailAddressValidator object
return $this;
/**
* Either require (true) or do not require (false) a basic domain name
*
* @access public
* @param boolean $allow
* @return EmailAddressValidator
*/
public function setBasicDomainName($allow = TRUE)
// Either require (true) or do not require (false) a basic domain name
$this->_basic_domain_name = $allow;
// Return the EmailAddressValidator object
return $this;
/**
* Either allow (true) or do not allow (false) a domain literal domain
*
* @access public
* @param boolean $allow
* @return EmailAddressValidator
*/
public function setDomainLiteral($allow = TRUE)
// Either allow (true) or do not allow (false) a domain literal domain
$this->_domain_literal = $allow;
// Return the EmailAddressValidator object
return $this;
/**
* Either allow (true) or do not allow (false) comments and folding white spaces
*
* @access public
* @param boolean $allow
* @return EmailAddressValidator
*/
public function setCFWS($allow = TRUE)
// Either allow (true) or do not allow (false) comments and folding white spaces
$this->_cfws = $allow;
// Return the EmailAddressValidator object
return $this;
/**
* Return the regular expression for a dot atom local part
*
* @access private
* @return string
*/
private function _getDotAtom()
return "([!#-'*+\/-9=?^-~-]+)(?>\.(?1))*";
/**
* Return the regular expression for a quoted string local part
*
* @access private
* @return string
*/
private function _getQuotedString()
return '"(?>[ !#-\[\]-~]|\\\[ -~])*"';
/**
* Return the regular expression for an obsolete local part
*
* @access private
* @return string
*/
private function _getObsolete()
return '([!#-\'*+\/-9=?^-~-]+|"(?>'
. $this->_getFWS()
. '(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*'
. $this->_getFWS()
. '")(?>'
. $this->_getCFWS()
. '\.'
. $this->_getCFWS()
. '(?1))*';
/**
* Return the regular expression for a domain name domain
*
* @access private
* @return string
*/
private function _getDomainName()
// Return the basic domain name format if required
if ($this->_basic_domain_name)
return '(?>' . $this->_getDomainNameLengthLimit()
. '[a-z\d](?>[a-z\d-]*[a-z\d])?'
. $this->_getCFWS()
. '\.'
. $this->_getCFWS()
. ')1,126[a-z]2,6';
// Otherwise return the full domain name format
return $this->_getDomainNameLengthLimit()
. '([a-z\d](?>[a-z\d-]*[a-z\d])?)(?>'
. $this->_getCFWS()
. '\.'
. $this->_getDomainNameLengthLimit()
. $this->_getCFWS()
. '(?2))0,126';
/**
* Return the regular expression for an IPv6 address
*
* @access private
* @return string
*/
private function _getIPv6()
return '([a-f\d]1,4)(?>:(?3))7|(?!(?:.*[a-f\d][:\]])8,)((?3)(?>:(?3))0,6)?::(?4)?';
/**
* Return the regular expression for an IPv4-mapped IPv6 address
*
* @access private
* @return string
*/
private function _getIPv4MappedIPv6()
return '(?3)(?>:(?3))5:|(?!(?:.*[a-f\d]:)6,)(?5)?::(?>((?3)(?>:(?3))0,4):)?';
/**
* Return the regular expression for an IPv4 address
*
* @access private
* @return string
*/
private function _getIPv4()
return '(25[0-5]|2[0-4]\d|1\d2|[1-9]?\d)(?>\.(?6))3';
/**
* Return the regular expression for a domain literal domain
*
* @access private
* @return string
*/
private function _getDomainLiteral()
return '\[(?:(?>IPv6:(?>'
. $this->_getIPv6()
. '))|(?>(?>IPv6:(?>'
. $this->_getIPv4MappedIPv6()
. '))?'
. $this->_getIPv4()
. '))\]';
/**
* Return either the regular expression for folding white spaces or its backreference
*
* @access private
* @param boolean $define
* @return string
*/
private function _getFWS($define = FALSE)
// Return the backreference if $define is set to FALSE otherwise return the regular expression
if ($this->_cfws)
return !$define ? '(?P>fws)' : '(?<fws>(?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)';
/**
* Return the regular expression for comments
*
* @access private
* @return string
*/
private function _getComments()
return '(?<comment>\((?>'
. $this->_getFWS()
. '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?P>comment)))*'
. $this->_getFWS()
. '\))';
/**
* Return either the regular expression for comments and folding white spaces or its backreference
*
* @access private
* @param boolean $define
* @return string
*/
private function _getCFWS($define = FALSE)
// Return the backreference if $define is set to FALSE
if ($this->_cfws && !$define)
return '(?P>cfws)';
// Otherwise return the regular expression
if ($this->_cfws)
return '(?<cfws>(?>(?>(?>'
. $this->_getFWS(TRUE)
. $this->_getComments()
. ')+'
. $this->_getFWS()
. ')|'
. $this->_getFWS()
. ')?)';
/**
* Establish and return the valid format for the local part
*
* @access private
* @return string
*/
private function _getLocalPart()
// The local part may be obsolete if allowed
if ($this->_obsolete)
return $this->_getObsolete();
// Otherwise the local part must be either a dot atom or a quoted string if the latter is allowed
if ($this->_quoted_string)
return '(?>' . $this->_getDotAtom() . '|' . $this->_getQuotedString() . ')';
// Otherwise the local part must be a dot atom
return $this->_getDotAtom();
/**
* Establish and return the valid format for the domain
*
* @access private
* @return string
*/
private function _getDomain()
// The domain must be either a domain name or a domain literal if the latter is allowed
if ($this->_domain_literal)
return '(?>' . $this->_getDomainName() . '|' . $this->_getDomainLiteral() . ')';
// Otherwise the domain must be a domain name
return $this->_getDomainName();
/**
* Return the email address length limit
*
* @access private
* @return string
*/
private function _getEmailAddressLengthLimit()
return '(?!(?>' . $this->_getCFWS() . '"?(?>\\\[ -~]|[^"])"?' . $this->_getCFWS() . ')255,)';
/**
* Return the local part length limit
*
* @access private
* @return string
*/
private function _getLocalPartLengthLimit()
return '(?!(?>' . $this->_getCFWS() . '"?(?>\\\[ -~]|[^"])"?' . $this->_getCFWS() . ')65,@)';
/**
* Establish and return the domain name length limit
*
* @access private
* @return string
*/
private function _getDomainNameLengthLimit()
return '(?!' . $this->_getCFWS() . '[a-z\d-]64,)';
/**
* Check to see if the domain can be resolved to MX RRs
*
* @access private
* @param array $domain
* @return integer|boolean
*/
private function _verifyDomain($domain)
// Return 0 if the domain cannot be resolved to MX RRs
if (!checkdnsrr(end($domain), 'MX'))
return 0;
// Otherwise return true
return TRUE;
/**
* Perform the validation check on the email address's syntax and, if required, call _verifyDomain()
*
* @access public
* @param boolean $verify
* @return boolean|integer
*/
public function isValid($verify = FALSE)
// Return false if the email address has an incorrect syntax
if (!preg_match(
'/^'
. $this->_getEmailAddressLengthLimit()
. $this->_getLocalPartLengthLimit()
. $this->_getCFWS()
. $this->_getLocalPart()
. $this->_getCFWS()
. '@'
. $this->_getCFWS()
. $this->_getDomain()
. $this->_getCFWS(TRUE)
. '$/isD'
, $this->_email_address
))
return FALSE;
// Otherwise check to see if the domain can be resolved to MX RRs if required
if ($verify)
return $this->_verifyDomain(explode('@', $this->_email_address));
// Otherwise return 1
return 1;
2016 年编辑: 在 PHP 7.1 beta 中,我注意到以下内容:
根据 RFC 6531 实施电子邮件验证。(Leo Feyer,Anatol)。参见第 3.3 节 https://www.rfc-editor.org/rfc/rfc6531#section-3.3
https://en.wikipedia.org/wiki/International_email
还有一些不错的额外示例
用户@例子.广告 (Chinese, Unicode)
उपयोगकर्ता@उदाहरण.कॉम (Hindi, Unicode)
юзер@екзампл.ком (Ukrainian, Unicode)
θσερ@εχαμπλε.ψομ (Greek, Unicode)
Dörte@Sörensen.example.com (German, Unicode)
【讨论】:
您说: 9. postbox@com(***域是有效的主机名),但该地址上的 FILTER_VALIDATE_EMAIL 过滤器返回 false!那么,过滤器是否只接受某些电子邮件地址(包括“joe@example.c”)而不接受其他电子邮件地址(无论如何,像“postbox@com”一样有效)?这是一种奇怪的行为! @visserSander:filter_var('"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com',FILTER_VALIDATE_EMAIL)
被接受,但postbox@com
不被接受。我称之为错误
@aaron:我完全不同意您所说的everything:您声明PHP 实现是正确的,但官方RFC 规范不是?我是说……真的吗?!自从更新以来,我有点明白为什么a@b
被认为是无效的,尽管我不禁将其视为一个决定,至少部分是出于懒惰的动机……在一个测试环境mail('root@localhost');
看起来不会太古怪吧?
是的,PHP 支持新的更长的顶层。但我注意到 php7.1 中的 filter_var
不认为 उपयोगकर्ता@उदाहरण.कॉम
是有效的电子邮件地址
FILTER_VALIDATE_EMAIL 不处理国际电子邮件地址。这种格式会导致错误,我几乎每天都通过电子邮件发送它。 local@domain.co.ke【参考方案3】:
google 代码上有一个用于验证电子邮件地址的 PHP 类:
http://code.google.com/p/php-email-address-validation
你可以像这样使用它
include('EmailAddressValidator.php');
$validator = new EmailAddressValidator;
if ($validator->check_email_address('test@example.org'))
// Email address is technically valid
else
// Email not valid
【讨论】:
已弃用...坚持使用 PHP 的 php.net/manual/en/filter.filters.validate.php,它根据 RFC 822 涵盖了几乎所有用例【参考方案4】:我选择使用:
<?php
$email =$argv[1];$result= is_valid_email($email); echo $result;
function is_valid_email($email) return preg_match('/^(([^<>()[\]\\.,;:\s@"\']+(\.[^<>()[\]\\.,;:\s@"\']+)*)|("[^"\']+"))@((\[\d1,3\.\d1,3\.\d1,3\.\d1,3\])|(([a-zA-Z\d\-]+\.)+[a-zA-Z]2,))$/', $email);
?>
在我的论坛软件中 https://github.com/neofutur/MyBestBB/blob/master/include/email.php#L39
但官方是这样的: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html (也更长)
【讨论】:
【参考方案5】:FILTER_VALIDATE_EMAIL 不支持 PHP 5.2.14
【讨论】:
我注意到了这一点,因为它在我的机器上本地运行,但当我将代码上传到托管服务器时却没有。以上是关于PHP FILTER_VALIDATE_EMAIL 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
PHP 的 filter_var FILTER_VALIDATE_EMAIL 真的有效吗?