<?php


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


    require 'mail/Exception.php';
    require 'mail/PHPMailer.php';
    require 'mail/SMTP.php';


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $first_name = isset($_POST['first_name']) ? htmlspecialchars($_POST['first_name']) : '';
    $last_name  = isset($_POST['last_name']) ? htmlspecialchars($_POST['last_name']) : '';
    $phone      = isset($_POST['phone']) ? htmlspecialchars($_POST['phone']) : '';
    $email      = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '';
    $country    = isset($_POST['country']) ? htmlspecialchars($_POST['country']) : '';
    $subject    = isset($_POST['subject']) ? htmlspecialchars($_POST['subject']) : 'Contact Form Submission';
    $message    = isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '';

    $mail = new PHPMailer(true);

    try {
        $mail->isSMTP();
        $mail->Host       = 'pld111.truehost.cloud';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'contact@roccoalessandro.com';      // Replace
        $mail->Password   = 'UFGB@peter2020';         // Replace
        $mail->SMTPSecure = 'tls';
        $mail->Port       = 587;

        $mail->setFrom('contact@roccoalessandro.com', 'Website Contact');
        $mail->addAddress('contact@roccoalessandro.com');             // Replace
        $mail->addReplyTo($email, $first_name);
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = "
            <h2>New Contact Form Submission</h2>
            <p><strong>Name:</strong> $first_name $last_name</p>
            <p><strong>Email:</strong> $email</p>
            <p><strong>Phone:</strong> $phone</p>
            <p><strong>Country:</strong> $country</p>
            <p><strong>Message:</strong><br>$message</p>
        ";

        $mail->send();
        echo "Message sent successfully.";
    } catch (Exception $e) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
} else {
    echo "Invalid request.";
}
