-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset-password.php
127 lines (106 loc) · 4.54 KB
/
reset-password.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
include('config/db.php');
require('includes/PHPMailer/src/PHPMailer.php');
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
// Initialize the session
session_start();
$msg = "";
if (isset($_POST, $_POST['resetPass'])) {
if (isset($_POST['email'])) {
$email = trim(htmlspecialchars($_POST['email']));
$code = rand(10000, 9999999);
//Load Composer's autoloader
require 'includes/vendor/autoload.php';
//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
// Check for email existence query
$query = "SELECT u_id FROM t_user WHERE u_email= :email";
if ($stmt = $pdo->prepare($query)) {
$stmt->execute(['email' => $email]);
if ($stmt->rowCount() == 0) {
$msg = "<div class='alert alert-danger'>User Not Exist!</div>";
} else {
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
//Recipients
$mail->setFrom('supp.shop2021@gmail.com', 'F&Co. Shop');
$mail->addAddress($email);
//Content
$mail->isHTML(true);
$mail->Subject = 'Reset Password Code';
$mail->Body = 'Your reset password code is: <b>' . $code . '</b>';
$mail->send();
$msg = '<div class="alert alert-success">Message has been sent</div>';
// set the code in session
$_SESSION['RESET_CODE'] = $code;
$_SESSION['RESET_EMAIL'] = $email;
// resirect to next step
header("location: reset-password2.php");
} catch (Exception $e) {
$msg = "<div class='alert alert-danger'>Message could not be sent email!</div>";
}
}
}
}
}
?>
<?php
$page_title = "Reset Password";
include('includes/head.php');
?>
<div class="container">
<div class="row m-auto col-sm-12 max-width-600">
<div class="col-sm-12 col-md">
<div class="card mt-5">
<div class="card-header text-center">
<h1>Reset Password</h1>
</div>
<div class="card-body">
<?php
if ($msg != "" || !empty($msg)) {
echo $msg;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<?php
if (!empty($error)) {
echo " <div class='alert alert-danger text-center'>$error</div>";
}
?>
<div class="form-group">
<label for="email">Email</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="fa fa-envelope font-icon"></i>
</span>
</div>
<input class="form-control" type="email" id="email" name="email"
placeholder="Email" style="direction: ltr">
</div>
<span id="emailAlert" class="alert-span"></span>
</div>
<button class="btn btn-dark btn-block font-weight-bold font-md mt-5" type="submit"
name="resetPass" id="resetBtn">Reset Password
</button>
</form>
</div>
</div>
</div>
</div>
</div>
<?php
include('includes/tail.php');
?>