<?php
// Function to sanitize user input
function sanitize_input($data) {
    return htmlspecialchars(stripslashes(trim($data)));
}

// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Validate and sanitize form data
    $userName = sanitize_input($_POST['name']);
    $userEmail = filter_var(sanitize_input($_POST['email']), FILTER_VALIDATE_EMAIL);
    $userMessage = sanitize_input($_POST['message']);

    // Error handling if validation fails
    if (empty($userName) || empty($userEmail) || empty($userMessage)) {
        echo 'All fields are required. Please fill out the form completely.';
    } elseif (!$userEmail) {
        echo 'Invalid email format. Please provide a valid email address.';
    } else {
        // Import PHPMailer classes into the global namespace
        use PHPMailer\PHPMailer\PHPMailer;
        use PHPMailer\PHPMailer\Exception;

        // Load Composer's autoloader
        require_once 'vendor/autoload.php';

       / Configuration
$smtpHost = 'smtp.zoho.com';
$smtpUsername = 'support@antpoolminersfx.com';
$smtpPassword = 'Swordfish1!#*';
$smtpPort = 587;
$smtpSecure = 'tls';
$fromEmail = 'antpoolmining@outlook.com';
$fromName = 'Antpool';
$toEmail = 'support@antpoolminersfx.com';

        // Create a new PHPMailer instance
        $mail = new PHPMailer(true);

        try {
            // SMTP settings
            $mail->isSMTP();
            $mail->Host = $smtpHost;
            $mail->SMTPAuth = true;
            $mail->Username = $smtpUsername;
            $mail->Password = $smtpPassword;
            $mail->SMTPSecure = $smtpSecure;
            $mail->Port = $smtpPort;

            // Optional settings for shared hosting
            $mail->SMTPAutoTLS = false; // Disable auto TLS if it causes issues
            $mail->SMTPKeepAlive = true;  // To avoid reconnecting to the server

            // Set the sender's address
            $mail->setFrom($fromEmail, $fromName);

            // Add recipient
            $mail->addAddress($toEmail);

            // Email subject and body content
            $mail->Subject = 'New Message from Website Contact Form';
            
            // Set email format to HTML and include plain text version
            $mail->isHTML(true);
            $mail->Body = "<h2>You have received a new message from the website contact form.</h2>
                           <p><strong>Name:</strong> $userName</p>
                           <p><strong>Email:</strong> $userEmail</p>
                           <p><strong>Message:</strong><br>$userMessage</p>";
            $mail->AltBody = "You have received a new message from $userName ($userEmail).\n\nMessage:\n$userMessage";

            // Send the email
            $mail->send();
            echo 'Your message has been sent successfully! We will get back to you shortly.';
        } catch (Exception $e) {
            // Catch errors and display them
            echo 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
        }
    }
}
?>