<?php
session_start();

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['phone']) && !empty($_POST['message'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone_number = $_POST['phone'];
    $message = $_POST['message'];
    $mail = new PHPMailer(true);

    try {
        // SMTP configuration
        $mail->isSMTP();
        $mail->Host = 'newdedi.clouddreams.in';
        $mail->SMTPAuth = true;
        $mail->Username = 'tester@clouddreams.in';
        $mail->Password = 'OUHbP$W2$';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 25;

        // Sender and recipient settings
        $mail->setFrom('tester@clouddreams.in', 'T&C Sports');
        $mail->addAddress('madhumitha@clouddreams.in', 'Recipient Name');

        // Email content
        $mail->isHTML(true);
        $mail->Subject = 'New Message from Contact Form';
        $mail->Body = "
        <html>
        <head></head>
        <body></body>
        <body style='background-color: #f4f4f4; font-family: Arial, sans-serif; color: #333; padding: 20px; margin: 0;'>
            <div style='background-color: #ffffff; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); max-width: 600px; margin: 0 auto;'>
                <div style='font-size: 24px; font-weight: bold; margin-bottom: 20px; color: #4CAF50;'>New Message from Contact Form</div>
                <div style='font-size: 16px; line-height: 1.6;'>
                    <p style='margin: 10px 0;'><strong>Name:</strong> $name</p>
                    <p style='margin: 10px 0;'><strong>Email:</strong> $email</p>
                    <p style='margin: 10px 0;'><strong>Phone Number:</strong> $phone_number</p>
                    <p style='margin: 10px 0;'><strong>Message:</strong> $message</p>
                </div>
                <div style='margin-top: 20px; font-size: 12px; color: #777; border-top: 1px solid #e4e4e4; padding-top: 10px;'>
                    <p style='margin: 0;'>This email was sent from your website's contact form.</p>
                </div>
            </div>
        </body>
        </html>";
       
        $mail->send();
        echo json_encode(["status" => "success", "message" => "Message sent successfully!"]);

    } catch (Exception $e) {
        echo json_encode(["status" => "success", "message" => "Failed!"]);
    }
} else {
    echo json_encode(["status" => "success", "message" => "Failed!"]);
}


?>
