[CentOS] [off-topic] Validating e-mail address with php

Fri May 6 16:11:08 UTC 2016
Alice Wonder <alice at domblogger.net>

After running into several web application that rejected any e-mail 
address on the new gTLDs because the apps were badly written to validate 
with a hard-coded list of valid TLDs, I wrote a e-mail validation 
function that I *think* does things properly.

Free for anyone to use who wants to.


What it really does is use the PHP built-in filter_var but it 
compensates for known bugs in the filter_var e-mail address validation.

It return boolean.

I *think* it will properly validate any legal routable address and 
reject any that are not. But bugs may exist.

By default when the host part of user at host is a domain name, it does a 
DNS query to validate the host resolves.

That adds a little latency but I think is worth it. My own 
implementation will probably cache a positive result for up to 24 hours 
with APCu but that's not in the function linked. The dns lookup can be 

By default it will reject IP address as the host part, but that can be 
enabled. When enabled, it will reject private and reserved range. Well, 
reject what is hard-coded into PHP filter_var - I haven't tested that.

International domain names will only validate if the php-intl functions 
are installed (to turn them into punycode)


For the username validation, that's where any bugs are most likely to 
exist. But I think I did it right. It's very unfortunate that the 
built-in php filter doesn't do it right.

Anyway free for anyone, I'm tired of e-mail address filters that don't 
properly work with common user names on perfectly legal domains that 
just happen to be newly ICANN approved.

You can see live testing by just removing the last s from the filename.