我正在尝试向我的简报订阅应用实施电子邮件确认。如果用户输入有效数据并添加到数据库,则代码为:
else {
$insert = $db -> prepare ("INSERT INTO newsletter (name, email, cfkey, time) VALUES(:name, :email, :cfkey, NOW())");
$insert -> execute(array(':name' => $name, ':email' => $email, ':cfkey' => $cfkey));
//Success! - Notify user
$userMsg = '<br /><br /><h4><font color="0066FF">Thanks ' . $name . ', you have been added successfully.</font></h4>';
$name = "";
$email = "";
}// End ELSE
$mail_body = '<html>
<head>
<title>My name</title>
</head>
<body>
<p style="color: green";>Hello ' . $name . ',</p>
<div>please confirm your subscription - http://www.domain.com/activation.php?key=$cfkey </div>
<p>~The temam</p>
</body>
</html>';
$subject = "Subsc";
$headers = "From: domain@domain.com\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$email";
$mail00 = mail($to, $subject, $mail_body, $headers);
}
if ($insert) {
echo "inserted in db";
}
else {
echo "not inserted in db";
}
/*
if ($mail00) {
echo "<br>Email sent!</br>";
}
else {
echo "<br>Email not sent</br>";
}
*/
?>
在代码的前面,声明了cfkey-generator:
$cfkey = md5 ($email . rand()); // Generate random hash value
“activation.php”脚本如下所示:
<?php
////Database connection
****
//Declare value
$key = $_GET['cfkey'];
$confirm = $db -> prepare ("SELECT * FROM newsletter WHERE email=?");
$confirm-> bindValue(1, $email);
$confirm -> execute ();
while($row = mysql_fetch_array($confirm)){
$db_key = $row ['cfkey'];
}
if ($key == $db_key) {
$db -> exec ("UPDATE newsletter SET confirmed='1'");
$db -> exec ("UPDATE newsletter SET cfkey='0'");
echo "Thank you!";
}
else {
echo "email confirmation was not valid";
}
?>
用户输入电子邮件后,会将其与唯一确认密钥一起成功存储在数据库中。我已经在这里待了大约一个星期,有两个问题:
1. 电子邮件未发送到输入的电子邮件地址。如果我将“to”字段设置为特定电子邮件(例如name@domain.com“),则会发送和接收电子邮件。但是,上述代码会发送电子邮件但未收到。
2. 永远不会验证电子邮件。上面的代码似乎不起作用 - 即使收到带有唯一cfkey的电子邮件,点击它后它总是“未经验证”。