<?php
	/*
	** File: validateEmail
	** Description: ensure that an email address is valid
	** Version: $Revision: 1.4 $
	** Created: 03/15/99
	** Author:Jon S. Stevens
	** Email: jon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/10/11 23:04:04 $
	**
	** Log : $Log: validateEmail,v $
	** Log : Revision 1.4  1999/10/11 23:04:04  leon
	** Log : Removed all require()'s, added block to stop double include()'s
	** Log :
	** Log : Require wasn't giving any performance advantage because it was always
	** Log : being called with a variable.  Futhermore, using return inside a
	** Log : require()'d file does not work in PHP4.
	** Log :
	** Log : Revision 1.3  1999/08/18 22:32:07  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.2  1999/08/16 19:05:47  leon
	** Log : Updated validateEmail with Jon's latest version.
	** Log :
	** Log : Revision 1.1  1999/07/28 19:41:08  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**  Copyright 1998 Jon S. Stevens, Clear Ink
	**  This code has all the normal disclaimers.
	**  It is free for any use, just keep the credits intact.
	*/

	/*
	** Ensure that this function isn't included twice
	*/

	$included_flag = 'INCLUDE_' . basename(__FILE__);
	
	if(defined($included_flag))
		//if this module has been included already, then the
		//constant will be defined.  Note the unusual use of
		//the return statement here.  Control will return to
		//the file that included it.
		return;
	
	define($included_flag, TRUE);	


	function validateEmail ( $email )
	{
		global $SERVER_NAME;
		$return = array ( false, "" );
		list ( $user, $domain )  = split ( "@", $email, 2 );
		$tld = $domain;
		if ( checkdnsrr ( $tld, "MX" ) )
		{
			if ( getmxrr ( $tld, $mxhosts, $weight ) )
			{
				for ( $i = 0; $i < count ( $mxhosts ); $i++ )
				{
					$fp = fsockopen ( $mxhosts[$i], 25 );
					if ( $fp )
					{
						$s = 0;
						$c = 0;
						$out = "";
						set_socket_blocking ( $fp, false );
						do
						{
							$out = fgets ( $fp, 2500 );
							if ( ereg ( "^220", $out ) )
							{
								$s = 0;
								$out = "";
								$c++;
							}
							else if ( ( $c > 0 ) && ( $out == "" ) )
							{ break; }
							else
							{ $s++;	}
							if ( $s == 9999 ) { break; }
						
						} while ( $out == "" );
						set_socket_blocking ( $fp, true );
	
						fputs ( $fp, "HELO $SERVER_NAME\n" );
						$output = fgets ( $fp, 2000 );
						fputs ( $fp, "MAIL FROM: <info@" . $tld . ">\n" );
						$output = fgets ( $fp, 2000 );
						fputs ( $fp, "RCPT TO: <$email>\n" );				
						$output = fgets ( $fp, 2000 );
						if ( ereg ( "^250", $output ) )
						{
							$return[0] = true;
						}
						else
						{
							$return[0] = false;
							$return[1] = $output;
						}
						fputs ( $fp, "QUIT\n" );
						fclose( $fp );
	
						if ( $return[0] == true )
						{ break; }
					}
				}
			}
		}
		return $return;
	}

?>