RFC (2)822 & 3696 Email Address Parser in PHP

Warning! Releases 4 and older contained a bug in the RFC2882 atext rule, which allowed periods in the wrong places. Upgrade to release 5 or later to fix this issue.

Source code

Tests

The test suite shows results for each parser, based on these test definitions. These are borrowed from Dominic Sayers who has a similar parser. We are still arguing over certain tests ;)

Download

You can download the latest version (release 8) of the functions here.

The very latest versions are available from the SVN repository.

The RFCs

Email address formats are covered by several RFCs:

RFC 822 - Standard for the Format of ARPA Internet Text Messages
The original 'email' RFC
RFC 2821 - Simple Mail Transfer Protocol
SMTP contains some address limits not in RFC2822
RFC 2822 - Internet Message Format
Superceeds RFC822
RFC 3696 - Application Techniques for Checking and Transformation of Names
An informative RFC that clarifies some rules (and muddies others)

Copyright

By Cal Henderson <cal@iamcal.com>

This code is licensed under a Creative Commons Attribution-ShareAlike 2.5 License

http://creativecommons.org/licenses/by-sa/2.5/

Limitations

The code only verifies that the email address matches the RFC spec. This does not mean it's a valid Internet email address! For an email address to be valid on the Internet, the domain part must be a valid domain name, be resolvable and have an MX. The code will identify the address "foo@bar.baz" as valid, even though we konw that there's no such domain as bar.baz. If you want to check that it's valid, fetching the MX for the domain is a good start. Connecting to the MX to verify it's a mail server is even better.

Extras

Tim Fletcher has translated the function to ruby and python: http://tfletcher.com/lib/.

A full rolled-up version of the RFC 2882 regexp can be seen here.