<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require __DIR__ . '/mailer/phpmailer/src/Exception.php';
require __DIR__ . '/mailer/phpmailer/src/PHPMailer.php';
require __DIR__ . '/mailer/phpmailer/src/SMTP.php';

function sendMail($to, $subject, $body) {
    $mail = new PHPMailer(true);

    try {
        // Enable SMTP
        $mail->isSMTP();
        $mail->Host       = 'smtp.gmail.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'alliantcuintl@gmail.com';  // Your Gmail address
        $mail->Password   = 'ouxxzkhrnxhiejlg';         // Gmail app password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port       = 587;

        // ✅ Gmail requires the sender to be the same account
        $mail->setFrom('alliantcuintl@gmail.com', 'Bmellon Intl');
        $mail->addAddress($to);

        // Email content
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $body;

        // Enable debug info
        $mail->SMTPDebug = 2;
        $mail->Debugoutput = function ($str, $level) {
            file_put_contents(__DIR__ . '/../debug_log.txt', date('Y-m-d H:i:s') . " | $str\n", FILE_APPEND);
        };

        $mail->send();
        return true;
    } catch (Exception $e) {
        // Log the error
        file_put_contents(__DIR__ . '/../debug_log.txt', date('Y-m-d H:i:s') . " | ERROR: {$mail->ErrorInfo}\n", FILE_APPEND);
        return false;
    }
}
?>
