Skip to main content

PHP - Simple Email Verification Using PHPMailer

Screenshot
Body
In this tutorial we will create a Simple Email Verification Using PHPMailer. PHP is a server-side scripting language designed primarily for web development. PHP is a server-side scripting language designed primarily for web development. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Installing the PHPMailer

Using Composer If you dont' have a composer installed on your machine you can get it here,
https://getcomposer.org/download/ First, Create a file that will receive the phpmailer file, write these code inside the text editor and save it as composer.json.
  1. <?php
  2. {
  3. "phpmailer/phpmailer": "~6.0"
  4. }
  5. ?>
Then, open your command prompt and cd to your working directory. After that type this command and hit enter afterwards.
  1. composer require phpmailer/phpmailer
After installation you will see different directory inside on your working directory, namely vendor, etc. Without Composer To manually install the phpmailer to your app, first download the file here https://github.com/PHPMailer/PHPMailer After you finish downloading the file, move the file inside the directory that you are working on. Now you're ready to use the phpmailer.

Creating Database

Open your database web server then create a database name in it db_verify, after that click Import then locate the database file inside the folder of the application then click ok tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = mysqli_connect('localhost', 'root', '', 'db_verify');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as shown below. index.php
  1. <?php session_start()?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Email Verification Using PHPMailer</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <a href="register_form.php">Not a member? Sign up here...</a>
  21. <br /><br />
  22. <form method="POST" action="login.php">
  23. <div class="form-group">
  24. <label>Username</label>
  25. <input type="text" name="username" class="form-control" required="required"/>
  26. </div>
  27. <div class="form-group">
  28. <label>Password</label>
  29. <input type="password" name="password" maxlength="12" class="form-control" required="required"/>
  30. </div>
  31. <div class="form-group">
  32. <button name="login" class="btn btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  33. </div>
  34. <?php
  35. if(ISSET($_SESSION['message'])){
  36. ?>
  37. <div class="alert alert-danger"><?php echo $_SESSION['message']?></div>
  38. <?php
  39. unset($_SESSION['message']);
  40. }
  41. ?>
  42. </form>
  43. </div>
  44. </div>
  45. </body>
  46. </html>
register_form.php
  1. <?php session_start()?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Email Verification Using PHPMailer</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-6">
  19. <a href="index.php">Already a member? Sign in here...</a>
  20. <br /><br />
  21. <form method="POST" action="register.php">
  22. <div class="form-group">
  23. <label>Username</label>
  24. <input type="text" name="username" class="form-control" required="required"/>
  25. </div>
  26. <div class="form-group">
  27. <label>Password</label>
  28. <input type="password" name="password" minlength="12" class="form-control" required="required"/>
  29. </div>
  30. <div class="form-group">
  31. <label>Firstname</label>
  32. <input type="text" name="firstname" class="form-control" required="required"/>
  33. </div>
  34. <div class="form-group">
  35. <label>Lastname</label>
  36. <input type="text" name="lastname" class="form-control" required="required"/>
  37. </div>
  38. <div class="form-group">
  39. <label>Email</label>
  40. <input type="email" name="email" class="form-control" required="required"/>
  41. </div>
  42. <div class="form-group">
  43. <button name="register" class="btn btn-primary btn-block">Sign up</button>
  44. </div>
  45. <?php
  46. if(ISSET($_SESSION['status'])){
  47. if($_SESSION['status'] == "error"){
  48. ?>
  49. <div class="alert alert-danger"><?php echo $_SESSION['result']?></div>
  50.  
  51. <?php
  52. }
  53.  
  54. unset($_SESSION['result']);
  55. unset($_SESSION['status']);
  56. }
  57. ?>
  58. </form>
  59. </div>
  60. </div>
  61. </body>
  62. </html>
home.php
  1. <?php session_start()?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Email Verification Using PHPMailer</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <h2>Welcome User</h2>
  21. </div>
  22. </div>
  23. </body>
  24. </html>
verification.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Email Verification Using PHPMailer</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <?php
  20. if(ISSET($_REQUEST['firstname']) && ISSET($_REQUEST['lastname']) && ISSET($_REQUEST['email'])){
  21. ?>
  22. <h3><strong>You're ready to go!</strong></h3>
  23. <br />
  24. <h5>Hi, <?php echo $_REQUEST['firstname']." ".$_REQUEST['lastname']?><h5>
  25. <h5>We've finished setting up your account.<h5>
  26. <h5>We sent you a confirmation to your email account<h5>
  27. <a class="btn btn-primary" href="https://<?php echo $_REQUEST['email']?>" target="_blank">Confirm Email</a>
  28. <?php
  29. }
  30. ?>
  31. </div>
  32. </div>
  33. </body>
  34. </html>
verified.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Email Verification Using PHPMailer</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <?php
  20. if(ISSET($_REQUEST['email'])){
  21. $email = $_REQUEST['email'];
  22. ?>
  23. <center><h4>The email that you provided is already verified.</h4></center>
  24.  
  25. <h5>Please let me know if you have any questions or would like further information, otherwise, no response is needed.</h5>
  26.  
  27. <h5>You may now login to your account <a href="confirm_account.php?email=<?php echo $email?>">here</a></h5>
  28.  
  29. <h5>Thank You!,</h5>
  30.  
  31. <?php
  32.  
  33. }
  34. ?>
  35. </div>
  36. </div>
  37. </body>
  38. </html>

Creating the Login Function

This code contains the login function of the Application. This code will check if the user account is already verified to have permission to login. To this just copy and write these code below inside the text editor, then save it as login.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['login'])){
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7.  
  8. $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password' && `status` = 'Verified'") or die(mysqli_error());
  9. $count = mysqli_num_rows($query);
  10.  
  11. if($count > 0){
  12. header("location: home.php");
  13. }else{
  14. $_SESSION['message'] = "Invalid username or password";
  15. header("location: index.php");
  16. }
  17. }
  18. ?>

Creating Confirm Account

This code contains the confirming of account in the application. This code will confirm the user account to enable the login access. To this just copy and write these block of code inside the text editor, then save it as confirm_account.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_REQUEST['email'])){
  5. $email = $_REQUEST['email'];
  6.  
  7. mysqli_query($conn, "UPDATE `user` SET `status` = 'Verified' WHERE `email` = '$email'") or die(mysqli_error());
  8.  
  9. header('location:index.php');
  10. }
  11. ?>

Creating the Main Function

This code contains the main function of the application. This code will sent a confirmation to the user email, to verify if the email account exist. To make this just follow and write these code below inside the text editor, then save it as register.php Note: To make this script work without composer, make sure you change this code:
  1. require 'vendor/autoload.php';
Into this:
  1. require 'path/to/PHPMailer/src/Exception.php';
  2. require 'path/to/PHPMailer/src/PHPMailer.php';
  3. require 'path/to/PHPMailer/src/SMTP.php';
So that it will recognize the phpmailer class when you call it in the code.
  1. <?php
  2. use PHPMailer\PHPMailer\PHPMailer;
  3. use PHPMailer\PHPMailer\Exception;
  4.  
  5. require_once 'conn.php';
  6.  
  7. if(ISSET($_POST['register'])){
  8. $username = $_POST['username'];
  9. $password = $_POST['password'];
  10. $firstname = $_POST['firstname'];
  11. $lastname = $_POST['lastname'];
  12. $email = $_POST['email'];
  13.  
  14. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$username', '$password', '$firstname', '$lastname', '$email', '')") or die(mysqli_error());
  15.  
  16. $link = "localhost/PHP - Simple Email Verification Using PHPMailer/verified.php?email=".$email."";
  17. $message = "Hello $firstname $lastname! <br>"
  18. . "Please click the link below to confirm your email and complete the registration process.<br>"
  19. . "You will be automatically redirected to a welcome page where you can then sign in.<br><br>"
  20. . "Please click below to activate your account:<br>"
  21. . "<a href='$link'>Click Here!</a>";
  22.  
  23. //Load composer's autoloader
  24. require 'vendor/autoload.php';
  25.  
  26. $mail = new PHPMailer(true);
  27.  
  28. try {
  29. //Server settings
  30. $mail->isSMTP();
  31. $mail->Host = 'smtp.gmail.com';
  32. $mail->SMTPAuth = true;
  33. $mail->Username = '[email protected]';
  34. $mail->Password = 'mypassword';
  35. $mail->SMTPOptions = array(
  36. 'ssl' => array(
  37. 'verify_peer' => false,
  38. 'verify_peer_name' => false,
  39. 'allow_self_signed' => true
  40. )
  41. );
  42. $mail->SMTPSecure = 'ssl';
  43. $mail->Port = 465;
  44.  
  45. //Send Email
  46. $mail->setFrom('[email protected]');
  47.  
  48. //Recipients
  49. $mail->addAddress($email);
  50. $mail->addReplyTo('[email protected]');
  51.  
  52. //Content
  53. $mail->isHTML(true);
  54. $mail->Subject = "Account registration confirmation";
  55. $mail->Body = $message;
  56.  
  57. $mail->send();
  58.  
  59. header("location:verification.php?firstname=".$firstname."&lastname=".$lastname."&email=".$email."");
  60.  
  61. } catch (Exception $e) {
  62. $_SESSION['result'] = 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
  63. $_SESSION['status'] = 'error';
  64. }
  65.  
  66. }
  67.  
  68. ?>
Note: To be able to send a mail to gmail, make sure you provide your own gmail username and password or much better is to make a dummy account to use. Inside your google account make sure you allow the less secure apps so that you can have permission to send the email. tut2 There you have it we successfully created a Simple Email Verification Using PHPMailer. I hope that these simple tutorial help you to your projects. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment