<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // Make sure PHPMailer is installed
require 'config/database.php'; // Make sure PHPMailer is installed
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = $_POST['email'] ?? '';
if (empty($email)) {
$error = "Please enter your email address";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Invalid email format";
} else {
// Check if email exists
$query = "SELECT id FROM users WHERE email = ? LIMIT 1";
$firstname = "SELECT first_name FROM users WHERE email = ? LIMIT 1";
$lastname = "SELECT last_name FROM users WHERE email = ? LIMIT 1";
$phonenumber = "8931095984";
$name = $firstname . $lastname;
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
// Generate reset token
$token = bin2hex(random_bytes(32));
$expires = date('Y-m-d H:i:s', strtotime('+1 hour'));
// Store reset token in database
$update_query = "UPDATE users SET reset_token = ?, reset_expires = ? WHERE email = ?";
$update_stmt = $conn->prepare($update_query);
$update_stmt->bind_param("sss", $token, $expires, $email);
if ($update_stmt->execute()) {
// Send reset email
$reset_link = "http://" . $_SERVER['HTTP_HOST'] . "/reset-password.php?token=" . $token;
// TODO: Implement email sending functionality
// For now, just show success message
$token = 'EAAY24NC5zwkBO19HMEHO3VGCo1nADZCqNU3ZCBk227hf8uH2CAaGCMHe2flI3O117J6UUdwQXCBoZBrdGetI3WieKkxnrikAF4jZCC3DPDyHNTFCAwCvVtpkqOiP55KV6KKwniQGj1b2bx9BE29Ol3ZCcVJxQrOuvhLBNg952i7WsrBWN2Nt2an0EyFS2c3NpfN4qj0xc0qSN0gfLdjX2VdnzqS80';
$phoneNumberId = '592611757275579';
$recipient = '91XXXXXXXXXX'; // user phone number with country code
// $resetLink = "http://localhost/reset-password.php?token=ABC123";
$messageData = [
'messaging_product' => 'whatsapp',
'to' => $phonenumber,
'type' => 'text',
'text' => [
'body' => "Hi! Click here to reset your password: $reset_link"
]
];
$ch = curl_init("https://graph.facebook.com/v19.0/$phoneNumberId/messages");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($messageData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Send email using PHPMailer
$mail = new PHPMailer(true);
try {
// SMTP setup
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Your Gmail
$mail->Password = 'jqbkssawpizdntad'; // App Password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 587;
// Email settings
$mail->setFrom('[email protected]', 'Your App');
$mail->addAddress($email);
$mail->Subject = 'Reset Your Password';
$mail->Body = "Hi, click the link below to reset your password:\n$resetLink";
$mail->send();
echo "Password reset link sent to your email.";
} catch (Exception $e) {
echo "Error: {$mail->ErrorInfo}";
}
// if(sendResetEmail($email, $name, $reset_link, $site_name)){
// $success = "A password reset link has been sent to your email address. Please check your inbox.";
// } else {
// $error = "Failed to send reset email. Please try again later.";
// }
} else {
$error = "Failed to process request. Please try again.";
}
} else {
$error = "Email not found in our records";
}
}
}
?>
// if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// $email = $_POST['email'];
// $resetLink = "http://localhost/reset-password.php?token=" . urlencode($token);
// ?>