<?php
// Get form data
$name = $_POST['yourName'] ?? '';
$email = $_POST['yourEmail'] ?? '';
$phone = $_POST['yourPhone'] ?? '';
$message = $_POST['comments'] ?? '';

// Validate basic input
if (empty($name) || empty($email) || empty($message)) {
    echo "<script>
        alert('Please fill in all required fields.');
        window.history.back();
    </script>";
    exit;
}

// Email recipient
$to = "developerm4b@gmail.com"; // Replace with your email

// Subject
$subject = "New Contact Form Submission from $name";

// Message Body
$body = "Name: $name\n";
$body .= "Email: $email\n";
$body .= "Phone: $phone\n";
$body .= "Message:\n$message\n";

// Email headers
$headers = "From: $name <$email>\r\n";
$headers .= "Reply-To: $email\r\n";

// Send email
$sent = mail($to, $subject, $body, $headers);

// Feedback to user
if ($sent) {
    echo "<script>
        alert('Your message has been sent successfully.');
        window.location.href = 'index.php';
    </script>";
} else {
    echo "<script>
        alert('Failed to send your message. Please try again.');
        window.history.back();
    </script>";
}
?>
