PHP Email Validator – Email MX DNS Record Check

By  on  

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 exist. 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.

The Code

function domain_exists($email, $record = 'MX'){
	list($user, $domain) = split('@', $email);
	return checkdnsrr($domain, $record);

The Usage

if(domain_exists('')) {
	echo('This MX records exists; I will accept this email as valid.');
else {
	echo('No MX record exists;  Invalid email.');

You'll see that MX is the default record check, though you could change that to A if you want. This method is not bulletproof but the function does provide a compelling enough argument for an email existing or being a fraud. If a valid email address is important for your purposes, use this function. Try it for yourself!

Recent Features

Incredible Demos

  • By
    CSS Sprites

    The idea of CSS sprites is pretty genius. For those of you who don't know the idea of a sprite, a sprite is basically multiple graphics compiled into one image. The advantages of using sprites are: Fewer images for the browser to download, which means...

  • By
    HTML5’s placeholder Attribute

    HTML5 has introduced many features to the browser;  some HTML-based, some in the form of JavaScript APIs, but all of them useful.  One of my favorites if the introduction of the placeholder attribute to INPUT elements.  The placeholder attribute shows text in a field until the...


  1. Saved, for later use… :)

    Thanks, Btw I like the way you display PHP code on your site.


  2. This only checks if an email account can exist for the domain in question. For example I tested and it came back valid. There is no email account of that name.

    If my host is typical, all domains in an account will have the MX record set – which is why even domains I own with no email addresses attached have come back valid. Also most email addresses given by spammers are This function, though it looks useful, has no validity as you will get false positives more often than false negatives.

  3. @tiggsy: You’re correct but it at least prevents someone from using “”.

  4. I have hunted around, and what you have given is the best that can be done, as one can’t rely on an email server being sufficiently insecure as to deny the existence of an email address. Many just swallow them without comment, but not all.

  5. hi david
    Is there anyway to check the user a/c also from that domain… and do u know how to login to webmail from website.I have done a form to collect user id and pass but its not passing to that webmail . if u have any reference or idea it woild be a great help for me

  6. I know this is an old post but now that the “split” function has been deprecated. The following works.

    function domain_exists( $email, $record = 'MX' ) {
    list( $user, $domain ) = explode( '@', $email );
    return checkdnsrr( $domain, $record );

  7. Robert Ameeti

    A domain need not have an MX record to receive mail. It only takes an A record to receive mail. An MX record is used specifically when the MX records is needed to point to a host other than that which the A record points to.


  8. This is returning high in Google so I thought I’d add a warning here- this doesn’t work for many servers. As per the the PHP manual for getmxrr (“do not use this function to verify email addresses”), this will return a false positive as the localhost is returned if no MX record is found. Apparently this is correct behaviour: “according to RFC 2821 when no mail exchangers are listed, hostname itself should be used as the only mail exchanger with a priority of zero”.

    Get around this by appending a “.” to the end of the domain, as in:

    $mx_record_really_exists = getmxrr('',$mx_records_output_array);

    Also works for A record checks with checkdnsrr()

  9. tanvi

    Hi…I want converted code for ur PHP code…..Pls give its urgent

  10. NeoArc

    list() generates a warning if one of the variables are not set.
    Error messages impact the performance of a script, perhaps even more than regular expressions.

  11. Nice tip. I really need it.
    But is there any way to check if email adress exists (even after checking the MX record)?

  12. There is a way to go beyond checking the email domain exists and check to see if the email actually exists in the domain but I haven’t found the code online yet. This site does it.

  13. Ben


    My comment is a bit off topic.
    Do you an equivalent function in C and/or C# ?

    Or is there an API to use with other languages ?


  14. dibin

    without @ it become error

  15. Ravi
    function domain_exists($email){
            list($user, $domain) = explode('@', $email);
            $arr= dns_get_record($domain,DNS_MX);
                    return $arr[0]['target'];
    $email= '';
    if(domain_exists($email)) {
            echo('This MX records exists; I will accept this email as valid.');
    else {
            echo('No MX record exists;  Invalid email.');
  16. Óscar

    Yeah, good point to make your webserver a public DDoS’er

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!