<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    // Recipient Email
    $to = "sriramuluchitfundspltd@gmail.com";
    
    // Subject
    $subject = "New Contact Inquiry from Website";
    
    // Sanitize input
    $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    $phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
    $message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
    $opt_out = isset($_POST['opt_out']) ? "Yes" : "No"; // Should likely be 'opt_in' logic based on text, but keeping var name consistent
    
    // Email Body
    $body = "<h2>New Contact Request</h2>";
    $body .= "<p><strong>Name:</strong> {$name}</p>";
    $body .= "<p><strong>Email:</strong> {$email}</p>";
    $body .= "<p><strong>Phone:</strong> {$phone}</p>";
    $body .= "<p><strong>Message:</strong><br>{$message}</p>";
    $body .= "<p><strong>Consent for Communication:</strong> {$opt_out}</p>";
    
    // Headers
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: <noreply@sriramuluchitfundspltd.online>" . "\r\n";
    $headers .= "Reply-To: {$email}" . "\r\n";
    
    // SMTP Configuration (Example - PHPMailer is recommended for production, but this is basic PHP mail)
    // If you have a specific SMTP server, you would typically use a library like PHPMailer.
    // Since I cannot install libraries here, this uses the server's default mail configuration.
    
    // Attempt to send email
    if (mail($to, $subject, $body, $headers)) {
        echo "<script>alert('Message sent successfully!'); window.location.href = 'index.html';</script>";
    } else {
        echo "<script>alert('Failed to send message. Please try again later.'); window.location.href = 'index.html';</script>";
    }
} else {
    // Not a POST request
    header("Location: index.html");
    exit;
}
?>
