email validation (no regex)

From: Date: Wed, 21 Sep 2005 17:49:35 +0000
Subject: email validation (no regex)
Groups: php.general 
Request: Send a blank email to php-general+get-222895@lists.php.net to get a copy of this message
I threw together this totally untested and unreliable code to solicit comments on whether or not this is a good way to validate emails. Consider the following: <pseudocode> function validate_email($email){ if (str_word_count($email,'@')!=1){return('Not a proper email address');} $parts=explode('@',$email); $name=$parts[0]; $domain=$parts[1]; $mxconnect=FALSE; if (!getmxrr($domain,$mxhosts)){ return('Invalid domain'); }//if foreach($mxhosts as $mxhost){ if($fp=fsockopen($mxhost,25)){ $mxconnect=TRUE; fwrite($fp,"EHLO test"); $response=fread($fp,256); fwrite($fp, "Mail From: jim@example.com".chr(13)); $response=fread($fp,256); fwrite($fp, 'RCPT To: '.$email.chr(13)); $response=fread($fp,256); $parts=explode(' ',$response); if ($parts[0]!='250'){ fwrite($fp,'QUIT'.chr(13)); fclose($fp); return('Unknown Recipient'); }//if }//if }//foreach if (!$mxconnect){return('Could not connect to MX');} fwrite($fp,'QUIT'.chr(13)); fclose($fp); return('OK'); }//function validate_email </pseudocode> So, what is the general thought about validating email addresses in this manner? JM

« previous php.general (#222895) next »