<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $fname   = htmlspecialchars($_POST['fname']);
    $lname   = htmlspecialchars($_POST['lname']);
    $phone   = htmlspecialchars($_POST['phone']);
    $email   = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    $to = "info@sdex.com";
    $subject = "New Contact Form Submission - SDEX Website";

    $body = "
    First Name: $fname\n
    Last Name: $lname\n
    Phone: $phone\n
    Email: $email\n
    Message:\n$message
    ";

    $headers = "From: $email\r\n";
    $headers .= "Reply-To: $email\r\n";

    if (mail($to, $subject, $body, $headers)) {
        echo "<script>alert('Message Sent Successfully!'); window.location.href='index.html';</script>";
    } else {
        echo "<script>alert('Message Sending Failed. Please try again.'); window.history.back();</script>";
    }

}
?>
