Files

143 lines
5.9 KiB
JavaScript
Raw Permalink Normal View History

import nodemailer from 'nodemailer';
import { Resend } from 'resend';
const FROM = process.env.MAIL_FROM || 'Dockly-Pfandsystem Demo <demo@dockly.de>';
let transporter = null;
if (process.env.SMTP_HOST && process.env.SMTP_USER && process.env.SMTP_PASS) {
transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT || '587'),
secure: (process.env.SMTP_SECURE || 'false') === 'true',
auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS },
});
console.log('[mail] SMTP transport aktiv:', process.env.SMTP_HOST);
}
const resend = process.env.RESEND_API_KEY ? new Resend(process.env.RESEND_API_KEY) : null;
async function sendMail({ to, subject, html }) {
if (transporter) {
const info = await transporter.sendMail({ from: FROM, to, subject, html });
console.log('[mail] SMTP sent:', to, 'msgId:', info.messageId);
return { provider: 'smtp', messageId: info.messageId };
}
if (resend) {
const r = await resend.emails.send({ from: FROM, to, subject, html });
console.log('[mail] Resend sent:', to, 'id:', r.data?.id);
return { provider: 'resend', id: r.data?.id };
}
console.log('[mail-dev] (kein Provider konfiguriert) ->', to, subject);
return { dev: true };
}
const wrap = (inner) => `
<div style="font-family:system-ui,Segoe UI,Helvetica,Arial,sans-serif;max-width:560px;margin:auto;color:#0f172a">
${inner}
<hr style="border:none;border-top:1px solid #e2e8f0;margin:24px 0">
<p style="color:#64748b;font-size:12px">
Dockly-Pfandsystem Demo · <a href="https://pfandsystem.dockly.de" style="color:#1a4148">pfandsystem.dockly.de</a>
</p>
</div>`;
export async function sendDemoInviteMail({ to, name, slug, password, loginUrl, days }) {
return sendMail({
to,
subject: `Dein Dockly-Pfandsystem-Demo-Zugang (${days} Tage)`,
html: wrap(`
<h2 style="color:#051e23">Willkommen im Dockly-Pfandsystem-Demo</h2>
<p>Hallo ${name},</p>
<p>dein <strong>${days}-Tage Demo-Zugang</strong> ist bereit. Du kannst alle Funktionen
in Ruhe testen und eigene Beispieldaten anlegen.</p>
<div style="background:#f1f7f8;border:1px solid #b9d2d7;border-radius:8px;padding:16px;margin:16px 0">
<p style="margin:4px 0"><strong>Login-URL:</strong> <a href="${loginUrl}">${loginUrl}</a></p>
<p style="margin:4px 0"><strong>E-Mail:</strong> ${to}</p>
<p style="margin:4px 0"><strong>Passwort:</strong> <code>${password}</code></p>
<p style="margin:4px 0"><strong>Tenant-Kennung:</strong> ${slug}</p>
</div>
<p>Bitte ändere dein Passwort nach dem ersten Login.</p>
`),
});
}
// 7 Tage vor Ablauf
export async function sendDemoExpiryReminderMail({ to, name, slug, daysLeft, expiresAt }) {
const loginUrl = `https://pfandsystem.dockly.de/login?tenant=${slug}`;
const dateStr = new Date(expiresAt).toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' });
return sendMail({
to,
subject: `Erinnerung: Dein Demo-Zugang läuft in ${daysLeft} Tagen ab`,
html: wrap(`
<h2 style="color:#051e23">Dein Demo-Zugang läuft bald ab</h2>
<p>Hallo ${name},</p>
<p>nur noch <strong>${daysLeft} Tag${daysLeft === 1 ? '' : 'e'}</strong> &mdash; danach wird dein
Demo-Zugang am <strong>${dateStr}</strong> automatisch deaktiviert.</p>
<p>Du willst weiter testen oder das System für deinen Betrieb live nehmen?
Antworte einfach auf diese Mail &mdash; wir verlängern den Zugang oder
besprechen die Live-Schaltung mit dir.</p>
<p style="margin-top:24px">
<a href="${loginUrl}"
style="background:#051e23;color:#fff;padding:10px 18px;border-radius:8px;text-decoration:none;font-weight:600">
Jetzt einloggen
</a>
</p>
`),
});
}
// 1 Tag vor Ablauf (Letzter-Tag-Variante)
export async function sendDemoLastDayMail({ to, name, slug, expiresAt }) {
const loginUrl = `https://pfandsystem.dockly.de/login?tenant=${slug}`;
const dateStr = new Date(expiresAt).toLocaleDateString('de-DE');
return sendMail({
to,
subject: 'Letzter Tag: Dein Demo-Zugang läuft morgen ab',
html: wrap(`
<h2 style="color:#051e23">Letzter Tag für dein Demo</h2>
<p>Hallo ${name},</p>
<p>morgen (<strong>${dateStr}</strong>) endet dein Demo-Zugang. Falls du noch Daten exportieren oder etwas
abschließend testen möchtest, schau heute nochmal rein.</p>
<p>Wir würden uns sehr freuen, wenn wir gemeinsam weitermachen.
Eine kurze Antwort auf diese Mail genügt &mdash; wir melden uns sofort zurück.</p>
<p style="margin-top:24px">
<a href="${loginUrl}"
style="background:#051e23;color:#fff;padding:10px 18px;border-radius:8px;text-decoration:none;font-weight:600">
Jetzt einloggen
</a>
</p>
`),
});
}
// Demo ist abgelaufen
export async function sendDemoExpiredMail({ to, name }) {
return sendMail({
to,
subject: 'Dein Demo-Zugang ist abgelaufen',
html: wrap(`
<h2 style="color:#051e23">Dein Demo ist abgelaufen</h2>
<p>Hallo ${name},</p>
<p>dein Demo-Zugang zum Dockly-Pfandsystem ist heute abgelaufen.
Deine Daten sind noch <strong>14 Tage</strong> bei uns gespeichert &mdash;
falls du jetzt verlängern oder live gehen möchtest, antworte einfach auf
diese Mail. Danach werden die Demo-Daten automatisch gelöscht.</p>
<p>Vielen Dank fürs Testen!</p>
`),
});
}
// SMTP-Selbsttest (zum Debuggen)
export async function sendTestMail(to) {
return sendMail({
to,
subject: 'Dockly-Pfandsystem - SMTP-Test',
html: wrap(`
<h2 style="color:#051e23">SMTP-Test</h2>
<p>Diese Test-Mail bestätigt, dass der Mail-Versand über
<code>${process.env.SMTP_HOST || 'kein SMTP'}</code> funktioniert.</p>
<p>Absender: <code>${FROM}</code></p>
<p>Zeitstempel: ${new Date().toISOString()}</p>
`),
});
}