RFC (2)822 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

Download

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

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.