<?php
// ========== PHPMailer einbinden ==========
require __DIR__.'/phpmailer/src/Exception.php';
require __DIR__.'/phpmailer/src/PHPMailer.php';
require __DIR__.'/phpmailer/src/SMTP.php';

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

// ========== Testmail versenden ==========
$mail = new PHPMailer(true);

try {
    // Debug ins Errorlog
    $mail->SMTPDebug   = SMTP::DEBUG_SERVER;
    $mail->Debugoutput = function($msg, $level){ error_log("PHPMailer[$level]: $msg"); };

    // SMTP Einstellungen
    $mail->isSMTP();
    $mail->Host       = 'mail.f-invent.de';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'noreply@f-invent.de';
    $mail->Password   = 'b*Y!@Ys.aKS^';  // <- anpassen!
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;


    // Absender & Empfänger
    $mail->setFrom('noreply@f-invent.de', 'Testsystem');
    $mail->addAddress('nils.bonk@googlemail.com'); // <- ändern

    // Inhalt
    $mail->isHTML(true);
    $mail->Subject = '2 Testmail PHPMailer funktioniert 🔥';
    $mail->Body = '
        <!doctype html>
        <html lang="de">
        <head><meta charset="utf-8"></head>
        <body style="background:#f3f4f6;font-family:Arial,sans-serif;padding:20px;">
            <div style="max-width:600px;margin:0 auto;background:#fff;border-radius:10px;padding:20px;
                        box-shadow:0 4px 10px rgba(0,0,0,0.08);">
                <h2 style="margin-top:0;color:#2563eb;">PHPMailer Test</h2>
                <p>Hallo! 👋</p>
                <p>Dies ist eine einfache <strong>HTML-Testmail</strong>, um zu prüfen,
                ob der Versand über deinen Server funktioniert.</p>
                <p style="color:#64748b;font-size:14px;">Zeit: '.date('d.m.Y H:i:s').'</p>
            </div>
        </body>
        </html>
    ';

    $mail->AltBody = 'Testmail: PHPMailer HTML-Inhalt.';

    $mail->send();

    echo "<h2 style='font-family:Arial'>Testmail wurde erfolgreich gesendet ✔️</h2>";

} catch (Throwable $e) {
    echo "<h2 style='color:red;font-family:Arial'>Fehler beim Senden ❌</h2>";
    echo "<pre>".htmlspecialchars($e->getMessage())."</pre>";
}
