A regex is not really suitable for determining the validity of email address syntax, and the FILTER_VALIDATE_EMAIL
option for the filter_var
function is rather unreliable too. I use the EmailAddressValidator Class to test email address syntax.
I have put together a few examples of incorrect results returned by filter_var
(PHP Version 5.3.2-1ubuntu4.2). There are probably more. Some are admittedly a little extreme, but still worth noting:
RFC 1035 2.3.1. Preferred name syntax
http://tools.ietf.org/search/rfc1035
Summarised as: a domain consists of labels separated by dot separators (not necessarily true for local domains though).
echo filter_var('name@example', FILTER_VALIDATE_EMAIL);
// name@example
RFC 1035 2.3.1. Preferred name syntax
The labels must follow the rules for ARPANET host names. They must start with a letter, and with a letter or digit, and have as interior characters only letters, digits, and hyphen.
echo filter_var('[email protected]', FILTER_VALIDATE_EMAIL);
// name@1example
RFC 2822 3.2.5. Quoted strings
http://tools.ietf.org/html/rfc2822#section-3.2.5
This is valid (although it is rejected by many mail servers):
echo filter_var('name"quoted"string@example', FILTER_VALIDATE_EMAIL);
// FALSE
RFC 5321 4.5.3.1.1. Local-part
http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1
The maximum total length of a user name or other local-part is 64 octets.
Test with 70 characters:
echo filter_var('AbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghij@example.com', FILTER_VALIDATE_EMAIL);
// AbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghij@example.com
RFC 5321 4.5.3.1.2. Domain
http://tools.ietf.org/html/rfc5321#section-4.5.3.1.2
The maximum total length of a domain name or number is 255 octets.
Test with 260 characters:
echo filter_var('name@AbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghij.com', FILTER_VALIDATE_EMAIL);
// name@AbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghijAbcdefghij.com
Have a look at Validate an E-Mail Address with PHP, the Right Way for more information.