Path : /home/vishqocm/vishalwebtech.in/
File Upload :
Current File : /home/vishqocm//vishalwebtech.in//sendEmail.php

<?php

// Allow access from all origins
header("Access-Control-Allow-Origin: *");

// Optional: Specify allowed methods
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");

// Optional: Specify allowed headers
header("Access-Control-Allow-Headers: Content-Type, Authorization");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Collect form data
    $name = htmlspecialchars(trim($_POST['name']));
    $mobile = htmlspecialchars(trim($_POST['mobile']));
    $email = htmlspecialchars(trim($_POST['email']));
    $services = htmlspecialchars(trim($_POST['services']));
    $message = htmlspecialchars(trim($_POST['message']));

    // Validate email
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid email address.']);
        exit;
    }

    // Set recipient email
    $to = "[email protected]";  // Replace with your company email

    // Set email headers
    $headers = "From: " . $email . "\r\n";
    $headers .= "Reply-To: " . $email . "\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

    // Set email subject
    $subject = "New Contact Form Submission";

    // Create email body
    $body = "
        <h2>Contact Form Submission</h2>
        <p><strong>Name:</strong> $name</p>
        <p><strong>Mobile Number:</strong> $mobile</p>
        <p><strong>Email:</strong> $email</p>
        <p><strong>Service Requested:</strong> $services</p>
        <p><strong>Message:</strong><br>$message</p>
    ";

    // Send email
    if (mail($to, $subject, $body, $headers)) {
        echo json_encode(['status' => 'success', 'message' => 'Email sent successfully!']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Failed to send email. Please try again later.']);
    }
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request.']);
}
?>