MBO-Tech-IT-Webseite/modules/01-email-system/files/app/api/admin/smtp-test/route.ts

66 lines
2.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextResponse } from "next/server";
import nodemailer from "nodemailer";
import { requireAdmin } from "@/lib/admin-auth";
export const dynamic = "force-dynamic";
// Schneller Diagnose-Endpunkt nur im Admin nutzbar, nicht öffentlich verlinkt
// Aufruf: GET /api/admin/smtp-test
export async function GET() {
const check = await requireAdmin();
if (check instanceof NextResponse) return check;
const config = {
host: process.env.SMTP_HOST ?? "(nicht gesetzt)",
port: Number(process.env.SMTP_PORT ?? 587),
user: process.env.SMTP_USER ?? "(nicht gesetzt)",
from: process.env.SMTP_FROM ?? "(nicht gesetzt)",
to: process.env.SMTP_TO ?? "(nicht gesetzt)",
};
const transporter = nodemailer.createTransport({
host: config.host,
port: config.port,
secure: false,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
connectionTimeout: 10000,
greetingTimeout: 8000,
socketTimeout: 12000,
tls: { rejectUnauthorized: false },
});
try {
await transporter.verify();
// Test-Mail senden
await transporter.sendMail({
from: `"Mietpark Hahn TEST" <${process.env.SMTP_FROM}>`,
to: process.env.SMTP_TO,
subject: `✓ SMTP-Test erfolgreich ${new Date().toLocaleString("de-DE")}`,
text: "Diese Test-Mail wurde automatisch von /api/admin/smtp-test gesendet.\n\nSMTP-Verbindung und Mail-Versand funktionieren korrekt.",
html: `<p style="font-family:sans-serif">Diese Test-Mail wurde automatisch von <code>/api/admin/smtp-test</code> gesendet.</p><p style="color:green;font-weight:bold">✓ SMTP-Verbindung und Mail-Versand funktionieren korrekt.</p>`,
});
return NextResponse.json({
ok: true,
message: "SMTP-Verbindung erfolgreich Test-Mail gesendet",
config: { ...config, pass: "***" },
});
} catch (err) {
const e = err as Error & { code?: string; command?: string };
return NextResponse.json(
{
ok: false,
error: e.message,
code: e.code,
command: e.command,
config: { ...config, pass: "***" },
},
{ status: 500 }
);
}
}