Files
immich-redaktions-plugin/src/modules/email/service.ts
T

127 lines
5.8 KiB
TypeScript

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 `<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="color-scheme" content="light dark">
<meta name="supported-color-schemes" content="light dark">
<style>
:root {
color-scheme: light dark;
}
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background-color: #f4f4f5; color: #18181b; line-height: 1.6; padding: 20px; margin: 0; -webkit-font-smoothing: antialiased; }
.container { max-width: 600px; margin: 0 auto; background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.header { padding: 32px 32px 0 32px; text-align: center; }
.header img { max-height: 64px; max-width: 100%; height: auto; display: block; margin: 0 auto; }
.dark-logo { display: none; }
.content { padding: 32px 20px; font-size: 16px; }
.title { font-size: 24px; font-weight: 700; margin-bottom: 24px; text-align: center; }
.button { display: inline-block; background-color: #18181b; color: #ffffff !important; padding: 14px 28px; text-decoration: none; border-radius: 8px; font-weight: 600; margin-top: 32px; }
.footer { padding: 32px 20px; text-align: center; font-size: 14px; color: #71717a; border-top: 1px solid #e4e4e7; background: #fafafa; }
.footer a { color: #71717a; text-decoration: underline; margin: 0 8px; display: inline-block; }
@media (prefers-color-scheme: dark) {
body { background-color: #09090b !important; color: #f4f4f5 !important; }
.container { background-color: #18181b !important; border: 1px solid #27272a; box-shadow: none !important; }
.light-logo { display: none !important; }
.dark-logo { display: block !important; }
.button { background-color: #fafafa !important; color: #18181b !important; }
.footer { background-color: #09090b !important; border-top-color: #27272a !important; color: #a1a1aa !important; }
.footer a { color: #a1a1aa !important; }
p { color: #d4d4d8 !important; }
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<img src="${env.BETTER_AUTH_URL}/brand/logo-black.png" alt="Logo" class="light-logo" />
<!--[if !mso]><!---->
<img src="${env.BETTER_AUTH_URL}/brand/logo-white.png" alt="Logo" class="dark-logo" />
<!--<![endif]-->
</div>
<div class="content">
<div class="title">${title}</div>
<p style="white-space: pre-line; text-align: center; margin: 0;">${content}</p>
${button ? `<div style="text-align: center;"><a href="${button.url}" class="button">${button.text}</a></div>` : ''}
</div>
<div class="footer">
<p style="margin: 0;">
<a href="${env.LEGAL_IMPRINT_URL}">Impressum</a> |
<a href="${env.LEGAL_PRIVACY_URL}">Datenschutz</a> |
<a href="${env.LEGAL_TERMS_URL}">AGB</a>
</p>
</div>
</div>
</body>
</html>`;
}
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 => `<li><strong>${a}</strong></li>`).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<ul>${htmlList}</ul>\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` }),
});
},
};