<?php
try {
    require_once __DIR__ . '/../vendor/autoload.php';
    Dotenv\Dotenv::createImmutable(dirname(__DIR__))->safeLoad();
    define('BASE_PATH', dirname(__DIR__));
    
    $config = require BASE_PATH . '/config/mail.php';
    echo "Host: {$config['host']}:{$config['port']} / User: {$config['user']} / Enc: {$config['encryption']}<br>";
    
    $mail = new PHPMailer\PHPMailer\PHPMailer(true);
    $mail->SMTPDebug = 2;
    $mail->Debugoutput = function($str, $level) { echo htmlspecialchars($str)."<br>"; };
    $mail->isSMTP();
    $mail->Host = $config['host'];
    $mail->SMTPAuth = true;
    $mail->Username = $config['user'];
    $mail->Password = $config['pass'];
    $mail->SMTPSecure = $config['encryption'] === 'ssl' ? PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS : PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = (int)$config['port'];
    $mail->Timeout = 10;
    $mail->setFrom($config['from_address'] ?: $config['user'], 'Test');
    $mail->addAddress($config['from_address'] ?: $config['user']);
    $mail->Subject = 'Test SMTP';
    $mail->Body = 'Prueba';
    $mail->send();
    echo "<br>✅ ENVIADO";
} catch (Throwable $e) {
    echo "<br>❌ " . htmlspecialchars($e->getMessage());
}
