<?php
// diagnosticar_smtp.php
require __DIR__ . '/PHPMailer/PHPMailer-master/src/Exception.php';
require __DIR__ . '/PHPMailer/PHPMailer-master/src/PHPMailer.php';
require __DIR__ . '/PHPMailer/PHPMailer-master/src/SMTP.php';
require_once 'config_smtp.php';

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

echo "<h2>🔧 Diagnóstico SMTP - notificacao@son.tvitecnologia.com.br</h2>";

// Testar diferentes hosts
$hosts_teste = [
    'dedi-14932523.tvitecnologia.com.br',
    'mail.son.tvitecnologia.com.br',
    'smtp.hostgator.com.br',
    'son.tvitecnologia.com.br'
];

$portas_teste = [587, 465, 25];
$usuario = 'notificacao@son.tvitecnologia.com.br';
$senha = '%WT3YvAvj+!C';

foreach ($hosts_teste as $host) {
    foreach ($portas_teste as $porta) {
        $secure = ($porta == 465) ? 'ssl' : 'tls';
        
        echo "<hr>";
        echo "<h3>📡 Testando: $host:$porta ($secure)</h3>";
        
        $mail = new PHPMailer(true);
        
        try {
            $mail->isSMTP();
            $mail->Host       = $host;
            $mail->SMTPAuth   = true;
            $mail->Username   = $usuario;
            $mail->Password   = $senha;
            $mail->SMTPSecure = $secure;
            $mail->Port       = $porta;
            $mail->CharSet    = 'UTF-8';
            $mail->SMTPDebug  = 2; // Debug completo
            
            // Opções SSL
            $mail->SMTPOptions = [
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                ]
            ];
            
            echo "<p style='color: blue;'>🔄 Tentando conectar...</p>";
            
            // Tentar conectar
            if ($mail->smtpConnect()) {
                echo "<p style='color: green; font-weight: bold;'>✅ CONEXÃO BEM-SUCEDIDA!</p>";
                $mail->smtpClose();
                
                // Se conectou, tentar enviar email
                echo "<p style='color: blue;'>🔄 Tentando enviar email...</p>";
                
                $mail2 = new PHPMailer(true);
                $mail2->isSMTP();
                $mail2->Host       = $host;
                $mail2->SMTPAuth   = true;
                $mail2->Username   = $usuario;
                $mail2->Password   = $senha;
                $mail2->SMTPSecure = $secure;
                $mail2->Port       = $porta;
                $mail2->CharSet    = 'UTF-8';
                $mail2->SMTPOptions = [
                    'ssl' => [
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    ]
                ];
                
                $mail2->setFrom($usuario, 'Teste SON');
                $mail2->addAddress('SEU-EMAIL@gmail.com'); // COLOQUE SEU GMAIL AQUI!
                
                $mail2->Subject = "Teste $host:$porta";
                $mail2->Body = "<h2>Teste de envio</h2><p>Host: $host:$porta</p>";
                
                if ($mail2->send()) {
                    echo "<p style='color: green; font-weight: bold;'>✅ EMAIL ENVIADO COM SUCESSO!</p>";
                    echo "<p>✅✅✅ CONFIGURAÇÃO FUNCIONA: Host=$host, Porta=$porta, Secure=$secure</p>";
                    break 3; // Sai de todos os loops
                }
            }
            
        } catch (Exception $e) {
            echo "<p style='color: red;'>❌ FALHA: " . $e->getMessage() . "</p>";
            
            if (strpos($e->getMessage(), 'authenticate') !== false) {
                echo "<p style='color: orange;'>⚠️ Erro de autenticação - Verifique usuário/senha</p>";
            }
        }
    }
}
?>