PHP development practice: Use PHP and MySQL to implement the user registration email sending function
Summary:
In Web applications, user registration is a common function. In order to ensure the security and timeliness of user information, it is often necessary to send a registration confirmation link to users via email. This article will introduce how to use PHP and MySQL to implement the function of sending user registration emails, and come with corresponding code examples.
The sample code is as follows:
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL );
The sample code is as follows:
<form action="register.php" method="post"> <input type="text" name="username" placeholder="用户名" required> <input type="email" name="email" placeholder="邮箱" required> <input type="password" name="password" placeholder="密码" required> <input type="submit" value="注册"> </form>
In the register.php file, we need to get the data submitted by the form and insert it into the database.
The sample code is as follows:
<?php $conn = mysqli_connect("localhost", "username", "password", "database_name"); $username = $_POST['username']; $email = $_POST['email']; $password = password_hash($_POST['password'], PASSWORD_DEFAULT); $sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')"; mysqli_query($conn, $sql); mysqli_close($conn); ?>
First, we need to write a function to send the email. We can use the PHPMailer library to simplify the process of sending emails.
The sample code is as follows:
<?php require 'vendor/autoload.php'; function sendConfirmationEmail($email, $activationCode) { $mail = new PHPMailerPHPMailerPHPMailer(); $mail->isSMTP(); $mail->SMTPAuth = true; $mail->SMTPSecure = 'ssl'; $mail->Host = 'smtp.example.com'; $mail->Port = 465; $mail->Username = 'your_email@example.com'; $mail->Password = 'your_email_password'; $mail->setFrom('your_email@example.com', 'Your Name'); $mail->addAddress($email); $mail->Subject = '账户激活'; $mail->Body = "请点击以下链接激活您的账户: "; $mail->Body .= "http://www.example.com/activate.php?code=$activationCode"; if (!$mail->send()) { echo '邮件发送失败:' . $mail->ErrorInfo; } else { echo '邮件发送成功!请查收您的邮箱!'; } } ?>
After successful registration, call the sendConfirmationEmail function to send the email.
The sample code is as follows:
$activationCode = md5($email); sendConfirmationEmail($email, $activationCode);
The sample code is as follows:
<?php $conn = mysqli_connect("localhost", "username", "password", "database_name"); $activationCode = $_GET['code']; $sql = "UPDATE users SET activated = 1 WHERE activation_code = '$activationCode'"; mysqli_query($conn, $sql); mysqli_close($conn); ?>
Conclusion:
This article introduces how to use PHP and MySQL to implement the user registration email sending function. By creating the database, designing the registration form and processing, sending the registration confirmation email, and handling the activation link, we can implement a complete user registration function. This will help improve the user experience and security of web applications.
The above is the detailed content of PHP development practice: Use PHP and MySQL to implement user registration email sending function. For more information, please follow other related articles on the PHP Chinese website!