VB icon

MEL :: Better Check Email Function

Email
Submitted on: 1/1/2015 9:56:00 PM
By: Melvin D. Nava (from psc cd)  
Level: Intermediate
User Rating: By 7 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 2966
 
     **UPDATED** This function will double check and validate an E-mail address by checking the sintaxis first and the domain's MX, A and CNAME records to be valid and active. It will return TRUE if the email is valid or FALSE if not, very simple. The best approach I've made to validate an Email. Let me know this has been useful, your comments and suggestions are very much appreciate it. **Please Vote**
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: MEL :: Better Check Email Function
// Description:**UPDATED** This function will double check and validate an E-mail address by checking the sintaxis first and the domain's MX, A and CNAME records to be valid and active. It will return TRUE if the email is valid or FALSE if not, very simple. The best approach I've made to validate an Email. Let me know this has been useful, your comments and suggestions are very much appreciate it. **Please Vote**
// By: Melvin D. Nava (from psc cd)
//
// Assumes:Relies on the checkdnsrr PHP Function to do the DNS work. Not available for Windows. I've included a replacement (ONLY WIN32)
//**************************************

<?php
//
// CHECK EMAIL FUNCTION
//***********************
function check_email_mx($email) { 
	if( (preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $email)) || 
		(preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/',$email)) ) { 
		$host = explode('@', $email);
		if(checkdnsrr($host[1].'.', 'MX') ) return true;
		if(checkdnsrr($host[1].'.', 'A') ) return true;
		if(checkdnsrr($host[1].'.', 'CNAME') ) return true;
	}
	return false;
}
//
//EXAMPLE
//***********************
if (check_email_mx("bill.gates@linux.com")) {
	echo "<p><font color=blue>";
	echo "<p>Email is valid, domain exists and has a valid MX host";
	echo "</font>";
} else {
	echo "<p><font color=red>";
	echo "Either your email is not valid, domain doesn't exists or there is no valid MX host available";
	echo "</font>";
}
//
// FIX FOR WINDOWS
// PROGRAMMERS
//***********************
// checkdnsrr is not available
// under windows I so included
// the next replacement. You
// may remove this if you are 
// gonna publish later over 
// any Linux/Unix OS
//
// thanx Jon Kriek for next snippet
// and Rickard Sj÷quist for notice
// Melvin D. Nava.
//
if (!function_exists('checkdnsrr')) {
	function checkdnsrr($host, $type = '') {
		if(!empty($host)) {
			if($type == '') $type = "MX";
			@exec("nslookup -type=$type $host", $output);
			while(list($k, $line) = each($output)) {
				if(eregi("^$host", $line)) {
					return true;
				}
			}
			return false;
		}
	}
}
?>


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.