prepare("SELECT id FROM users WHERE email = ?"); $stmt->execute([$email]); $user = $stmt->fetch(); if ($user) { $token = bin2hex(random_bytes(50)); $expires_timestamp = time() + 1800; // 30 minutes $expires_datetime = date('Y-m-d H:i:s', $expires_timestamp); $stmt = $pdo->prepare("INSERT INTO password_resets (email, token, expires_at) VALUES (?, ?, ?)"); $stmt->execute([$email, $token, $expires_datetime]); $reset_link = "http://" . $_SERVER['HTTP_HOST'] . "/reset_password_form.php?token=" . $token; $subject = "Password Reset Request"; $body = "
Hello,
"; $body .= "You requested a password reset. Click the link below to reset your password:
"; $body .= ""; $body .= "This link will expire in 30 minutes.
"; $body .= "If you did not request a password reset, please ignore this email.
"; // Use MailService to send the email $mail_result = MailService::sendMail($email, $subject, $body, strip_tags($body)); if (!empty($mail_result['success'])) { $message = 'A password reset link has been sent to your email address.'; $message_type = 'success'; } else { $message = 'Could not send the password reset email. Please try again later.'; error_log("MailService Error: " . ($mail_result['error'] ?? 'Unknown error')); } } else { $message = 'No user found with that email address.'; } } catch (PDOException $e) { $message = "Database error: " . $e->getMessage(); error_log($message); } catch (Exception $e) { $message = "An error occurred: " . $e->getMessage(); error_log($message); } } $_SESSION['message'] = $message; $_SESSION['message_type'] = $message_type; header("Location: forgot_password.php"); exit; }