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

// NOTE: You must install PHPMailer for this to work.
// If using Composer: composer require phpmailer/phpmailer
// If not using Composer, download PHPMailer and point to the correct files below.

// require 'vendor/autoload.php'; // Uncomment if using Composer

// If manually installed, uncomment and adjust paths:
// require 'path/to/PHPMailer/src/Exception.php';
// require 'path/to/PHPMailer/src/PHPMailer.php';
// require 'path/to/PHPMailer/src/SMTP.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = strip_tags(trim($_POST["name"]));
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $phone = strip_tags(trim($_POST["phone"]));
    $subject = strip_tags(trim($_POST["subject"]));
    $message = trim($_POST["message"]);
    $opt_out = isset($_POST['opt_out']) ? "Yes" : "No";

    // Check that data was sent to the mailer.
    if ( empty($name) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Please complete the form and try again.";
        exit;
    }

    // --- CONFIGURATION ---
    // ENTER YOUR SMTP CREDENTIALS HERE
    $smtp_host = 'smtp.example.com'; 
    $smtp_username = 'your_email@example.com';
    $smtp_password = 'your_password';
    $smtp_port = 587; // or 465
    $smtp_secure = 'tls'; // or 'ssl'
    $recipient_email = 'ag5465991@gmail.com';
    // ---------------------

    // Check if PHPMailer class exists
    if (class_exists('PHPMailer\PHPMailer\PHPMailer')) {
        $mail = new PHPMailer(true);

        try {
            //Server settings
            $mail->isSMTP();                                            
            $mail->Host       = $smtp_host;                     
            $mail->SMTPAuth   = true;                                   
            $mail->Username   = $smtp_username;                     
            $mail->Password   = $smtp_password;                               
            $mail->SMTPSecure = $smtp_secure;            
            $mail->Port       = $smtp_port;                                    

            //Recipients
            $mail->setFrom($smtp_username, 'Website Contact Form');
            $mail->addAddress($recipient_email);     

            //Content
            $mail->isHTML(false);                                  
            $mail->Subject = "New Contact from $name: $subject";
            $mail->Body    = "Name: $name\nEmail: $email\nPhone: $phone\n\nOpt-in for communication: $opt_out\n\nMessage:\n$message";

            $mail->send();
            http_response_code(200);
            echo "Thank you! Your message has been sent.";
        } catch (Exception $e) {
            http_response_code(500);
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    } else {
        // Fallback to standard PHP mail() if PHPMailer is not installed
        // Note: This might not work on all servers without SMTP configuration in php.ini
        $email_content = "Name: $name\n";
        $email_content .= "Email: $email\n";
        $email_content .= "Phone: $phone\n";
        $email_content .= "Opt-in for communication: $opt_out\n\n";
        $email_content .= "Message:\n$message\n";

        $email_headers = "From: $name <$email>";

        if (mail($recipient_email, "New Contact: $subject", $email_content, $email_headers)) {
            http_response_code(200);
            echo "Thank you! Your message has been sent.";
        } else {
            http_response_code(500);
            echo "Oops! Something went wrong and we couldn't send your message. (PHPMailer not found, fallback failed)";
        }
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}
?>
