<?php
// Configuration
$to = "vishal.notani@viyuha.com";
$subject = "New Inquiry from Website";

// Get form inputs safely (handling both index.html and contact.html forms)
$name    = htmlspecialchars(trim($_POST['form_name'] ?? $_POST['name'] ?? ''));
$email   = filter_var(trim($_POST['form_email'] ?? $_POST['email'] ?? ''), FILTER_SANITIZE_EMAIL);
$phone   = htmlspecialchars(trim($_POST['form_phone'] ?? $_POST['phone'] ?? ''));
$sub     = htmlspecialchars(trim($_POST['form_subject'] ?? $_POST['service'] ?? 'Website Contact'));
$company = htmlspecialchars(trim($_POST['company'] ?? ''));
$message = htmlspecialchars(trim($_POST['form_message'] ?? $_POST['message'] ?? ''));

// Compose message
$body = "
You have received a new inquiry from the contact form on your website.

Full Name: $name
Email: $email
Mobile No: $phone
Company: " . ($company ? $company : "N/A") . "
Subject/Service: $sub

Message:
$message
";

// Email headers - Prevent DMARC issues
// The 'From' must be local to the server. The 'Reply-To' will let them click reply.
$headers = "From: Viyuha Website <noreply@viyuha.com>\r\n";
if ($email) {
    $headers .= "Reply-To: $name <$email>\r\n";
}
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

// Validate and send email
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    if (mail($to, $subject, $body, $headers)) {
        header("Location: index.html?status=success"); // Redirect back to index on success
        exit;
    } else {
        echo "Mail could not be sent. Please try again. Ensure PHP mail is configured on your hosting.";
    }
} else {
    echo "Invalid email address.";
}
?>
