<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-6.9.3/src/Exception.php';
require 'PHPMailer-6.9.3/src/PHPMailer.php';
require 'PHPMailer-6.9.3/src/SMTP.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars(trim($_POST["name"]));
    $email = htmlspecialchars(trim($_POST["email"]));
    $phone = htmlspecialchars(trim($_POST["phone"]));
    $subject = htmlspecialchars(trim($_POST["subject"]));
    $message = htmlspecialchars(trim($_POST["message"]));

    $mail = new PHPMailer(true);

    try {
        // Server settings
        $mail->isSMTP();
        $mail->Host = 'mail.asapeats.com.au';
        $mail->SMTPAuth = true;
        $mail->Username = 'requests@asapeats.co.au';
        $mail->Password = 'requests@4321';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = 587;  

        // Recipients
        $mail->setFrom('requests@asapeats.co.au', 'ASAP EATS');
        $mail->addAddress('info@asapeats.co.au');
        $mail->addAddress('harshplad@gmail.com');

        // Content
        $mail->isHTML(true);
        $mail->Subject = 'New Contact Us Request: ' . $subject;
        $mail->Body    = "<p>Name: $name</p>
                          <p>Email: $email</p>
                          <p>Phone: $phone</p>
                          <p>Subject: $subject</p>
                          <p>Message:</p>
                          <p>$message</p>";
        $mail->AltBody = "Name: $name\n
                          Email: $email\n
                          Phone: $phone\n
                          Subject: $subject\n
                          Message:\n$message";

        $mail->send();
        header("Location: contact.php?status=success&message=" . urlencode("Thank you for contacting us. We will get back to you shortly."));
        exit();
    } catch (Exception $e) {
        header("Location: contact.php?status=error&message=" . urlencode("There was an error sending your message. Please try again later."));
        exit();
    }
} else {
    header("Location: contact.php?status=error&message=" . urlencode("Invalid request method."));
    exit();
}
?>