如何在注册后发送电子邮件

时间:2010-12-25 18:53:10

标签: database email registration activation

之后有人在网站上注册,通常会将邮件发送到他的邮件帐户。但要生成此链接或可以在此链接中放置哪些信息,以便可以使用它来激活用户帐户??

4 个答案:

答案 0 :(得分:1)

您可以放置​​任何可以识别有效用户的东西

1-哈希值

2-加密字符串

3- A Guid

当用户点击链接时,您可以验证该值。

答案 1 :(得分:1)

检查这部分代码:

生成代码和电子邮件:

/* if $acces = 0 everything is perfect so the system send a confirmation mail */ 
                                if($acces == 0) 
{ 
    print("<br>A mail has been send to " . $mail . "<br><br>") ; 

    /* prepare the vars */ 
    $activ = $user . $pass ; 
    $code = md5($activ) ; 
    /* to how send to mail */ 
    $to = $mail ; 
    /* prepare the subject */ 
    $subject = "You need to confirm you registration to " . $_SERVER['HTTP_HOST'] ; 
    /* start writing the message */ 
    $message = "Hello " . $user . ",\r\n\r\n" ; 
    $message .= "Thank you for registering at " . $_SERVER['HTTP_HOST'] . " Your account is created and must be activated before you can use it.\r\n" ;
    $message .= "To activate the account click on the following link or copy-paste it in your browser :\r\n\r\n" ; 
    $message .= "http://" . $_SERVER['HTTP_HOST'] . "/~carron/registration/register_send.php?user=" . $user . "&activation=" . $code . "\r\n\r\n" ; 
    $message .= "After activation you may login to http://" . $_SERVER['HTTP_HOST'] . " using the following username and password:\r\n\r\n" ; 
    $message .= "Username - " . $user . "\r\nPassword - " . $pass . "\r\n" ; 

    /* To send HTML mail, you can set the Content-type header. */ 
    $headers  = "MIME-Version: 1.0"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1"; 

    /* set up additional headers */ 
    $headers .= "To: " . $to . "<br>\n" ; 
    $headers .= "From: " . $from . $addmail ; 

    /* writing data in the base */ 
    $query = "INSERT INTO registration (user, pass, activ, mail) VALUES ('$user', '$pass', '$code', '$mail') ;" ; 

    $result = mysql_query($query, $db); 

    if ($result == false) 
        die("Failed " . $query); 
    else 
        { 
            /* everything went well so we can mail it now */ 
            mail($to, $subject, $message, $headers); 
        } 
} 

检查激活:

/* controle if the validation link is right */ 
$x = 0 ; 

$query = "SELECT user, pass, activ, mail FROM registration WHERE user = '" . $username . "';" ; 

$result = mysql_query($query, $db); 

if ($result == false) die("Failed " . $query); 


while ($fields = mysql_fetch_row($result)) 
        { 
         for ($i=0, $max=sizeof($fields) ; $i < $max ; $i++) 
                { 
                    $tmp[$i] = $fields[$i] ; 
                } 

         /* the activation link is right so we can update 
         the datas in the data base */ 
         if($activation == $tmp[2] AND $username == $tmp[0]) 
            { 
                $x = 1 ; 
                $query2 = "UPDATE registration SET activated = '1' WHERE user = '" . $username . "' AND activ = '" . $activation . "' ;" ; 

                $result2 = mysql_query($query2, $db); 

                if ($result2 == false) 
                    die("Failed " . $query2); 
            } 
         else 
            $x = -1 ; 
        } 

/* give a confirmation message to the user */ 
if($x == 1) 
    print($username . " your activation has been done perfectly<br> Thank you...") ; 
else 
    print($username . " your activation has not been done corectly<br> Please try again later...") ; 

Script from PHPclasses.org

答案 2 :(得分:0)

用户注册后,您可以使用uniqid()创建激活码并存储在数据库中。然后在邮件中,提供如下链接:http://....../activate.php?code=[uniqid()]
activate.php中,您可以从数据库中读取激活代码并进行检查。

答案 3 :(得分:0)

我们的想法是拥有一个只有电子邮件收件人知道的链接。因此,当您在网站上访问该链接时,您知道有人已阅读您发送的电子邮件并点击该链接,因此您可以假设注册的人和阅读该电子邮件的人是相同的。

因此,您只需要一个不容易猜到的链接。随机选择一些东西(并将其记录在用户的个人资料中)或散列用户名+种子或其他内容。