<?php
// Show all errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
// print_r($_POST);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Sanitize inputs
    $name    = htmlspecialchars(trim($_POST['name']));
    $email   = htmlspecialchars(trim($_POST['email']));
    $phone   = htmlspecialchars(trim($_POST['phone']));
    $subject = htmlspecialchars(trim($_POST['subject']));
    $message = htmlspecialchars(trim($_POST['message']));

    $formType = isset($_POST['form_type']) ? $_POST['form_type'] : "General";

    $mail = new PHPMailer(true);
   
    try {
        // SMTP settings
        $mail->isSMTP();
        $mail->SMTPDebug = 4; 
        $mail->Host       = 'mail.trioniccapitalpartners.com';   // Or your SMTP host
        $mail->SMTPAuth   = true;
        $mail->Username   = 'trioniccapitalpartners.com'; // Replace with your email
        $mail->Password   = 'yyeZ0;jPwQAn';   // Gmail app password
        $mail->SMTPSecure = 'tls';
        $mail->Port       = 587;
        //  $mail->SMTPOptions = array (
        // 'ssl' => array(
        //     'verify_peer'  => false,
        //     'verify_peer_name'  => false,
        //     'allow_self_signed' => true));
            

        // From & To
        $mail->setFrom('no-reply@trioniccapitalpartners.com', 'Trionic Capital Partners');
        $mail->addAddress('clix@appdemo.space');

        // Email content
        $mail->isHTML(false);
        $mail->Subject = "[$formType] New Submission: " . $subject;
        $mail->Body    = "Form: $formType\n\nName: $name\nEmail: $email\nPhone: $phone\n\nMessage:\n$message";

        $mail->send();
        // print_r($mail->send());exit;
        $status = "success";
    } catch (Exception $e) {
        $status = "error";
        // // Debugging (optional)
        echo "Mailer Error: " . $mail->ErrorInfo;
        exit;
    }

    header("Location: " . $_SERVER["HTTP_REFERER"] . "?status=" . $status);
    exit;
}
?>
