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

require 'vendor/autoload.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = strip_tags(trim($_POST["name"]));
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["message"]);

    if (empty($name) || empty($email) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Invalid input!";
        exit;
    }

    $mail = new PHPMailer(true);
    try {
        // SMTP Server Configuration
        $mail->isSMTP();
        $mail->Host = 'smtp.zoho.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'care@gaurly.com';  // Your Zoho Email
        $mail->Password = 'your_password_here';  // Your Zoho Email Password
        $mail->SMTPSecure = 'ssl';
        $mail->Port = 465;

        // Email Content
        $mail->setFrom('care@gaurly.com', 'Gaurly');
        $mail->addAddress('care@gaurly.com'); // Your Zoho Email
        $mail->addReplyTo($email, $name);

        $mail->Subject = "New Contact Form Submission from " . $name;
        $mail->Body = "You have received a new message from your website contact form.\n\n".
                      "Name: $name\n".
                      "Email: $email\n\n".
                      "Message:\n$message\n";

        $mail->send();
        echo "success";
    } catch (Exception $e) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
}
?>
