How to check if an email address exists or not using php

We make lot of websites every month and 90% of them have a registration page or a contact us page where users have to fill their email id for get in touch with site owner.We hope that they will provide correct email address but what if user fill out a wrong email id that is not actually exists.In that case it will just fill one record in our database nothing else and if lot of users will do the same then it will make a burden on our database.

So before filling our database or executing a script we have to check if the email id provided by user is really exists or that is a bogus email address.
While there’s no foolproof way to make sure a user isn’t giving you a completely bogus email address, you can at least help cut down on the problem by making sure that email addresses your site is given at least correspond to a real domain.

 

Php provides pre defined functions to check this.We can do this by two ways.
1.Using getmxrr function of php
or
2.Using checkdnsrr function of php

Let me explain one by one.

1.Using getmxrr function of php.

getmxrr function Get MX records corresponding to a given Internet host name.
Every email address has host name after @ sign Exam. harry@gmail.com means gmail.com is host name.

getmxrr  ( string $hostname  , array &$mxhosts  [, array &$weight  ] )

This function has 3 Parameters. first Parameters is host name and that is compulsory and 2 Parameters is mxhosts (array) and that is compulsory and 3 Parameters weight (array) is optional.
mxhosts is an array.A list of the MX records found is placed into the array mxhosts .
If the weight array is given, it will be filled with the weight information gathered.

Now I show you how to use this function:-

<?php
$mailparts                    =    explode( ‘@’, ‘abc@yahoo.com’, 2 );
$domain                        =    $mailparts[1];
$mxFound                    =    false;
while ( strpos( $domain, ‘.’ ) !== false ) {
// Validate domain:
$mxRecords            =    array();
$mxWeights            =    array();
if ( @getmxrr( $domain . ‘.’, $mxRecords, $mxWeights ) ) {
$mxFound    =    true;
break;
} else {
$subDomains        =    explode( ‘.’, $domain, 2 );
if ( count( $subDomains ) == 2 ) {
$domain        =    $subDomains[1];
} else {
break;
}
}
}
print_r($mxRecords);
echo ‘<hr>’;
print_r($mxWeights);
?>

First we will take email address filled by user and explode that with @.This will make an array with 2 indexes and on 2th index there will be host name like this
$mailparts(0=>’abc’,1=>’yahoo.com’)

Then we are checking if the host name is valid or not .After that we are passing host name in getmxrr function.Don’t get confused by $mxRecords and $mxWeights because we just have to declare them nothing else , getmxrr function will return both arrays with values if successful or blank if not successful.

At the end we will check both arrays .If email id is valid then both array will contain some values.
$mxRecords will contain maximum host names for that domain and $mxWeights will contain weight information gathered.
So if arrays are empty then email id not exists and if arrays have some values then email id exists.

2.Using checkdnsrr function of php.

The checkdnsrr function Check DNS records corresponding to a given Internet host name or IP address. It has this format:

int checkdnsrr(string $host [,string $type]);

This PHP function checks the DNS records for the given host to see if there are any records of the specified type. Note that the type parameter is optional, and if you don’t supply it then the type defaults to “MX” (which means Mail Exchange). If any records are found, the function returns TRUE or 1. Otherwise, it returns FALSE or 0.
<?php

// take a given email address and split it into the username and domain.
$mailparts                    =    explode( ‘@’, ‘abc@yahoo.com’, 2 );
$domain                        =    $mailparts[1];
if (checkdnsrr($domain, “MX”)) {
// this is a valid email domain!
}
else {
// this email domain doesn’t exist!
}
?>

you can use both functions (getmxrr and checkdnsrr) or one of them to check if email is exists.Using both of them will be good so it will be more secure.
Before some years these function were available only for linux but these function now available also on Windows platforms.

Advertisements
By Rz Rasel Posted in Php

6 comments on “How to check if an email address exists or not using php

  1. Hi there just happened upon your blog via Google after I entered in, “How to check if an email address exists or not using php | arzrasel” or perhaps something similar (can’t quite remember exactly). Anyways, I’m pleased I found it because your content is exactly what I’m looking for (writing a college paper) and I hope you don’t mind if I gather some information from here and I will of course credit you as the source. Appreciate it.

  2. i have tried your above code for method 1 i.e getmxrr and in second also
    suppose i have enter email address like: asdjhdkjashdkjsahdkjh@gmail.com
    this email address is not exist in gmail and according to your above code u are telling if any one array getting null value than that email address is not exist but wherever i have enter above email address and other email address that is not exist to that domain in both array i m getting some value
    i have used code like
    <?php
    $mailparts=explode( '@', 'asdjhdkjashdkjsahdkjh@gmail.com', 2 );
    $domain=$mailparts[1];
    $mxFound=false;
    while ( strpos( $domain, '.' ) !== false )
    {
    // Validate domain:
    $mxRecords = array();
    $mxWeights = array();
    if ( @getmxrr( $domain . '.', $mxRecords, $mxWeights ) )
    {
    $mxFound=true;
    //echo "true";
    break;
    }
    else
    {
    $subDomains=explode( '.', $domain, 2 );
    if ( count( $subDomains ) == 2 )
    {
    $domain=$subDomains[1];
    }
    else
    {
    //echo "not found";
    break;
    }
    }
    }
    print_r($mxRecords);
    echo '’;
    print_r($mxWeights);
    ?>
    output:
    Array ( [0] => alt1.gmail-smtp-in.l.google.com [1] => alt2.gmail-smtp-in.l.google.com [2] => alt3.gmail-smtp-in.l.google.com [3] => alt4.gmail-smtp-in.l.google.com [4] => gmail-smtp-in.l.google.com )

    second array:Array ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 5 )

    you also try
    if is there any code mistake than please tell me the correct code
    Thanks
    i hope you will reply me

  3. Pingback: URL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s