Email Verification in PHP

0
290

registration form:

<h2 class="card-title mb-3">Create an account</h2>
<form method="post" action="code.php" id="formRegister">
<div class="row">
<div class="col">
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg></span>
</div>
<input class="form-control" name="first_name" type="text" placeholder="First name" data-pristine-required>
</div>
</div>
</div>
<div class="col">
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg></span>
</div>
<input class="form-control" name="last_name" type="text" placeholder="Last name" data-pristine-required>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"></path><polyline points="22,6 12,13 2,6"></polyline></svg></span>
</div>
<input name="email" type="email" placeholder="Email" data-pristine-required class="form-control">
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg></span>
</div>
<input class="form-control" name="password" type="password" placeholder="Password" data-pristine-required>
</div>
</div>
<div class="form-group">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg></span>
</div>
<input class="form-control" name="password_confirmation" type="password" placeholder="Confirm password" data-pristine-required>
</div>
</div>


<input type="hidden" name="usertype" value="user">


<div style="margin: 30px 0 10px;">
<div class="g-recaptcha" data-sitekey="6LdgjHEUAAAAAJN3Hh-PJ-jLZwx6Gcf-0NRXiV24"></div>
</div>
<button type="submit"  name="registerbtn" id="btnRegisterSubmit" class="submit button" style="margin-top: 15px;">Submit</button>
<div style="margin-top: 40px;">
<a href="login.php">Already have an account? Sign in instead</a>
</div>
</form>

Code.php

<?php
include('security.php');
//$connection = mysqli_connect("localhost","root","","billing");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

function sendemail_verify($first_name,$verify_token,$email)
{

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->SMTPDebug  = 1;  
$mail->SMTPAuth   = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port       = 587;
$mail->Host       = "smtp.gmail.com";
$mail->Username   = "xxxx@gmail.com";
$mail->Password   = "****";

$mail->IsHTML(true);
$mail->AddAddress($email);
$mail->SetFrom("xxxx@gmail.com",$first_name);
//$mail->AddReplyTo("reply-to-email@domain", "reply-to-name");
//$mail->AddCC("cc-recipient-email@domain", "cc-recipient-name");
$mail->Subject = "Email Verification from Glorious web tech";
$content = " <h2> You have registered with Glorious web tech </h2>
<h5> verify your email address to login with the below link </h5>
<br></br>
<a href='http://localhost/gloriouswebtech/billing/verify-email.php?token=$verify_token'> Click Me </a>";
$mail->MsgHTML($content); 
if(!$mail->Send()) {
  echo "Error while sending Email.";
  var_dump($mail);
} else {
  echo "Email sent successfully";
}
 
}


if(isset($_POST['registerbtn']))
{
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $password = md5($_POST['password']);
    $cpassword = md5($_POST['password_confirmation']);
    $usertype = $_POST['usertype'];
    $verify_token=md5(rand());

     $email_query = "SELECT * FROM register WHERE email='$email' ";
     $email_query_run = mysqli_query($connection, $email_query);
     if(mysqli_num_rows($email_query_run) > 0)
     {
         $_SESSION['status'] = "Email Already Taken. Please Try Another one.";
         $_SESSION['status_code'] = "error";
         header('Location: register.php');  
     }
     else
     {
         if($password === $cpassword)
         {
             $query = "INSERT INTO register (firstname,lastname,verify_token,email,password,confirmpassword,usertype) VALUES ('$first_name','$last_name','$verify_token','$email','$password','$cpassword','$usertype')";
             $query_run = mysqli_query($connection, $query);
             
             if($query_run)
             {
                 sendemail_verify("$first_name","$verify_token","$email");
                 //echo "Saved";
                 $_SESSION['success'] = "Registration successfull..!! Please Verify Email address..!!";
                 $_SESSION['status_code'] = "success";
                 header('Location: login.php');
             }
             else 
             {
                 $_SESSION['status'] = "Admin Profile Not Added";
                 $_SESSION['status_code'] = "error";
                 header('Location: register.php');  
             }
         }
         else 
         {
             $_SESSION['status2'] = "Password and Confirm Password Does Not Match";
             $_SESSION['status_code'] = "warning";
             header('Location: register.php');  
         }
     }
 
 }

Terminal:
$ composer require phpmailer/phpmailer

LEAVE A REPLY

Please enter your comment!
Please enter your name here