• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Simple Email Verification Using PHPMailer

joe

One-Liner Genius
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
61
Likes
16
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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, [url=https://getcomposer.org/download/]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
2018-09-12_14_39_05-localhost_127.0.0.1_db_verify_phpmyadmin_4.7.0.png

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. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

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. <?php
  51. }

  52. unset

    (

    $_SESSION

    [

    'result'

    ]

    )

    ;
  53. unset

    (

    $_SESSION

    [

    'status'

    ]

    )

    ;
  54. }
  55. ?>
  56. </form>
  57. </div>
  58. </div>
  59. </body>
  60. </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. <h5>Please let me know if you have any questions or would like further information, otherwise, no response is needed.</h5>

  25. <h5>You may now login to your account <a href="confirm_account.php?email=<?php

    echo

    $email

    ?>

    ">here</a></h5>

  26. <h5>Thank You!,</h5>

  27. <?php

  28. }
  29. ?>
  30. </div>
  31. </div>
  32. </body>
  33. </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. session_start

    (

    )

    ;
  3. require_once

    'conn.php'

    ;

  4. if

    (

    ISSET

    (

    $_POST

    [

    'login'

    ]

    )

    )

    {
  5. $username

    =

    $_POST

    [

    'username'

    ]

    ;
  6. $password

    =

    $_POST

    [

    'password'

    ]

    ;

  7. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `user` WHERE `username` = '$username

    ' && `password` = '$password

    ' && `status` = 'Verified'"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  8. $count

    =

    mysqli_num_rows

    (

    $query

    )

    ;

  9. if

    (

    $count

    >

    0

    )

    {
  10. header

    (

    "location: home.php"

    )

    ;
  11. }

    else

    {
  12. $_SESSION

    [

    'message'

    ]

    =

    "Invalid username or password"

    ;
  13. header

    (

    "location: index.php"

    )

    ;
  14. }
  15. }
  16. ?>

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. if

    (

    ISSET

    (

    $_REQUEST

    [

    'email'

    ]

    )

    )

    {
  4. $email

    =

    $_REQUEST

    [

    'email'

    ]

    ;

  5. mysqli_query

    (

    $conn

    ,

    "UPDATE `user` SET `status` = 'Verified' WHERE `email` = '$email

    '"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;

  6. header

    (

    'location:index.php'

    )

    ;
  7. }
  8. ?>

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. require_once

    'conn.php'

    ;
  5. session_start

    (

    )

    ;

  6. if

    (

    ISSET

    (

    $_POST

    [

    'register'

    ]

    )

    )

    {
  7. $username

    =

    $_POST

    [

    'username'

    ]

    ;
  8. $password

    =

    $_POST

    [

    'password'

    ]

    ;
  9. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  10. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  11. $email

    =

    $_POST

    [

    'email'

    ]

    ;

  12. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `user` VALUES('', '$username

    ', '$password

    ', '$firstname

    ', '$lastname

    ', '$email

    ', '')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;

  13. $link

    =

    "localhost/PHP - Simple Email Verification Using PHPMailer/verified.php?email="

    .

    $email

    .

    ""

    ;
  14. $message

    =

    "Hello $firstname

    $lastname

    ! <br>"
  15. .

    "Please click the link below to confirm your email and complete the registration process.<br>"
  16. .

    "You will be automatically redirected to a welcome page where you can then sign in.<br><br>"
  17. .

    "Please click below to activate your account:<br>"
  18. .

    "<a href='$link

    '>Click Here!</a>"

    ;

  19. //Load composer's autoloader
  20. require

    'vendor/autoload.php'

    ;

  21. $mail

    =

    new

    PHPMailer(

    true

    )

    ;

  22. try {
  23. //Server settings
  24. $mail

    ->

    isSMTP

    (

    )

    ;
  25. $mail

    ->

    Host

    =

    'smtp.gmail.com'

    ;
  26. $mail

    ->

    SMTPAuth

    =

    true

    ;
  27. $mail

    ->

    Username

    =

    '[email protected]

    '

    ;
  28. $mail

    ->

    Password

    =

    'mypassword'

    ;
  29. $mail

    ->

    SMTPOptions

    =

    array

    (
  30. 'ssl'

    =>

    array

    (
  31. 'verify_peer'

    =>

    false

    ,
  32. 'verify_peer_name'

    =>

    false

    ,
  33. 'allow_self_signed'

    =>

    true
  34. )
  35. )

    ;
  36. $mail

    ->

    SMTPSecure

    =

    'ssl'

    ;
  37. $mail

    ->

    Port

    =

    465

    ;

  38. //Send Email
  39. $mail

    ->

    setFrom

    (

    '[email protected]

    '

    )

    ;

  40. //Recipients
  41. $mail

    ->

    addAddress

    (

    $email

    )

    ;
  42. $mail

    ->

    addReplyTo

    (

    '[email protected]

    '

    )

    ;

  43. //Content
  44. $mail

    ->

    isHTML

    (

    true

    )

    ;
  45. $mail

    ->

    Subject

    =

    "Account registration confirmation"

    ;
  46. $mail

    ->

    Body

    =

    $message

    ;

  47. $mail

    ->

    send

    (

    )

    ;

  48. header

    (

    "location:verification.php?firstname="

    .

    $firstname

    .

    "&lastname="

    .

    $lastname

    .

    "&email="

    .

    $email

    .

    ""

    )

    ;

  49. }

    catch (

    Exception $e

    )

    {
  50. $_SESSION

    [

    'result'

    ]

    =

    'Message could not be sent. Mailer Error: '

    .

    $mail

    ->

    ErrorInfo

    ;
  51. $_SESSION

    [

    'status'

    ]

    =

    'error'

    ;
  52. }

  53. }

  54. ?>

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.
2018-09-01_14_34_13-less_secure_app_access_0.png


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![/url]


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top