Is the PHP function filter_var('bob@example.com', FILTER_VALIDATE_EMAIL)
validating the email using the standard RFC 5322?
Asked
Active
Viewed 1,701 times
2

Marin Bînzari
- 5,208
- 2
- 26
- 43
-
1http://stackoverflow.com/questions/3722831/does-phps-filter-var-filter-validate-email-actually-work – Marc B Feb 02 '15 at 14:41
-
@MarcB, There isn't anything about RFC 5322. – Marin Bînzari Feb 02 '15 at 14:44
-
2which would sort of imply that it ISN'T using it? – Marc B Feb 02 '15 at 14:44
2 Answers
4
As the author of the regular expression being used by filter_var
I can confirm that it doesn't comprehensively use RFC 5322 (specifically, it doesn't allow for comments and folding white space).
The article VolkerK links to contains updated validation -- including a more accurate implementation of RFC 5322 as well as an implementation of RFC 5321 (in my opinion the more appropriate standard) -- but filter_var
hasn't been updated to incorporate the improvements.

Michael
- 11,912
- 6
- 49
- 64
2
There's a comment in the implementation code of that filter:
void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
{
/*
* 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.
*
* This regex does not handle comments and folding whitespace. While
* this is technically valid in an email address, these parts aren't
* actually part of the address itself.
*
* Michael's regex carries this copyright:
*
* Copyright © Michael Rushton 2009-10
* http://squiloople.com/
* Feel free to use and redistribute this code. But please keep this copyright notice.
*
*/
and the "original" source is most likely: http://squiloople.com/2009/12/20/email-address-validation/
/**
* Validate an email address using RFC 5322
*
...
So, you have a claim and someone who fixed an alleged error and ...
...beyond that I have no clue ;-)

VolkerK
- 95,432
- 20
- 163
- 226
-
The "observed error" isn't actually an error, just a disagreement over how to interpret the standard. I continue to claim that a@b is *syntactically* valid as per the very section quoted above: "In the case of a top-level domain used by itself in an email address, a single string is used without any dots." – Michael Feb 27 '15 at 17:25
-