import "server-only"; import nodemailer from "nodemailer"; import { env } from "@/config/env"; import { billingReportMessage } from "./billing-report-message"; const transporter = nodemailer.createTransport({ host: env.SMTP_HOST, port: env.SMTP_PORT, secure: env.SMTP_PORT === 465, auth: { user: env.SMTP_USER, pass: env.SMTP_PASSWORD, }, }); function buildHtml(title: string, content: string, button?: { text: string, url: string }) { return `
${title}

${content}

${button ? `
${button.text}
` : ''}
`; } export const emailService = { async sendBillingReport(toEmail: string, reportMonth: string, pdfBytes: Buffer, invoiceNumber: string) { const filename = "Nutzungsnachweis-" + invoiceNumber.replace(/[^A-Za-z0-9_-]/g, "_") + ".pdf"; const message = billingReportMessage(reportMonth); await transporter.sendMail({ from: env.SMTP_FROM, to: toEmail, subject: message.subject, text: message.text, html: buildHtml("Ihr Nutzungsnachweis", message.htmlContent), attachments: [ { filename, content: pdfBytes, contentType: "application/pdf", }, ], }); }, async sendNewAlbumNotification(toEmail: string, albumNames: string | string[]) { const albums = Array.isArray(albumNames) ? albumNames : [albumNames]; if (albums.length === 0) return; const isMultiple = albums.length > 1; const subject = isMultiple ? `Neue Fotoalben verfügbar (${albums.length})` : `Neues Fotoalbum verfügbar: ${albums[0]}`; const textList = albums.map(a => `- ${a}`).join("\n"); const htmlList = albums.map(a => `
  • ${a}
  • `).join(""); const textBody = isMultiple ? `Hallo,\n\nes wurden ${albums.length} neue Fotoalben für Ihre Redaktion freigegeben:\n\n${textList}\n\nSie können die Bilder ab sofort im Redaktionsportal ansehen und lizenzieren.\n\nViele Grüße,\nIhr Redaktions-Team` : `Hallo,\n\nein neues Fotoalbum "${albums[0]}" wurde für Ihre Redaktion freigegeben. Sie können die Bilder ab sofort im Redaktionsportal ansehen und lizenzieren.\n\nViele Grüße,\nIhr Redaktions-Team`; const htmlBody = isMultiple ? `Hallo,\n\nes wurden soeben **${albums.length} neue Fotoalben** für Ihre Redaktion freigegeben:\n\n\n\nSie können die Bilder ab sofort im Redaktionsportal ansehen und herunterladen.\n\nViele Grüße,\nIhr Redaktions-Team` : `Hallo,\n\nein neues Fotoalbum **"${albums[0]}"** wurde soeben für Ihre Redaktion freigegeben.\n\nSie können die Bilder ab sofort im Redaktionsportal ansehen und herunterladen.\n\nViele Grüße,\nIhr Redaktions-Team`; await transporter.sendMail({ from: env.SMTP_FROM, to: toEmail, subject, text: textBody, html: buildHtml(isMultiple ? "Neue Alben verfügbar" : "Neues Album verfügbar", htmlBody, { text: "Zum Portal", url: `${env.BETTER_AUTH_URL}/login` }), }); }, };