Important alert: (current site time 7/15/2013 11:02:05 AM EDT)
 

VB icon

checkEmail function

Email
Submitted on: 12/10/2005 10:11:24 AM
By: Youry De Winter  
Level: Advanced
User Rating: By 1 Users
Compatibility: PHP 4.0, PHP 5.0
Views: 11656
author picture
(About the author)
 
     Check if a given email address is valid by checking: - is the format correct? - has the server of the given domain an MX record?
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: checkEmail function
// Description:Check if a given email address is valid by checking:
 - is the format correct?
 - has the server of the given domain an MX record?
// By: Youry De Winter
//
// Inputs:Example of use:
checkEmail('someone@somewhere.com')
//
// Returns:TRUE when the address is correct format and the domain has an MX record or FALSE when not.
//
// Assumes:If u are using the function on a non-win32 platform, u could use the lighter version, which i included at the bottom.
The reason why I had to make 2 version is simple:
 Win32 does not have the needed modules for using getmxrr() functions in PHP, so I had to re-program the function of PHP itself in order to have a short code.
//
// Side Effects:Using the script on Win32 server platforms makes it run a little bit slower.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1867&lngWId=8//for details.//**************************************

//OVERALL COMPATIBLE VERSION
function checkEmail($Email) {
	if ((substr_count(PHP_OS, 'win')) || (substr_count(PHP_OS, 'Win')) || (substr_count(PHP_OS, 'WIN'))) {
		function getmxrr($hostname, &$mxhosts) {
			$mxhosts = array();
			exec('nslookup -type=mx '.$hostname, $result_arr);
			foreach($result_arr as $line) {
				if (preg_match("/.*mail exchanger = (.*)/", $line, $matches)) {
					$mxhosts[] = $matches[1];
				}
			}
			return( count($mxhosts) > 0 );
		}
	}
	if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $Email)) {
		return FALSE;
	}
	list($Username, $Domain) = split("@",$Email);
	if(getmxrr($Domain, $MXHost)) {
		return TRUE;
	} else {
		if(fsockopen($Domain, 25, $errno, $errstr, 30)) {
			return TRUE; 
		} else {
			return FALSE; 
		}
	}
}
//NON WIN32-SERVER COMPATIBLE VERSION
function checkEmail($Email) {
	if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $Email)) {
		return FALSE;
	}
	list($Username, $Domain) = split("@",$Email);
	if(getmxrr($Domain, $MXHost)) {
		return TRUE;
	} else {
		if(fsockopen($Domain, 25, $errno, $errstr, 30)) {
			return TRUE; 
		} else {
			return FALSE; 
		}
	}
}


Other 4 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 Advanced 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
12/22/2006 2:54:11 AMBB Bohara

hi

how can i implement in php?
(If this comment was disrespectful, please report it.)

 
5/19/2007 8:45:37 PMpak_megan

Nice piece of code... could be better though. I'd rate for 5 if you add a routine to check if the email address is registered, it's the only weakness i found.

Good work, and thank you for sharing
(If this comment was disrespectful, please report it.)

 

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.