User Registration with Email Verification in PHP

Send confirmation email link after registration in PHP and MySQL. Here, we will show you how to send user confirmation email after registration with activation link in PHP and MySQL.

As well as we provide the email verification php source code link at the end of this tutorial.

Suppose, any user registered with your PHP and MySQL web/app. And you need to verify user email address by send activation/verification link in email on user registration.

So, this user registration with email verification in PHP tutorial will guide you step by step on how to send email verification/activation link after user registration in PHP and MySQL.

How to Send User Confirmation Email After Registration with Activation Link in PHP

Use the below given easy steps to verify/activate user account by email link in PHP and MySQL after registration:

  • Step 1: Create Table In DB by Sql Query
  • Step 2: Create a Database Connection PHP File
  • Step 3: Create User Registration Page PHP
  • Step 4: Store User Registration Data and Send Email
  • Step 5: Create Email Verification PHP File

Step 1: Create Table In DB by Sql Query

First of all, open your phpmyadmin and run the following sql query. To create table into your selected database:

CREATE TABLE `users1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `email` varchar(100) NOT NULL, `password` varchar(250) NOT NULL, `status` int(11) NOT NULL DEFAULT '0', `email_verification_link` varchar(255) NOT NULL, `email_verified_at` TIMESTAMP NULL, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step 2: Create a Database Connection PHP File

In this step, create a file name db.php and add the following code into db.php file:

<?php $servername='localhost'; $username='root'; $password=''; $dbname = "my_db"; $conn=mysqli_connect($servername,$username,$password,"$dbname"); if(!$conn){ die('Could not Connect MySql Server:' .mysql_error()); }
?>

Note that, This code is used to create a MySQL database connection in PHP project.

Step 3: Create User Registration Page PHP

In this step, create a user registration php file that named index.php and add the below PHP and HTML code into index.php file.

Note that, This HTML code shows the user registration form.

Now, add the following html form into index.php file:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>User Registration with Email Verification in PHP</title>
<!-- CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<div class="card">
<div class="card-header text-center">
User Registration with Email Verification in PHP
</div>
<div class="card-body">
<form action="store-registration-send-email.php" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" name="name" class="form-control" id="name" required="">
</div> <div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" required="">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Password</label>
<input type="password" name="password" class="form-control" id="password" required="">
</div> <div class="form-group">
<label for="exampleInputEmail1">Confirm Password</label>
<input type="password" name="cpassword" class="form-control" id="cpassword" required="">
</div> <input type="submit" name="password-reset-token" class="btn btn-primary">
</form>
</div>
</div>
</div>
</body>
</html>

Step 3: Store User Registration Data and Send Email

In this step, create new PHP file named store-registration-send-email.php. This file PHP code will store user registration data into db table. And As well as send an email verification link to user and store it into MySQL DB.

To add the following php code into store-registration-send-email.php file:

<?php
if(isset($_POST['password-reset-token']) &amp;&amp; $_POST['email'])
{
include "db.php";
$result = mysqli_query($conn,"SELECT * FROM users WHERE email='" . $_POST['email'] . "'");
$row= mysqli_num_rows($result);
if($row < 0)
{
$token = md5($_POST['email']).rand(10,9999);
mysqli_query($conn, "INSERT INTO users(name, email, email_verification_link ,password) VALUES('" . $_POST['name'] . "', '" . $_POST['email'] . "', '" . $token . "', '" . md5($_POST['password']) . "')");
$link = "<a href='localhost/email-verification/verify-email.php?key=".$_POST['email']."&amp;token=".$token."'>Click and Verify Email</a>";
require_once('phpmail/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
$mail->IsSMTP();
// enable SMTP authentication
$mail->SMTPAuth = true; // GMAIL username
$mail->Username = "[email protected]";
// GMAIL password
$mail->Password = "your_gmail_password";
$mail->SMTPSecure = "ssl"; // sets GMAIL as the SMTP server
$mail->Host = "smtp.gmail.com";
// set the SMTP port for the GMAIL server
$mail->Port = "465";
$mail->From='[email protected]';
$mail->FromName='your_name';
$mail->AddAddress('reciever_email_id', 'reciever_name');
$mail->Subject = 'Reset Password';
$mail->IsHTML(true);
$mail->Body = 'Click On This Link to Verify Email '.$link.'';
if($mail->Send())
{
echo "Check Your Email box and Click on the email verification link.";
}
else
{
echo "Mail Error - >".$mail->ErrorInfo;
}
}
else
{
echo "You have already registered with us. Check Your email box and verify email.";
}
}
?>

Step 5: Create Email Verification PHP File

In this step, create email verification php file that named verify-email.php. This php file code verify user registration email from mysql database.

So, add the following php and html code into verify-email.php file:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>User Account Activation by Email Verification using PHP</title>
<!-- CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<?php
if($_GET['key'] &amp;&amp; $_GET['token'])
{
include "db.php";
$email = $_GET['key'];
$token = $_GET['token'];
$query = mysqli_query($conn, "SELECT * FROM `users` WHERE `email_verification_link`='".$token."' and `email`='".$email."';"
);
$d = date('Y-m-d H:i:s');
if (mysqli_num_rows($query) > 0) {
$row= mysqli_fetch_array($query);
if($row['email_verified_at'] == NULL){
mysqli_query($conn,"UPDATE users set email_verified_at ='" . $d . "' WHERE email='" . $email . "'");
$msg = "Congratulations! Your email has been verified.";
}else{
$msg = "You have already verified your account with us";
}
} else {
$msg = "This email has been not registered with us";
}
}
else
{
$msg = "Danger! Your something goes to wrong.";
}
?>
<div class="container mt-3">
<div class="card">
<div class="card-header text-center">
User Account Activation by Email Verification using PHP
</div>
<div class="card-body">
<p><?php echo $msg; ?></p>
</div>
</div>
</div>
</body>
</html>

Conclusion

Send email activation link in php mysql tutorial, you have learned how to send activation/verfication link after user registration in php and MySQL.

Related Posts