在php中激活用户表单电子邮件


Activate user form email in php

我正在为员工制作注册页面。一旦员工注册激活链接应发送给管理员电子邮件,一旦管理员点击该链接,员工应被激活。消息应该发送给员工电子邮件,他现在可以登录到自己的帐户。。。到目前为止,我写了一个代码,将员工详细信息存储在数据库中,并在管理电子邮件中发送消息继承我的代码。

<?php  
#database coding
if(!empty($_POST['txtfstname']) && !empty($_POST['txtlstname']) &&     !empty($_POST['txtemail']) && !empty($_POST['txtempno']))  
        {
    $con=mysqli_connect("servername","username",'password',"database");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    // escape variables for security
    $firstname = mysqli_real_escape_string($con, $_POST['txtfstname']);
    $lastname = mysqli_real_escape_string($con, $_POST['txtlstname']);
        $empno = mysqli_real_escape_string($con, $_POST['txtempno']);
            $pass = substr(hash('sha256', mt_rand()), 0, 50);
    $email = mysqli_real_escape_string($con, $_POST['txtemail']);
            $email_code = md5($_POST['txtfstname'] + microtime());
            if (!preg_match("/(['w'-]+'@['w'-]+'.['w'-]+)/",$email))
                                {  
                                }
            else
                {
             $sql="INSERT INTO empreg (first_name, last_name, email, emp_no, password, email_code)
             VALUES ('$firstname', '$lastname','$email','$empno','$pass','$email_code')";
             if (!mysqli_query($con,$sql))
               {
              die('Error: ' . mysqli_error($con));
               }
                echo "1 record added";
       }
mysqli_close($con);

#email Coding
# It's best to just make sure each element isn't empty, using the empty() function.  
# Note that if a field is not set, it is NULL, which is consdered empty, so there  
# is no need to use the isset() function as well. 

            $firstname = trim(stripslashes(htmlspecialchars($_POST['txtfstname']))); 
            $lastname = trim(stripslashes(htmlspecialchars($_POST['txtlstname']))); 
            $email = trim(stripslashes(htmlspecialchars($_POST['txtemail'])));
            $ip = $_SERVER['REMOTE_ADDR']; 
                            if(!eregi("^[_a-z0-9-]+('.[_a-z0-9-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*('.[a-z]{2,3})$", $email))  
                    {   
                    # Return Error - Invalid Email 
                    $msg = 'The email you have entered is invalid, please try again.';  
                    } 
                else 
                                {  
                # Sending the Email Message 
                # I moved this into the else clause. By putting it outside it was getting 
                # sent even if the email was invalid, which isn't exactly a good idea :)
                $to = 'adminemail@something.com'; // Send email to receipient
                $subject = 'Employee Registration'; // Give the email a subject
 $message = 'From: ' . $firstname . "'r'r'n" . $lastname . "'r'r'r'n" . 'IP Address: ' . $ip; // Our message above including who sent it
$message_body .= "Please click on link to activate Employee 'n'n email=$email   $email_code ";

                # Here I am capturing the return value of the mail() function so that I
                # can check if it was actually successful. Just because the function is
                # executed does not mean the email was sent. The return value tells us
                # whether it was or not.
                $success = mail($to,$subject,$message_body); // Send our email
                if($success) 
                    {
                        # The email was sent successfully!
                        $msg = 'Thank you for your message.';
                    }
                else 
                    {
                        # Email failed to send.
                        $msg = 'An error occured. The mail could not be sent.';
                    }
            }
        }
    else if (!isset($_POST['submit']))
        {
            # If the form hasn't been submitted yet, then do nothing. Do not prompt to enter a name just yet.
        }
    # One of the fields was empty. This finds out which one and creates a message
    # to indicate which it was.
    else
        {
            $msg = "*";
            if(empty($_POST['txtfstname'])) 
            {
                $msg .= "Please enter your first name";
            }
            elseif(empty($_POST['txtlstname']))
            {
                $msg .= "Please enter your last name";
            }
            elseif(empty($_POST['txtemail'])) 
            {
                $msg .= "Please enter your email";
            } 
            else 
            {
                $msg .= "Please enter your employee number";
            }
        }

?>
<form id="contact" class="form" action="" method="post" name="contact"><strong>Employee  Registration</strong>
<em>Please enter the following information and submit. Once the administrator approves your registration, you will receive a confirmation email with login details.</em>
<p><?php echo $msg ?></p>
<table>
<tbody>
<tr>
<td>First Name:</td>
<td><input id="name" class="required" name="txtfstname" type="text" value="<?php echo    $_POST['txtfstname']; ?>" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input id="name" class="required" name="txtlstname" type="text" value="<?php echo  $_POST['txtlstname']; ?>" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input id="name" class="required" name="txtemail" type="text" value="<?php echo $_POST['txtemail']; ?>" /></td>
</tr>
<tr>
<td>Employee No:</td>
<td><input id="name" class="required" name="txtempno" type="text" value="<?php echo    $_POST['txtempno']; ?>" /></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="Send" /></td>
</tr>
</tbody>
</table>
</form>  

这是我的数据库列。

ID,first_name,last_name,电子邮件emp_ no,暗语地位电子邮件代码

现在我想继承一些我想不通的东西1.如何在管理员单击电子邮件中的激活链接时将状态表单0更新为12.如何向员工发送电子邮件,说明他已被激活,现在可以在管理员激活员工时登录他的帐户。3.当员工向管理员发送的注册邮件进入SPAM文件夹时。但我想把它放在收件箱里。该怎么办。任何帮助都值得事先感谢。

好的,所以,基本上点击链接,重定向如下:

$user变量必须具有激活其帐户的用户的用户名。

<a href="activate.php?user=<?php echo $user; ?>">Activate</a>

因此,在activate.php中:

<?php
if(isset($_GET['user']) && strlen($_GET['user']) > 0) {
// Update the users activated status from 0 to 1 by running a query.
// Mail to the employee of his account activation
} else {
// No user selected.
}

要解决垃圾邮件文件夹中收到邮件的问题,请将电子邮件保存到您的联系人/取消阻止该电子邮件,它将在收件箱中收到。

我对PHP没有真正的经验,但在使用mail()函数之前我已经发送过电子邮件。要了解更多信息,您可以查看:

W3学校

Php文档

我希望这能帮助你:)