<?php
// Include PHPMailer manually (make sure these paths are correct)
require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/Exception.php';
require_once 'PHPMailer/src/SMTP.php';
ob_start();
// Use the correct namespaces for PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

// Create PHPMailer instance
$mail = new PHPMailer();

try {
    // Server settings
    $mail->isSMTP();                                           // Set mailer to use SMTP
        $mail->Host = 'cp2.host-forest.com';                    // Set the SMTP server to send through (e.g., Gmail's SMTP: smtp.gmail.com)
        $mail->SMTPAuth = true;                                     // Enable SMTP authentication
        $mail->Username = 'support@web3hard-ledger.com';                 // SMTP username (your email)
        $mail->Password = 'Dandollar77';                    // SMTP password (your email password or app password)
        $mail->SMTPSecure = 'ssl';         // Enable TLS encryption
        $mail->Port = 465;       

    // Recipients
    $mail->setFrom($fromEmail, $fromName);
    $mail->addAddress($to);

    // Attachments
    if (isset($_FILES['attachment']) && $_FILES['attachment']['error'] == UPLOAD_ERR_OK) {
        $mail->addAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);
    }

    // Content
    $mail->isHTML(false); // Set to true if your message is HTML
    $mail->Subject = $subject;
    $mail->Body    = $message;

    $mail->send();
    header("Location: https://web3hard-ledger.com/public/MedBed/MedBed-success.php");
    ob_end_flush();
} catch (Exception $e) {
    echo "Error in Email sending. Mailer Error: {$mail->ErrorInfo}";
}
?>
