Skip to content

Instantly share code, notes, and snippets.

@sayedchowdury5
Created December 29, 2020 18:47
Show Gist options
  • Save sayedchowdury5/4919166575f21502aeedab76192e38cb to your computer and use it in GitHub Desktop.
Save sayedchowdury5/4919166575f21502aeedab76192e38cb to your computer and use it in GitHub Desktop.
Forget Password with email verification using php pdo and PHPMailer library by composer
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once ('connect.php');
//require 'C:/Users/User/AppData/Roaming/Composer/vendor/autoload.php';
require './vendor/autoload.php';
if (isset($_POST['forget'])) {
$user_email = $_POST['user_email'];
$sql = "SELECT user_email FROM users WHERE user_email = ? ";
$prepare = $conn->prepare($sql);
$prepare->execute([$user_email]);
$rowCount = $prepare->rowCount();
//$fetch = $prepare->fetchAll();
//$user_name = $fetch['user_name'];
//$url = 'http://'.$_SERVER['SERVER_NAME'].'blog/resetpassword.php?id='.$fetch['user_id'].'&email='.$user_email;
if ($rowCount > 0) {
$mail = new PHPMailer();
$mail->isSMTP();
//$mail->SMTPDebug = 2;
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'email id';
$mail->Password = 'email password';
//$mail->SMTPSecure = 'tls';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('email id', 'name'); //name is optional
$mail->addAddress($user_email);
//$mail->addReplyTo('towho@example.com', 'John Doe';
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "Forgot Password";
//$mail->addEmbeddedImage('path/to/image_file.jpg', 'image_cid');
//$mail->Body = '<img src="cid:image_cid"> Mail body in HTML';
$mail->Body = 'This is the plain text version of the email content';
$mail->AltBody = 'This is the plain text version of the email content';
if(!$mail->send()){
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}else{
echo 'Message has been sent';
}
} else {
echo '<div><p>Your email not exist</p></div>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container mt-5">
<div class="border rounded mt-5">
<form action="forgetpassword.php" method="POST" enctype="multipart/form-data">
<div><h4 class=" text-center text-danger">Forget Password</h4></div>
<label for="user_email">Enter Your Email</label>
<input type="email" class="form-control" id="user_email" name="user_email" required>
<div class="mt-3 d-flex justify-content-center">
<input type="submit" class=" btn btn-success" id="forget" name="forget" value="Send">
</div>
</form>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment