<?php
// test_mail.php — SMTP debug tool
// Browser mein kholo: localhost/communeglobal_backend/test_mail.php
// DONE hone ke baad DELETE kar do!
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/config/mail.php';

$result = '';
$log    = '';

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $testEmail = trim($_POST['test_email'] ?? '');
    if (!$testEmail) { $result = 'error'; $log = 'Email required.'; goto show; }

    // Capture PHPMailer debug output
    ob_start();


    $mail = new PHPMailer(true);
    try {
        $mail->SMTPDebug  = 3; // Full debug
        $mail->Debugoutput = 'echo';
        $mail->isSMTP();
        $mail->Host       = SMTP_HOST;
        $mail->SMTPAuth   = true;
        $mail->Username   = SMTP_USER;
        $mail->Password   = SMTP_PASS;
        $mail->SMTPSecure = SMTP_SECURE;
        $mail->Port       = SMTP_PORT;
        $mail->CharSet    = 'UTF-8';
        $mail->SMTPOptions = ['ssl'=>['verify_peer'=>false,'verify_peer_name'=>false,'allow_self_signed'=>true]];
        $mail->setFrom(SMTP_USER, SMTP_FROM_NAME);
        $mail->addAddress($testEmail, 'Test User');
        $mail->isHTML(true);
        $mail->Subject = 'communeglobal SMTP Test';
        $mail->Body    = '<b>SMTP is working!</b> Sent at '.date('d M Y H:i:s');
        $mail->AltBody = 'SMTP working. Sent at '.date('d M Y H:i:s');
        $mail->send();
        $result = 'success';
        $log    = ob_get_clean();
    } catch (Exception $e) {
        $result = 'error';
        $log    = ob_get_clean() . "\n\nERROR: " . $mail->ErrorInfo;
    }
}
show:
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SMTP Test</title>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&display=swap" rel="stylesheet">
<style>
body{font-family:'Open Sans',sans-serif;background:#F0F4F8;padding:30px;max-width:700px;margin:0 auto}
h2{font-size:20px;font-weight:700;margin-bottom:20px;color:#1B3F6E}
.card{background:#fff;border-radius:10px;padding:24px;box-shadow:0 2px 12px rgba(0,0,0,.08);margin-bottom:20px}
label{font-size:13px;font-weight:600;display:block;margin-bottom:5px}
input[type=email]{width:100%;padding:10px 14px;border:1.5px solid #D1D5DB;border-radius:6px;font-size:14px;margin-bottom:14px;font-family:inherit;outline:none}
input[type=email]:focus{border-color:#1A7F8E}
button{padding:11px 28px;background:#1A7F8E;color:#fff;border:none;border-radius:6px;font-size:14px;font-weight:700;cursor:pointer}
button:hover{background:#136070}
.cfg{background:#F8FAFC;border:1px solid #E5E7EB;border-radius:6px;padding:16px;font-size:13px;line-height:1.8}
.cfg strong{color:#1B3F6E;min-width:140px;display:inline-block}
.success{background:#F0FDF4;border:1px solid #86EFAC;border-radius:6px;padding:14px;color:#166534;font-size:14px;font-weight:600;margin-bottom:16px}
.error{background:#FEF2F2;border:1px solid #FECACA;border-radius:6px;padding:14px;color:#991B1B;font-size:14px;font-weight:600;margin-bottom:16px}
pre{background:#1E293B;color:#E2E8F0;padding:16px;border-radius:6px;font-size:12px;overflow-x:auto;white-space:pre-wrap;max-height:400px;overflow-y:auto}
.warn{background:#FFFBEB;border:1px solid #FDE68A;border-radius:6px;padding:12px;color:#92400E;font-size:13px;margin-top:16px}
</style>
</head>
<body>

<h2>🔧 SMTP Connection Tester</h2>

<!-- Current config -->
<div class="card">
  <div style="font-size:13px;font-weight:700;margin-bottom:10px;color:#444">Current mail.php Config:</div>
  <div class="cfg">
    <div><strong>SMTP_HOST:</strong> <?= defined('SMTP_HOST') ? htmlspecialchars(SMTP_HOST) : '<span style="color:red">NOT SET</span>' ?></div>
    <div><strong>SMTP_PORT:</strong> <?= defined('SMTP_PORT') ? SMTP_PORT : '<span style="color:red">NOT SET</span>' ?></div>
    <div><strong>SMTP_SECURE:</strong> <?= defined('SMTP_SECURE') ? htmlspecialchars(SMTP_SECURE) : '<span style="color:red">NOT SET</span>' ?></div>
    <div><strong>SMTP_USER:</strong> <?= defined('SMTP_USER') ? htmlspecialchars(SMTP_USER) : '<span style="color:red">NOT SET</span>' ?></div>
    <div><strong>SMTP_PASS:</strong> <?= defined('SMTP_PASS') ? str_repeat('●', min(strlen(SMTP_PASS),12)) : '<span style="color:red">NOT SET</span>' ?></div>
    <div><strong>APP_URL:</strong> <?= defined('APP_URL') ? htmlspecialchars(APP_URL) : '<span style="color:red">NOT SET</span>' ?></div>
  </div>
</div>

<!-- Test form -->
<div class="card">
  <div style="font-size:13px;font-weight:700;margin-bottom:12px;color:#444">Send Test Email:</div>
  <?php if ($result === 'success'): ?>
    <div class="success">✅ Mail successfully sent! Inbox check karo.</div>
  <?php elseif ($result === 'error'): ?>
    <div class="error">❌ Mail send failed. Debug log neeche dekho.</div>
  <?php endif; ?>

  <form method="POST">
    <label>Send test to:</label>
    <input type="email" name="test_email"
           value="<?= htmlspecialchars($_POST['test_email']??'') ?>"
           placeholder="apna@gmail.com" required>
    <button type="submit">Send Test Mail</button>
  </form>

  <?php if ($log): ?>
  <div style="margin-top:16px;font-size:13px;font-weight:600;color:#444">Debug output:</div>
  <pre><?= htmlspecialchars($log) ?></pre>
  <?php endif; ?>
</div>

<!-- Common fixes -->
<div class="card">
  <div style="font-size:13px;font-weight:700;margin-bottom:10px;color:#444">Common Problems & Fixes:</div>
  <table style="width:100%;font-size:12.5px;border-collapse:collapse">
    <tr style="background:#F8FAFC">
      <th style="text-align:left;padding:8px;border:1px solid #E5E7EB">Error</th>
      <th style="text-align:left;padding:8px;border:1px solid #E5E7EB">Fix</th>
    </tr>
    <tr><td style="padding:8px;border:1px solid #E5E7EB"><code>Could not connect to SMTP host</code></td><td style="padding:8px;border:1px solid #E5E7EB">SMTP_HOST galat hai ya port blocked. Try port 587 + TLS</td></tr>
    <tr><td style="padding:8px;border:1px solid #E5E7EB"><code>Could not authenticate</code></td><td style="padding:8px;border:1px solid #E5E7EB">Email/password galat hai. cPanel mein check karo</td></tr>
    <tr><td style="padding:8px;border:1px solid #E5E7EB"><code>SSL operation failed</code></td><td style="padding:8px;border:1px solid #E5E7EB">Port 465+ssl se 587+tls try karo</td></tr>
    <tr><td style="padding:8px;border:1px solid #E5E7EB"><code>Connection timed out</code></td><td style="padding:8px;border:1px solid #E5E7EB">WAMP localhost pe port 465/587 block ho sakta hai. Gmail SMTP try karo (neeche)</td></tr>
  </table>
</div>

<div class="warn">
  ⚠️ <strong>DELETE karo</strong> ye file production pe — <code>test_mail.php</code> publicly accessible nahi hona chahiye.
</div>

</body>
</html>