-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_otp.php
More file actions
72 lines (57 loc) · 2.29 KB
/
Copy pathsend_otp.php
File metadata and controls
72 lines (57 loc) · 2.29 KB
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
<?php
session_start();
include 'includes/db_connect.php';
// LOAD PHPMAILER MANUALLY
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if (isset($_POST['send_code'])) {
$email = $_POST['email'];
// 1. Check if email exists
$check = $conn->prepare("SELECT id FROM users WHERE email = ?");
$check->bind_param("s", $email);
$check->execute();
$result = $check->get_result();
if ($result->num_rows > 0) {
// 2. Generate 6-digit OTP
$otp = rand(100000, 999999);
// 3. Save OTP to Database
$update = $conn->prepare("UPDATE users SET reset_token = ? WHERE email = ?");
$update->bind_param("ss", $otp, $email);
$update->execute();
// 4. Send Email using PHPMailer
$mail = new PHPMailer(true);
try {
// SMTP Settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'peerlink7@gmail.com'; // <--- CHANGE THIS
$mail->Password = 'bwlz dbtj zonl mwge'; // <--- CHANGE THIS (The 16 char code)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Email Content
$mail->setFrom('no-reply@peerlink.com', 'PeerLink Security');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = 'Your PeerLink Reset Code';
$mail->Body = "<h3>Password Reset Request</h3>
<p>Your verification code is: <b style='font-size: 20px;'>$otp</b></p>
<p>Enter this code to reset your password.</p>";
$mail->send();
// 5. Redirect to Verification Page
$_SESSION['reset_email'] = $email; // Save email for next step
header("Location: verify_otp.php");
exit();
} catch (Exception $e) {
header("Location: forgot_password.php?error=Message could not be sent. Mailer Error: {$mail->ErrorInfo}");
exit();
}
} else {
header("Location: forgot_password.php?error=Email not found!");
exit();
}
}
?>