thinkphp realizes mailbox activation when users register

When some websites register, they will send email to the user's mailbox. Click the link in the email to activate the account. The basic principle is to save the user's information into the database at the time of registration (i.e. Click to send email). In addition, there is an activated field (default is not activated) that clicks the link in the email. After verification, the field status It will be activated. The link information in the email includes: user id (as long as the corresponding data can be found in the database, not necessarily id), activation code. After clicking this link, the data will be sent to a method we have written, as long as it is verified in this method.

operation flow

1. User submits registration information

2. Write to the database, the account is not activated at this time

3. Encrypt the user name password or other identification characters to form the activation code

4. Send the URL composed of activation code and user id to the mailbox

5. The user logs in the email. Click URL to activate

6. Verify the activation code, activate the account correctly, and change the activation status

Database design

 

Send mail

You need to load phpemailer class before sending mail. See my last article Thinkphp5+PHPMailer to send email

public function index()
    {
        $toemail = 'xxx@126.com';    //This is the recipient's email
        $active_url = "http://test.zxf/index/sendmail/active.html?id=1&active_key=123";
        $body =  "Dear".$toemail.": <br/>Thank you for registering your new account with us.<br/>Please click the link to activate your account.<br/> 
                    <a href='".$active_url."' target= '_blank'>Click activation</a><br/> 
                    //If the above link cannot be clicked, please copy it to your browser address bar to access it. The link is valid for 24 hours."; 

        $mail=new Phpmailer();
        $mail->isSMTP();    // Use SMTP Service (service sending mail)
        $mail->CharSet = "utf8";    // The encoding format is utf8,If the code is not set, the Chinese code will be garbled
        $mail->Host = "smtp.qq.com";    // Sender SMTP server address
        $mail->SMTPAuth = true;    // Use authentication or not
        $mail->Username = "xx@qq.com";    // Applied for smtp Mailbox name of the service (own mailbox name)
        $mail->Password = "xxxx";    // Sender's email password, not login password,yes qq Third party authorized login code of,You have to open it by yourself
        $mail->SMTPSecure = "ssl";    // Use ssl Protocol mode,
        $mail->Port = 465;    // QQ Mailbox ssl Protocol mode port number is 465/587
        $mail->setFrom("xx@qq.com","Test sender");    // Set sender information, such as sender in message format description,
        $mail->addAddress($toemail,'Test recipient');    // Set recipient information, such as recipients in the message format description
        $mail->addReplyTo("xx@qq.com","Reply");    // Set replier information, which refers to the email address to which the recipient will send the reply email if he wants to reply after receiving the email
        $mail->Subject = "This is a test email";    // Mail title
        $mail->Body = $body;// Mail text
        
        $mail->CharSet = "UTF-8";   //character set
        $mail->Encoding = "base64"; //Encoding mode
        $mail->IsHTML(true);    //html content support

       if(!$mail->send()){    // Send mail
           echo "Message could not be sent.";
           echo "Mailer Error: ".$mail->ErrorInfo;    // Output error message
        }else{
            echo '';
            return 'Send successfully';
        }
    }

After sending successfully, the email content. Note that if the email content cannot parse the html code after sending, you need to add the content in html format $mail - > ishtml (true);

 

Verify registration activation account

public function active()
    {
        $data = request()->param();
        // adopt id Find the corresponding activation code
        $active_key = Db::table('active')->where(['id'=>$data['id']])->value('active_key');
        if ($active_key) {

            // Verify that the activation code is correct
            if ($active_key == $data['active_key']) {
                // Change activation status
                $res = Db::table('active')->where(['id'=>$data['id']])->update(['status'=>1]);
                if ($res) {
                    echo "Successful activation";
                }else{

                    echo "Activation failed";
                }
            }else{
                echo "Incorrect activation code";
            }
        }else{
            echo "user does not exist";
        }
    }

This is just a simple example. The security is not high. You can encrypt the activation code and add the expiration time of the activation code to improve the security

Keywords: PHP Database encoding SSL

Added by guyfromfl on Wed, 08 Apr 2020 07:46:04 +0300