Current Location: Home> Latest Articles> PHP Email Verification Code Sending and Validation Tutorial: Complete Implementation and Code Example

PHP Email Verification Code Sending and Validation Tutorial: Complete Implementation and Code Example

M66 2025-06-16

PHP Email Verification Code Sending and Validation

With the rapid development of the internet, email verification codes have become a vital method for verifying user identities. When developing websites or applications, email verification codes are commonly used for user registration, password recovery, and other features. This article will explain how to implement email verification code sending and validation using PHP, and provide detailed code examples.

1. Sending Email Verification Code

First, we need to send the verification code to the user's registered email using PHP. Below is a simple example using the PHPMailer library to send an email:

<?php
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
require 'phpmailer/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Configure the email sending settings
$mail = new PHPMailer(true); // Create a PHPMailer instance
$mail->isSMTP(); // Use SMTP for sending emails
$mail->Host = 'smtp.example.com'; // SMTP server address
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your_email@example.com'; // Username
$mail->Password = 'your_email_password'; // Password
$mail->SMTPSecure = 'tls'; // Use TLS encryption
$mail->Port = 587; // SMTP port number
$mail->CharSet = 'UTF-8'; // Email character set

// Set the email content
$mail->setFrom('your_email@example.com', 'Your Name'); // Sender's email and name
$mail->addAddress('recipient@example.com', 'Recipient Name'); // Recipient's email and name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Email Verification Code'; // Email subject
$mail->Body    = 'Your verification code is: 123456'; // Email body

// Send the email
if ($mail->send()) {
    echo 'Verification code email sent successfully!';
} else {
    echo 'Email sending failed: ' . $mail->ErrorInfo;
}
?>

In the code above, we first include the PHPMailer library and configure the SMTP settings necessary for sending the email. Then, we use the `setFrom` and `addAddress` methods to set the sender and recipient email addresses. Finally, we call the `send` method to send the email.

Note that in a real application, you will need to replace the example SMTP server address, sender's email, and password with actual values, and adjust the recipient's information as needed. Additionally, you can include more detailed information in the email body, such as the expiration time of the verification code.

2. Validating the Email Verification Code

After the user enters the email verification code, we need to validate it. Below is a simple code example for validating the user-entered verification code:

<?php
session_start(); // Start a session

// Get the user-input verification code
$userInputCode = $_POST['code']; // Assume the user submitted the code via a form

// Get the previously sent verification code
$generatedCode = $_SESSION['email_code']; // Retrieve the verification code from the session

// Validate the code
if ($userInputCode === $generatedCode) {
    echo 'Verification code validated successfully!';
} else {
    echo 'Verification code validation failed!';
}
?>

In the above code, we first start a session. Then, we use the `$_POST` superglobal variable to get the verification code submitted by the user through the form. We also use the `$_SESSION` superglobal variable to retrieve the previously sent verification code from the session. Finally, we compare the user input code with the generated code to determine if the validation is successful.

Note that the code for generating and sending the verification code is not included in this example. In a real-world scenario, you would store the verification code in a database and send it to the user via email. When validating the code, you would compare the user input with the code stored in the database.

Summary

This article explained how to implement email verification code sending and validation using PHP. By using the PHPMailer library, developers can easily send emails and include the verification code, and store the code in the session to validate the user input. We hope this guide helps developers implement email verification functionality in their websites or applications, enhancing user authentication security.