| px | top | add code | search | signup | login | help |
<?php
/**********************************************
CODE: [MEL] Better Check Email Function
AUTHOR: Melvin D. Nava
URL: http://mdnava.network.com.ve/
EMAIL: In my website
DESCRIPTION:
This function will double check and
validate an email by checking the sintaxis and
the domain A, MX and CNAME records to exist.
The best approach I ve made to validate an Email.
It will return TRUE if the email is valid or FALSE
if not, very simple. Let me know this has been
useful to you. Comments and suggestions are very
much appreciate it.
REQUIREMENTS:
It will work on any PHP enabled
server but it relies on the checkdnsrr PHP
Function to do the DNS work that is not
available for Windows so I ve included a
checkdnsrr replacement for win32 programmers.
** Comment the checkdnsrr replacement when
publishing into any Linux/Unix server **
TERMS OF USE:
Feel free to use or modify this code to fit
your needs. This code is given as it is in
the hope it will be useful but the author
takes no responsability on any problem or
damage it may come from its use.
**********************************************/
//
// 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)) ) {
$dom = explode('@', $email);
if(checkdnsrr($dom[1].'.', 'MX') ) return true;
if(checkdnsrr($dom[1].'.', 'A') ) return true;
if(checkdnsrr($dom[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 so include
// the next replacement in
// your code and COMMENT IT
// OVER IF PUBLISHING LATER
// ON ANY LINUX/UNIX OS
//
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;
}
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.