On Thursday 26 June 2008 05:34:04 pm Pritpal Bedi wrote:
> Hello All,
>
> Is there a way to verify if an email address is valid or fake ?
>
> I am looking for pure Harbour solution.
>
> Regards
> Pritpal Bedi

I have a php form that does it if it could help.

At least it would walk you through the concepts... It leaves you to solve the 
need to query the DNS for the MX list.

# cat do_sendfeedback.php3
<?
// Change the info on the next two lines of code
$mailto = "[EMAIL PROTECTED]";             // give me the email to address
$mail_subject = "Feedback Form from The-Oasis"; // give me the subject line of 
mail note
$feedback_message = "For taking the time to write a message We will get back 
to you as soon as
possible I appreciate your message.";

// ******************************************
// DO NOT change any code below this line   *
// ******************************************
$error = "";

// This script was origionally written to accept variables directly from the 
post method
// The method now passes arguments in a array called $_POST
// Store array eliments in ordigional variables so we don't have to rewrite 
function.
$message = $_POST[message];
$sender_name = $_POST[sender_name];
$sender_email = $_POST[sender_email];


// Test the customer to see if he passed us the stuff..
if( $message === "" )
{
   echo "<P align=center>Why send an empty message?</P>";
   return;
}

if( $sender_name === "" )
{
   echo "<P align=center>You didn't provide your name...</P>";
   return;
}

if( $sender_email === "" )
{
   echo "<P align=center>You didn't provide your email address...</P>";
   return;
}

if( verifyEmail( $sender_email, $error ) === TRUE )
{

   $msg  = "Sender's Full Name:\t$sender_name\n";
   $msg .= "Sender's Email:\t\t$sender_email\n";
   $msg .= "Additional Message:\t$message\n\n";

   $mailheaders  = "From: $sender_email\n";
   $mailheaders .= "Reply-To: $sender_email\n\n";

   mail( "$mailto", "$mail_subject", strip_tags(stripslashes( $msg ) ), 
$mailheaders );

   echo "<H1 align=center>Thank You, $sender_name</H1>";
   echo "<P align=center>$feedback_message</P>";
}
else
{
   echo "<CENTER>There is a problem with your email
address:<P> <B>$sender_email</B><P>$error</CENTER>";
}

  /*
        ** Function: verifyEmail
        ** Input: STRING address, REFERENCE error
        ** Output: BOOLEAN
        ** Description: Attempts to verify an email address by
        ** contacting a mail exchanger.  Registered mail
        ** exchangers are requested from the domain controler first,
        ** then the exact domain itself.  The error argument will
        ** contain relevant text if the address could not be
        ** verified.
        */

        function verifyEmail($address, &$error)
        {
                global $SERVER_NAME;

                list($user, $domain) = split("@", $address, 2);

                //make sure the domain has a mail exchanger
                if(checkdnsrr($domain, "MX"))
                {
                        //get mail exchanger records
                        if(!getmxrr($domain, $mxhost, $mxweight))
                        {
                                $error = "Could not retrieve mail 
exchangers!<BR>\n";
                                return(FALSE);
                        }
                }
                else
                {
                        //if no mail exchanger, maybe the host itself
                        //will accept mail
                        $mxhost[] = $domain;
                        $mxweight[] = 1;
                }

                //create sorted array of hosts
                for($i = 0; $i < count($mxhost); $i++)
                {
                        $weighted_host[($mxweight[$i])] = $mxhost[$i];
                }
                ksort($weighted_host);

                //loop over each host
                foreach($weighted_host as $host)
                {
                        //connect to host on SMTP port
                        if(!($fp = fsockopen($host, 25)))
                        {
                                //couldn't connect to this host, but
                                //the next might work
                                continue;
                        }


                        /*
                        ** skip over 220 messages
                        ** give up if no response for 10 seconds
                        */
                        set_socket_blocking($fp, FALSE);

                        $stopTime = time() + 10;
                        $gotResponse = FALSE;

                        while(TRUE)
                        {
                                //try to get a line from mail server
                                $line = fgets($fp, 1024);

                                if(substr($line, 0, 3) == "220")
                                {
                                        //reset timer
                                        $stopTime = time() + 10;
                                        $gotResponse = TRUE;
                                }
                                elseif(($line == "") AND ($gotResponse))
                                {
                                        break;
                                }
                                elseif( time() > $stopTime )
                                {
                                        break;
                                }
                        }

                        if(!$gotResponse)
                        {
                                //this host was unresponsive, but
                                //maybe the next will be better
                                continue;
                        }

                        set_socket_blocking ($fp, TRUE);

                        //sign in
                        fputs($fp, "HELO $SERVER_NAME\r\n");
                        fgets($fp, 1024);

                        //set from
                        fputs($fp, "MAIL FROM: <[EMAIL PROTECTED]>\r\n");
                        fgets($fp, 1024);

                        //try address
                        fputs($fp, "RCPT TO: <$address>\r\n");
                        $line = fgets($fp, 1024);

                        //close connection
                        fputs($fp, "QUIT\r\n");
                        fclose($fp);

                        if(substr($line, 0, 3) != "250")
                        {
                                //mail server doesn't recognize
                                //this address, so it must be bad
                                $error = $line;
                                return(FALSE);
                        }
                        else
                        {
                                //address recognized
                                return(TRUE);
                        }
                }

                $error = "Unable to reach a mail exchanger!";
                return(FALSE);
        }

?>


-- 
Waiting for sunspots.
_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to