简体   繁体   English

在新用户注册时发送激活电子邮件

[英]Sending an Activation Email when a New User Registers

The code below is a login system that I am using. 下面的代码是我正在使用的登录系统。 It is supposed to allow a new user to register and then send the new user an activation email. 它应该允许新用户注册,然后向新用户发送激活电子邮件。 It is inserting the new user into the MySQL database, but it is not sending the activation email. 它将新用户插入MySQL数据库,但它没有发送激活电子邮件。 Any ideas why it's not sending the activation email? 任何想法为什么它不发送激活邮件?

Thanks in advance, 提前致谢,

John 约翰

header.php: header.php文件:

<?php
//error_reporting(0); 
session_start();
require_once ('db_connect.inc.php'); 
require_once ("function.inc.php"); 
$seed="0dAfghRqSTgx"; 
$domain =  "...com"; 


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>The Sandbox - <?php echo $domain; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="sandbox.css"> 
<div class="hslogo"><a href="http://www...com/sandbox/"><img src="images/hslogo.png" alt="Example" border="0"/></a></div>
</head>
<body>

login.php: login.php中:

<?php
if (!isLoggedIn())
{
    // user is not logged in.
    if (isset($_POST['cmdlogin']))
    {
        // retrieve the username and password sent from login form & check the login.
        if (checkLogin($_POST['username'], $_POST['password']))
        {
            show_userbox();
        } else
        {
            echo "Incorrect Login information !";
            show_loginform();
        }
    } else
    {
        // User is not logged in and has not pressed the login button
        // so we show him the loginform
        show_loginform();
    }

} else
{
    // The user is already loggedin, so we show the userbox.
    show_userbox();
}
?>


function show_loginform($disabled = false)
{

    echo '<form name="login-form" id="login-form" method="post" action="./index.php?'.$_SERVER['QUERY_STRING'].'"> 

    <div class="usernameformtext"><label title="Username">Username: </label></div> 
    <div class="usernameformfield"><input tabindex="1" accesskey="u" name="username" type="text" maxlength="30" id="username" /></div> 


    <div class="passwordformtext"><label title="Password">Password: </label></div> 
    <div class="passwordformfield"><input tabindex="2" accesskey="p" name="password" type="password" maxlength="15" id="password" /></div> 


    <div class="registertext"><a href="http://www...com/sandbox/register.php" title="Register">Register</a></div> 
    <div class="lostpasswordtext"><a href="http://www...com/sandbox/lostpassword.php" title="Lost Password">Lost password?</a></div> 

  <p class="loginbutton"><input tabindex="3" accesskey="l" type="submit" name="cmdlogin" value="Login" ';
    if ($disabled == true)
    {
        echo 'disabled="disabled"';
    }
    echo ' /></p></form>';


}

register.php: register.php:

<?php

require_once "header.php"; 

if (isset($_POST['register'])){

    if (registerNewUser($_POST['username'], $_POST['password'], $_POST['password2'], $_POST['email'])){

        echo "<div class='registration'>Thank you for registering, an email has been sent to your inbox, Please activate your account.
        <a href='http://www...com/sandbox/index.php'>Click here to login.</a>
        </div>";

    }else {

        echo "Registration failed! Please try again.";
        show_registration_form();

    }

} else {
// has not pressed the register button
    show_registration_form();   
}


?>

New User Function: 新用户功能:

function registerNewUser($username, $password, $password2, $email)
{

    global $seed;

    if (!valid_username($username) || !valid_password($password) || 
            !valid_email($email) || $password != $password2 || user_exists($username))
    {
        return false;
    }


    $code = generate_code(20);
    $sql = sprintf("insert into login (username,password,email,actcode) value ('%s','%s','%s','%s')",
        mysql_real_escape_string($username), mysql_real_escape_string(sha1($password . $seed))
        , mysql_real_escape_string($email), mysql_real_escape_string($code));


    if (mysql_query($sql))
    {
        $id = mysql_insert_id();

        if (sendActivationEmail($username, $password, $id, $email, $code))
        {

            return true;
        } else
        {
            return false;
        }

    } else
    {
        return false;
    }
    return false;

}

Send Activation Email function: 发送激活电子邮件功能

function sendActivationEmail($username, $password, $uid, $email, $actcode)
{
    global $domain;
    $link = "http://www.$domain/sandbox/activate.php?uid=$uid&actcode=$actcode";
    $message = "
Thank you for registering on http://www.$domain/,

Your account information:

username:  $username
password:  $password

Please click the link below to activate your account.

$link

Regards
$domain Administration
";

    if (sendMail($email, "Please activate your account.", $message, "no-reply@$domain"))
    {
        return true;
    } else
    {
        return false;
    }
}

Maybe because the function for sending email is mail and not sendMail? 也许是因为发送电子邮件的功能是邮件而不是sendMail? If the function sendMail is defined maybe there is an error in that function. 如果定义了函数sendMail,则该函数可能存在错误。

if (mail($email, "Please activate your account.", $message, "no-reply@$domain"))
{
    return true;
} else
{
    return false;
}

In addition to needing to use the mail function as mentioned by others, which is your primary problem, there is a small problem with this: 除了需要使用其他人提到的mail功能(这是您的主要问题)之外,还有一个小问题:

"no-reply@$domain"

PHP is expecting a header: PHP期待一个标题:

"From: no-reply@$domain"

or 要么

"Reply-To: no-reply@$domain"

This isn't the reason your script is failing (as mentioned above, it's using the wrong function), but it is still important to comply with the standards or else things may break when you don't expect them to. 这不是您的脚本失败的原因(如上所述,它使用了错误的功能),但遵守标准仍然很重要,否则当您不期望它们时可能会中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户在 Laravel 中更新电子邮件地址时发送电子邮件激活(确认)链接 - Sending email activation(confirmation) link when user UPDATING email address in Laravel 通过电子邮件发送用户激活链接。 这个怎么运作? - Sending user activation link via email. How it works? 为什么我在尝试使用phpmailer发送电子邮件激活新用户帐户时收到此错误 - Why am i getting this error when trying to send email for activation of new user account with phpmailer 向用户发送激活电子邮件 - Send activation email to user 用户注册和电子邮件激活 - user registration and email activation 新用户注册和收到激活电子邮件之间的4分钟延迟 - 4 Minute Delay Between New User Registration and Receipt of Activation Email PHP电子邮件未通过激活密钥发送 - PHP Email Not Sending With Activation Key Tank_Auth 当用户注册和激活失败并显示“不正确或过期”消息时显示警告 - Tank_Auth Shows warnings when user registers and activation fails with “Incorrect or Expired” message 当新用户向应用程序注册时添加表单字段 - Adding form fields when a new user registers with application 数据库正确注册用户,但没有发送 email - database registers user correctly but no email is send out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM