<?php
// Include PHPMailer library
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
include 'PHPMailer.php';
include 'SMTP.php';

// use PHPMailer\PHPMailer\PHPMailer;
// Load Composer's autoloader or manually include PHPMailer files
require 'vendor/autoload.php'; // If using Composer
// require 'path-to-phpmailer/src/PHPMailer.php';
// require 'path-to-phpmailer/src/SMTP.php';
// require 'path-to-phpmailer/src/Exception.php';

// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = htmlspecialchars(trim($_POST['vname']));
    $email = htmlspecialchars(trim($_POST['vemail']));
    $phone = htmlspecialchars(trim($_POST['phonenumber']));
    $message = htmlspecialchars(trim($_POST['msg']));

    // Validate inputs
    if (empty($name) || empty($email) || empty($phone) || empty($message)) {
        die('Please fill out all fields.');
    }

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

    try {
        // SMTP configuration
        $mail->isSMTP();
        $mail->Host = 'smtp.sendgrid.net'; // Replace with your SMTP server
        $mail->SMTPAuth = true;
        $mail->Username = 'apikey'; // Replace with your SMTP username
        $mail->Password = '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';   // Replace with your SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Use `PHPMailer::ENCRYPTION_STARTTLS` for TLS
        $mail->Port = 465; // Use 587 for TLS, 465 for SSL

        // Email settings
        $mail->setFrom('sales@tmcards.com', 'Promotional playing cards'); // Sender's email and name
        $mail->addAddress('sales@tmcards.com'); // Recipient's email and name

        // Content
        $mail->isHTML(true);
        $mail->Subject = 'New Contact Form Submission From Promotional playing cards website';
        $mail->Body = "<p><strong>Name:</strong> $name</p> <br>
                       <p><strong>Email:</strong> $email</p><br>
                       <p><strong>Phone:</strong> $phone</p><br>
                       <p><strong>Message:</strong> $message</p><br>
                       <b> From Promotional playing cards </b> ";

        // Send email
        if ($mail->send()) {
            header('location: thank-you.php');
        } else {
            echo 'Message could not be sent.';
        }
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
} else {
    echo 'Invalid request.';
}
