|
Posted by Al on 09/21/05 21:00
Jim Moseby wrote:
> 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
Thre is a good reason why virtually everyone uses regex patterns for email validating.
[Back to original message]
|