Thursday 16 June 2022

Php Mailer

 step 1:-

download phpmailer library file form https://github.com/PHPMailer/PHPMailer


step 2:- signupform.html code :-


<html>

<head>

</head>

<body>

<form action="register.php" method="post">

<label> First name </label>

<input type="text" name="firstname">

<label> Last name </label>

<input type="text" name="lastname">

<label> Contact no </label>

<input type="text" name="contactno">

<label> Email </label>

<input type="text" name="email">

<labe> Password</label>

<input type="text" name="password">

<label> Confirm Password </label>

<input type="text" name="confirmpassword">


<input type="submit" name="send" value="signup">

</form>

</body>

</html>

</html>


step 3:- write register.php file code.


<?php


include ("connect.php");


use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\SMTP;

use PHPMailer\PHPMailer\Exception;

 

 $firstname=$_POST['firstname'];

 $lastname=$_POST['lastname'];

 $contactno=$_POST['contactno'];

 $email=$_POST['email'];

 $password=$_POST['password'];


 

 

 

 $v_code=bin2hex(random_bytes(16));

 

 function sendmail($email,$v_code)

 {

require "PHPMailer\PHPMailer.php"; 

require "PHPMailer\SMTP.php";

require "PHPMailer\Exception.php";

$mail = new PHPMailer(true);

try {

   

    $mail->isSMTP();                                            //Send using SMTP

    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through

    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication

    $mail->Username   = 'yourgmailid@gmail.com';                     //SMTP username

    $mail->Password   = 'yourgmailpassword';                               //SMTP password

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption

    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`


    //Recipients

    $mail->setFrom('yourgmailid@gmail.com', 'Musaib');

    $mail->addAddress($email);     //Add a recipient

    


    //Content

    $mail->isHTML(true);                                  //Set email format to HTML

    $mail->Subject = 'Email Verification from Musaib';

    $mail->Body    = "Thanks for Registration!

             Click the link below to verify the email address

<a href='http://localhost:8081/emailverify/verify.php?email=$$email&v_code=$v_code'>Verify </a>";

   


    $mail->send();

    return true; //mail sent

    } 

catch (Exception $e) {

    return false; //mail not sent

   }

 }

 

 $sql="insert into user (firstname,lastname,contactno,email,password,v_code,is_verified) values('$firstname','$lastname','$contactno','$email','$password','$v_code')";

 

 $result=mysqli_query($con,$sql);

 

 if($result&&sendmail($_POST['email'],$v_code))

 

{

echo "<script>

alert ('Registration Succesfully');

window.location.href='signupform.html';

</script>";

}

 

else

{

echo "<script>

alert ('Registration Failed');

</script>"; 

}

 

 


 

 



?>


step 4:- verify.php file code:-


<?php

include("connect.php");


$email=$_GET['email'];

$v_code=$_GET['v_code'];


if(isset($_GET['email')&& isset($_GET['v_code']))

  {

  

  $sql="select from user where email='$email' And v_code='$v_code'";

  

  $result=mysqli_query($con,$sql);

  $num=mysqli_num_rows($result);

  

   if($num>0)

   

   {

   $result_fetch=mysqli_fetch_array($result);

   if($result_fetch['is_verified']==0)

    {

$sql="update user set is_verified='1' where email='$result_fetch'";

if(mysqli_query($con,$sql)

{

echo "<script>

alert("email verification succesful");

</script>";

}

else{

echo "<script>

alert("email verification failed");

</script>";

}

}

   }

   else

   {

   echo "<script>

alert("email already registered");  

</script>";// not executed

   }

  

  }



?>

No comments:

Post a Comment