PHP - Simple Email Verification Using PHPMailer

Submitted by: 
Language: 
Visitors have accessed this post 376 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">PHP - Simple Email Verification Using PHPMailer</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-3"></div>
  20. <div class="col-md-6">
  21. <a href="register_form.php">Not a member? Sign up here...</a>
  22. <br /><br />
  23. <form method="POST" action="login.php">
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" name="username" class="form-control" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" name="password" maxlength="12" class="form-control" required="required"/>
  31. </div>
  32. <div class="form-group">
  33. <button name="login" class="btn btn-primary btn-block"><span class="glyphicon glyphicon-log-in"></span> Login</button>
  34. </div>
  35. <?php
  36. if(ISSET($_SESSION['message'])){
  37. ?>
  38. <div class="alert alert-danger"><?php echo $_SESSION['message']?></div>
  39. <?php
  40. unset($_SESSION['message']);
  41. }
  42. ?>
  43. </form>
  44. </div>
  45. </div>
  46. </body>
  47. </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="<a href="https://sourcecodester.com">Sourcecodester</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">PHP - Simple Email Verification Using PHPMailer</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-6">
  20. <a href="index.php">Already a member? Sign in here...</a>
  21. <br /><br />
  22. <form method="POST" action="register.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" minlength="12" class="form-control" required="required"/>
  30. </div>
  31. <div class="form-group">
  32. <label>Firstname</label>
  33. <input type="text" name="firstname" class="form-control" required="required"/>
  34. </div>
  35. <div class="form-group">
  36. <label>Lastname</label>
  37. <input type="text" name="lastname" class="form-control" required="required"/>
  38. </div>
  39. <div class="form-group">
  40. <label>Email</label>
  41. <input type="email" name="email" class="form-control" required="required"/>
  42. </div>
  43. <div class="form-group">
  44. <button name="register" class="btn btn-primary btn-block">Sign up</button>
  45. </div>
  46. <?php
  47. if(ISSET($_SESSION['status'])){
  48. if($_SESSION['status'] == "error"){
  49. ?>
  50. <div class="alert alert-danger"><?php echo $_SESSION['result']?></div>
  51.  
  52. <?php
  53. }
  54.  
  55. unset($_SESSION['result']);
  56. unset($_SESSION['status']);
  57. }
  58. ?>
  59. </form>
  60. </div>
  61. </div>
  62. </body>
  63. </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="<a href="https://sourcecodester.com">Sourcecodester</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">PHP - Simple Email Verification Using PHPMailer</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-3"></div>
  20. <div class="col-md-6">
  21. <h2>Welcome User</h2>
  22. </div>
  23. </div>
  24. </body>
  25. </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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </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. <?php
  21. if(ISSET($_REQUEST['firstname']) && ISSET($_REQUEST['lastname']) && ISSET($_REQUEST['email'])){
  22. ?>
  23. <h3><strong>You're ready to go!</strong></h3>
  24. <br />
  25. <h5>Hi, <?php echo $_REQUEST['firstname']." ".$_REQUEST['lastname']?><h5>
  26. <h5>We've finished setting up your account.<h5>
  27. <h5>We sent you a confirmation to your email account<h5>
  28. <a class="btn btn-primary" href="https://<?php echo $_REQUEST['email']?>" target="_blank">Confirm Email</a>
  29. <?php
  30. }
  31. ?>
  32. </div>
  33. </div>
  34. </body>
  35. </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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </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-2"></div>
  19. <div class="col-md-8">
  20. <?php
  21. if(ISSET($_REQUEST['email'])){
  22. $email = $_REQUEST['email'];
  23. ?>
  24. <center><h4>The email that you provided is already verified.</h4></center>
  25.  
  26. <h5>Please let me know if you have any questions or would like further information, otherwise, no response is needed.</h5>
  27.  
  28. <h5>You may now login to your account <a href="confirm_account.php?email=<?php echo $email?>">here</a></h5>
  29.  
  30. <h5>Thank You!,</h5>
  31.  
  32. <?php
  33.  
  34. }
  35. ?>
  36. </div>
  37. </div>
  38. </body>
  39. </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 = '<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>';
  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('<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>');
  47.  
  48. //Recipients
  49. $mail->addAddress($email);
  50. $mail->addReplyTo('<a href="mailto:[email protected]" rel="nofollow">[email protected]</a>');
  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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.