<?php
// Load Composer's autoloader or manual PHPMailer includes
require '../vendor/autoload.php';  // if using Composer
// If manually including PHPMailer files
// require 'PHPMailer/src/Exception.php';
// require 'PHPMailer/src/PHPMailer.php';
// require 'PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Your script code starts here, for example:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $_POST['email'];  // Example form field

    $mail = new PHPMailer(true);  // Create an instance of PHPMailer

    try {
        // Set mailer to use SMTP
        $mail->isSMTP();
        $mail->Host = 'mail.mfdrivingschool.co.tz';
        $mail->SMTPAuth = true;
        $mail->Username = 'support@mfdrivingschool.co.tz';
        $mail->Password = 'support@2024';
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;

        // Recipient settings
        $mail->setFrom('support@mfdrivingschool.co.tz', 'Trust Driving School');
        $mail->addAddress($email);
        $mail->addReplyTo('support@mfdrivingschool.co.tz', 'Support');
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );

        // Email content
        $mail->isHTML(true);
        $mail->Subject = 'RESET PASSWORD';
        $mail->Body    = 'We have received a request to reset your password.<br/> 
                          Please click the given link to change your password:<br>
                          link: <a href="https://mfdrivingschool.co.tz/drivingschool/auth/reset_password.php?key=' . $crypt . '">click here</a> to reset your password';
        $mail->AltBody = 'We have received a request to reset your password. Please visit the link to change your password.';

        // Send email
        $mail->send();
        $sms = 'An Email has been sent to ' . $email;
    } catch (Exception $e) {
        $err =  'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
    }
}
