import "server-only"; import nodemailer from "nodemailer"; import { env } from "@/config/env"; export type MailMessage = { from: string; to: string; subject: string; text: string; }; export type MailTransport = { sendMail(message: MailMessage): Promise; }; export type Mailer = { sendVerification(input: { to: string; verificationUrl: string }): Promise; sendPasswordReset(input: { to: string; resetUrl: string }): Promise; sendApprovalNotice(input: { to: string; editorialName: string; editorialId?: string; }): Promise; }; export function createMailer({ from, transport }: { from: string; transport: MailTransport }): Mailer { async function send(message: Omit): Promise { await transport.sendMail({ from, ...message }); } return { async sendVerification({ to, verificationUrl }) { await send({ to, subject: "Bitte bestätigen Sie Ihre E-Mail-Adresse", text: `Bitte bestätigen Sie Ihre E-Mail-Adresse für das Redaktionsportal:\n\n${verificationUrl}`, }); }, async sendPasswordReset({ to, resetUrl }) { await send({ to, subject: "Passwort für das Redaktionsportal zurücksetzen", text: `Setzen Sie Ihr Passwort für das Redaktionsportal zurück:\n\n${resetUrl}`, }); }, async sendApprovalNotice({ to, editorialName }) { await send({ to, subject: "Ihr Zugang zum Redaktionsportal ist freigegeben", text: `Der Zugang für ${editorialName} wurde freigegeben. Sie können sich jetzt im Redaktionsportal anmelden.`, }); }, }; } const smtpTransport = nodemailer.createTransport({ host: env.SMTP_HOST, port: env.SMTP_PORT, secure: env.SMTP_PORT === 465, auth: { user: env.SMTP_USER, pass: env.SMTP_PASSWORD, }, }); export const mailer = createMailer({ from: env.SMTP_FROM, transport: smtpTransport });