MEL :: Better Check Email Function
**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**
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
<?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;
}
}
}
?>
Original Comments (3)
Recovered from Wayback Machine