This PHP function checks if an email address is valid. By default it will only do syntax checking with a regex, but it can also be configured to do a MX record check. I have expanded the function so that it can also be configured to verify the address via an SMTP probe.
It will give an error in these situations:
- A 5xx SMTP response code is encountered
- None of the mailservers can be contacted
- No usable DNS records are found
- The script take more than a certain amount of time (mostly transient errors)
This function makes connections to remote SMTP servers. This means that outgoing TCP network connections to port 25 must be possible. If this is not the case, then the script will not work.
Some hosting providers block exactly these network connections because they are also used by spammers.
When you always get “None of the mailservers listed for $domain could be contacted”, no matter what address you fill in, this is usually a good indication that port 25 is blocked. In this case there is no workaround other than finding a new hosting provider.
Using this function on a server might cause it’s IP address(es) to get blacklisted. This can be a problem if the server is also a mail server. So don’t use it on a server that runs mail in a production environment.
How to use
Here is a simple example.
- 15 April 2012 Due to heavy abuse of the simple demo page I had to remove that and add a CAPTCHA to the main page.
- 21 Februari 2010 My mail server was blacklisted by Microsoft for exhibiting “namespace mining behavior”. Since I already have whitelisted those domains in Postifx, this verification page must have been the cause. I updated the verification code so that it now uses the Postfix access file (currently only type PCRE is supported). If you verify a whitelisted address, the Debug option below will tell you.
- 30 December 2009 Because on Windows several PHP DNS functions are not available, I have hacked in support via the PEAR Net_DNS class. In order to install Net_DNS however, you need the mhash module in php (lib_mhash.dll). Thanks to email@example.com for reminding.