<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Load PHPMailer
require 'phpmailer/Exception.php';
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $name    = $_POST['form_name'];
    $email   = $_POST['email'];
    $subject = $_POST['form_subject'];
    $message = $_POST['form_message'];

$mail = new PHPMailer(true);

try {
    // SMTP Configuration
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com';     // SMTP server
    $mail->SMTPAuth   = true;
    $mail->Username   = 'snehasshinde123@gmail.com'; // Your Gmail
    $mail->Password   = 'zigwxujvnqpcdhf';    // App Password (NOT your Gmail password)
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;

    // Sender & Recipient
    $mail->setFrom('info@Spike.com', 'Spike Pest email');
    $mail->addAddress('pranjal@shinoracorp.com', 'Spike Pest');
    
    $mail->addCC('sneha@shinoracorp.com');

    // Email Content
    $mail->isHTML(true);
    $mail->Subject = 'Test Email from PHPMailer';
    $body  = "<h2>New Contact Form Submission</h2>";
    $body .= "<p><strong>Name:</strong> " . htmlspecialchars($name) . "</p>";
    $body .= "<p><strong>Email:</strong> " . htmlspecialchars($email) . "</p>";
    $body .= "<p><strong>Subject:</strong> " . htmlspecialchars($subject) . "</p>";
    $body .= "<p><strong>Message:</strong><br>" . nl2br(htmlspecialchars($message)) . "</p>";

    $mail->Body    = $body;

    $mail->AltBody = strip_tags($message);

    $mail->send();
    echo 'Message has been sent successfully';
} catch (Exception $e) {
    echo "Message could not be sent. Error: {$mail->ErrorInfo}";
}
} else {
    echo "Access denied.";
}