<?php
session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/PHPMailerAutoload.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name    = $_POST['name'];
    $mobile  = $_POST['mobile'];
    $email   = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    // Input validation
    if (empty($name) || empty($mobile) || empty($email) || empty($subject) || empty($message)) {
        $_SESSION['msg'] = 'fail';
        header('Location: index.php');
        exit();
    }

    $mail = new PHPMailer(true);

    try {
        // SMTP config
        $mail->isSMTP();
        $mail->Host       = 'smtp.gmail.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'query.mamasmile@gmail.com'; // ✅ Your Gmail
        $mail->Password   = 'AEIOU!@#123';         // 🔐 Use App Password (not real password)
        $mail->SMTPSecure = 'tls';
        $mail->Port       = 587;

        // Email headers
        $mail->setFrom('query.mamasmile@gmail.com', 'Mama Smile Website');
        $mail->addAddress('query.mamasmile@gmail.com', 'Pratibha');

        // Content
        $mail->isHTML(false);
        $mail->Subject = "New Inquiry from Website: $subject";
        $mail->Body    = "Name: $name\nMobile: $mobile\nEmail: $email\n\nMessage:\n$message";

        $mail->send();
        $_SESSION['msg'] = 'ok';
    } catch (Exception $e) {
        $_SESSION['msg'] = 'fail';
    }

    header('Location: index.php');
    exit();
}
?>
