Return to Snippet

Revision: 62808
at March 13, 2013 19:08 by mafhh14

Updated Code
function domain_exists($email, $record = 'MX'){
   list($user, $domain) = explode('@', $email);
   return checkdnsrr($domain, $record);
if(domain_exists('[email protected]')) {
   echo('This MX records exists; I will accept this email as valid.');
else {
   echo('No MX record exists;  Invalid email.');

Revision: 62807
at March 13, 2013 19:07 by mafhh14

Initial Code

Initial URL

Initial Description
Validating an email address is one of the hardest feats on the web. A valid email can be marketing
gold, but an invalid email address is dead weight. Not only does it require a CPU-taxing PHP regular
expression ("/^[A-z0-9\._-]+"."@" . "[A-z0-9][A-z0-9-]*". "(\.[A-z0-9_-]+)*"."\.([A-z]{2,6})$/"),
the regular expression can be useless even after it's validated for format if the domain doesn't

A regular expression simply wont do -- we need to think more low-level. What does email at any
domain require? A DNS MX record. Well, PHP provides a great solution to validate that there's a MX
record for the provided email address' domain.

Initial Title
PHP Email Validator – Email MX DNS Record Check

Initial Tags
email, php

Initial Language