<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Check if all required fields are filled
    if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['number']) && !empty($_FILES['document']['name'])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $number = $_POST['number'];

        // File upload handling
        $targetDirectory = "uploads/";
        $targetFile = $targetDirectory . basename($_FILES['document']['name']);

        if (move_uploaded_file($_FILES['document']['tmp_name'], $targetFile)) {
            // Email sending
            $to = 'pawarsmita83@gmail.com';
            $subject = 'New Form Submission';

            // Add a subject to the email message
            $message = "Name: $name\nEmail: $email\nNumber: $number";
            $message = wordwrap($message, 70); // Word wrap for email formatting

            // Set additional headers for email
            $headers = "From: $email\r\n";
            $headers .= "Reply-To: $email\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

            if (mail($to, $subject, $message, $headers)) {
                // Success message
                echo 'Thank you for your submission!';

                // Redirect to index.html after a delay (3 seconds in this example)
                header('Refresh: 3; URL=index.html');
            } else {
                echo 'There was an error sending the email.';
            }
        } else {
            echo 'Error uploading the document.';
        }
    } else {
        echo 'Please fill out all the required fields.';
    }
}
?>
