How to validate email id using checkdnsrr() function in PHP

Are you tired of dummy email id’s being used on your website. Like test@test.com, abc@abc.com etc ?

You can use PHP function

checkdnsrr()

It checks whether the dns value is valid or not. Only the email id with valid domain name will be accepted 😉

This is inspired from the original post of one of our Author SNEHAL. Originally posted on his blog.

checkdnsrr() function will take an email address and performs a DNS lookup on the specified host (either a hostname or IP address). You can use this function before sending emails. The function will return an error informing the user that the domain related to email address probably doesn’t exist.

This function can look up the following record types:

bool checkdnsrr (string $host [, string $type = “MX” ] )

string $host: Hostname or IP address to check
string $type (optional): Type of record to check for

Record Type Details
________________________________________________________

A : Address: Defined in RFC 1035.
ALL : Any of the valid types.
CNAME : Canonical Name: Defined in RFC 1035.
MX : Mail Exchanger: Defined in RFC 1035.
NS : Name Server: Defined in RFC 1035.
PTR : Pointer: Defined in RFC 1035.
SOA : Start of Authority: Defined in RFC 1035.
________________________________________________________

Example :

<?php
$email = "test@gmail.com";
echo validate_email($email);

function validate_email($email){

$exp = "/^(([^()[\]\\.,;:\s@\”]+(\.[^()[\]\\.,;:\s@\”]+)*)|(\”.+\”))@(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/”;

if(preg_match($exp,$email)){

if(checkdnsrr(array_pop(explode(“@”,$email)),”MX”)){
//array_pop : returns the last value of an array.
return “Valid Email”;
}else{
return “Invalid Email”;
}

}else{

return “Invalid ID”;

}
}

?>

Please provide your feedback and suggestions in comment section. To get latest Update follow our blog.

Advertisements

4 thoughts on “How to validate email id using checkdnsrr() function in PHP

  1. Hello! This is kind of off topic but I need some guidance from an established blog. Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about making my own but I’m not sure where to begin. Do you have any tips or suggestions? Appreciate it For news and guidance about how to Create A Website visit 4buildwebsite.blogspot.com.

    Like

    • Hi.. you don’t really need to have web development knowledge to create a blog.. you can create you free blog from wordpress.com or blogspot.com. There are many others too who offers this facility but wordpress.com is best

      Like

  2. L’idée est bonne, mais ce formulaire refusera tous les nouveaux domaines avec un suffixe en .paris, .voyage ou des centaines d’autres…. Par ailleurs lisez la doc: il n’y a aucune obligation à définir un champs MX pour qu’un domaine puisse recevoir des email.

    Like

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