Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EMAIL VALIDATION QUESTION

Status
Not open for further replies.

dadms

Technical User
Mar 8, 2003
67
US
I am trying to come up with a good way to validate a users email address is working. I have a form that the user will enter their email address. Upon submission I would like to send them an email and if it does not get rejected to update the MYSQL database with their email address. Does this sound possible?
 
It's not really possible to do it the way you envision.

When PHP hands an email address off to a mail server, the mail() function returns a status which states whether the local mail server accepted the message. It does not return that the message was successfully delivered to its recipient.

This is because an email can be handed off through any number of mail servers.

The typical solution to validating the existence of an email address is to create the record in the database. The database record contains some kind of unique, nonconsecutive id and a flag as to whether the email is validated (defaults to "invalid"). The script then sends the email, which contains a link to a validation script.

The link, when clicked, will pass the database record's unique id to the validation script. It'll be of the form: [ignore][/ignore]

The validation script will take the id, go to the database, find the record which has a matching id, and set the email flag to "valid".

Want the best answers? Ask the best questions: TANSTAAFL!!
 
sleipnir214 hit it on the head. That really is the best way to go about doing what you want to do.

Alternatly, if you're generating passwords you can include the login information in the email.. if they provide a bad address, they won't get it

Personaly both couldn't hurt if you want to be extra paranoid ;)
 
yep that's the way to do it ;) with the md5 function you can generate some nice fuzzy keys for the link
 
I found this code on . I have seen it on more than one website. It says that it actually check the email address by querying the domain etc.. Let me know what you think.

function SnowCheckMail($Email,$Debug=false)
{
global $HTTP_HOST;
$Return =array();
// Variable for return.
// $Return[0] : [true|false]
// $Return[1] : Processing result save.

if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email))
{
$Return[0]=false;
$Return[1]="${Email} is E-Mail form that is not right.";
if ($Debug) echo &quot;Error : {$Email} is E-Mail form that is not right.<br>&quot;;
return $Return;
}
else if ($Debug) echo &quot;Confirmation : {$Email} is E-Mail form that is not right.<br>&quot;;

// E-Mail @ by 2 by standard divide. if it is $Email this &quot;lsm@ebeecomm.com&quot;..
// $Username : lsm
// $Domain : ebeecomm.com
// list function reference : // split function reference : list ( $Username, $Domain ) = split (&quot;@&quot;,$Email);

// That MX(mail exchanger) record exists in domain check .
// checkdnsrr function reference : if ( checkdnsrr ( $Domain, &quot;MX&quot; ) ) {
if($Debug) echo &quot;Confirmation : MX record about {$Domain} exists.<br>&quot;;
// If MX record exists, save MX record address.
// getmxrr function reference : if ( getmxrr ($Domain, $MXHost)) {
if($Debug) {
echo &quot;Confirmation : Is confirming address by MX LOOKUP.<br>&quot;;
for ( $i = 0,$j = 1; $i < count ( $MXHost ); $i++,$j++ ) {
echo &quot; Result($j) - $MXHost[$i]
<BR>&quot;;
}
}
}
// Getmxrr function does to store MX record address about $Domain in arrangement form
to $MXHost.
// $ConnectAddress socket connection address.
$ConnectAddress = $MXHost[0];
}
else {
// If there is no MX record simply @ to next time address socket connection do .
$ConnectAddress = $Domain;
if ($Debug) echo &quot;Confirmation : MX record about {$Domain} does not exist.<br>&quot;;
}

// fsockopen function reference : $Connect = fsockopen ( $ConnectAddress, 25 );

// Success in socket connection
if ($Connect)
{
if ($Debug) echo &quot;Connection succeeded to {$ConnectAddress} SMTP.<br>&quot;;
// Judgment is that service is preparing though begin by 220 getting string after
connection .
// fgets function reference : if ( ereg ( &quot;^220&quot;, $Out = fgets ( $Connect, 1024 ) ) ) {

// Inform client's reaching to server who connect.
fputs ( $Connect, &quot;HELO $HTTP_HOST\r\n&quot; );
if ($Debug) echo &quot;Run : HELO $HTTP_HOST<br>&quot;;
$Out = fgets ( $Connect, 1024 ); // Receive server's answering cord.

// Inform sender's address to server.
fputs ( $Connect, &quot;MAIL FROM: <{$Email}>\r\n&quot; );
if ($Debug) echo &quot;Run : MAIL FROM: <{$Email}><br>&quot;;
$From = fgets ( $Connect, 1024 ); // Receive server's answering cord.

// Inform listener's address to server.
fputs ( $Connect, &quot;RCPT TO: <{$Email}>\r\n&quot; );
if ($Debug) echo &quot;Run : RCPT TO: <{$Email}><br>&quot;;
$To = fgets ( $Connect, 1024 ); // Receive server's answering cord.

// Finish connection.
fputs ( $Connect, &quot;QUIT\r\n&quot;);
if ($Debug) echo &quot;Run : QUIT<br>&quot;;

fclose($Connect);

// Server's answering cord about MAIL and TO command checks.
// Server about listener's address reacts to 550 codes if there does not exist
// checking that mailbox is in own E-Mail account.
if ( !ereg ( &quot;^250&quot;, $From ) || !ereg ( &quot;^250&quot;, $To )) {
$Return[0]=false;
$Return[1]=&quot;${Email} is address done not admit in E-Mail server.&quot;;
if ($Debug) echo &quot;{$Email} is address done not admit in E-Mail server.<br>&quot;;
return $Return;
}
}
}
// Failure in socket connection
else {
$Return[0]=false;
$Return[1]=&quot;Can not connect E-Mail server ({$ConnectAddress}).&quot;;
if ($Debug) echo &quot;Can not connect E-Mail server ({$ConnectAddress}).<br>&quot;;
return $Return;
}
$Return[0]=true;
$Return[1]=&quot;{$Email} is E-Mail address that there is no any problem.&quot;;
return $Return;
}
?>
 
It attempts to send the email through direct communication over port 25. This is good -- it bypasses the server stuff.

But there are still problems.

First, a lot of hosting providers turn off socket programming on their PHP installations. You haven't stated your run environment, but it's something to consider.

Second, at best this script checks whether the address exists, not whether it is your user's address. Sending information that your user will have to respond to will give you more deterministic answers. I could, for example, put in &quot;president@whitehouse.gov&quot; and the script might accept it (depending on the White House's server's behavior). But that doesn't mean it's my address.

Third, some servers can be configured to accept mail from any address, but to send those emails to the postmaster. Again, these &quot;catch-all&quot; addresses may not belong to me.

Fourth, in the era of ubiquitous spam, a lot of spammers probe servers to see what addresses are available. So some servers will not give a response on an address' non-existence unless a message is sent and that message is checked by the server's antispam heuristics.

I strongly recommend that you first check for an email's well-formedness (see my FAQ: faq434-2408) then send an email to which your user will have to respond.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top