Current Location: Home> Latest Articles> Complete Guide to Sending Email Verification During User Registration in PHP

Complete Guide to Sending Email Verification During User Registration in PHP

M66 2025-08-04

Steps to Send Verification Email During User Registration in PHP

User registration is an essential feature for modern websites. To ensure that the provided email address is valid and authentic, it is common to send a verification email to confirm the user's identity. This article explains how to implement this functionality in PHP with practical code examples to help developers get started quickly.

Configure the SMTP Server

Email sending relies on an SMTP server, and it is recommended to use the PHPMailer library for this purpose. First, install PHPMailer using Composer:

composer require phpmailer/phpmailer

After installation, include the autoload file in your project:

require 'vendor/autoload.php';

Then configure the SMTP server settings such as host, port, username, and password according to your email service provider.

Handle the User Registration Request

When the user submits the registration form, capture the input information such as username and email:

$username = $_POST['username'];
$email = $_POST['email'];

Generate a unique verification token for email validation:

$token = bin2hex(random_bytes(16));

Store the user data and the verification token in the database for later verification and updates.

Create Verification Link and Send Email

Build a verification link containing the token. When the user clicks it, the email can be validated:

$verificationLink = "http://yourdomain.com/verify.php?token=" . $token;

Use PHPMailer to send the email, including the verification link in the message body:

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'username@example.com';
$mail->Password = 'password';
$mail->setFrom('from@example.com', 'Your Site');
$mail->addAddress($email, $username);
$mail->Subject = 'Please verify your email address';
$mail->Body = 'Please click the following link to verify your email: ' . $verificationLink;
if (!$mail->send()) {
    echo 'Failed to send email.';
    echo 'Error: ' . $mail->ErrorInfo;
} else {
    echo 'Verification email has been sent. Please check your inbox.';
}

Verify User Email

When the user clicks the verification link, the verification page (verify.php) retrieves the token from the URL to complete the validation process:

$token = $_GET['token'];

Query the database by token, verify the corresponding user, update the email verification status as verified, and display the verification result to the user.

Conclusion

Following these steps, developers can implement a complete email verification process during user registration in PHP. This improves registration security, prevents invalid email registrations, and enhances site management efficiency and user experience.