How to check if an email address exists or not using php
Nov 11, 2009 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.
Tags: DNS, domain, email address, email bounce, email bouncing, emailId, host array, hostname, ip address, valid email
November 11th, 2009 at 1:36 pm
[...] This post was mentioned on Twitter by quicksolution, quicksolution. quicksolution said: Published a new post: How to check if an email address exists or not using php @http://zz.gd/cdc458 [...]
December 23rd, 2009 at 3:29 am
When one considers the issue at hand, i have to agree with your determinations. You intelligibly show cognition about this theme and i have much to discover after reading your article.Lot’s of salutations and i will come back for any further updates.
December 26th, 2009 at 2:08 am
I want to quote your post in my blog. It can?
And you et an account on Twitter?