186 lines
8.2 KiB
TypeScript
186 lines
8.2 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { MailValidationError, normalizeMail, type NormalizedMail } from "./schema";
|
|
|
|
const httpUrl = z
|
|
.string()
|
|
.url()
|
|
.max(2000)
|
|
.refine((value) => /^https?:\/\//i.test(value), "Nur http(s)-Links sind erlaubt");
|
|
|
|
const mailbox = z.union([z.string().min(3), z.array(z.string().min(3)).min(1).max(50)]);
|
|
const attachment = z.object({
|
|
filename: z.string().min(1).max(255),
|
|
content: z.string().min(1),
|
|
content_type: z.string().min(3).max(200).optional(),
|
|
content_id: z.string().min(1).max(200).optional(),
|
|
});
|
|
|
|
export const sportTemplateSchema = z.object({
|
|
template: z.literal("sport-v1"),
|
|
brand: z.object({
|
|
name: z.string().min(1).max(80),
|
|
accent: z.string().regex(/^#[0-9a-fA-F]{6}$/).optional(),
|
|
logo_cid: z.string().regex(/^[A-Za-z0-9._-]{1,100}$/).optional(),
|
|
}),
|
|
content: z.object({
|
|
title: z.string().min(1).max(200).optional(),
|
|
preheader: z.string().max(200).optional(),
|
|
paragraphs: z.array(z.string().max(4000)).max(20).optional(),
|
|
facts: z
|
|
.array(z.object({ label: z.string().min(1).max(40), value: z.string().min(1).max(300) }))
|
|
.max(20)
|
|
.optional(),
|
|
button: z.object({ label: z.string().min(1).max(80), url: httpUrl }).optional(),
|
|
note: z.string().max(1200).optional(),
|
|
footer: z.string().max(300).optional(),
|
|
}),
|
|
});
|
|
|
|
export const templateSendSchema = z.object({
|
|
from: z.string().min(3),
|
|
to: mailbox,
|
|
cc: mailbox.optional(),
|
|
bcc: mailbox.optional(),
|
|
reply_to: mailbox.optional(),
|
|
subject: z.string().min(1).max(998),
|
|
template: z.literal("sport-v1"),
|
|
brand: sportTemplateSchema.shape.brand,
|
|
content: sportTemplateSchema.shape.content,
|
|
attachments: z.array(attachment).max(20).optional(),
|
|
tags: z
|
|
.array(z.object({ name: z.string().min(1).max(256), value: z.string().min(1).max(256) }))
|
|
.max(50)
|
|
.optional(),
|
|
});
|
|
|
|
export type TemplateSendInput = z.infer<typeof templateSendSchema>;
|
|
|
|
const INK = "#18181b";
|
|
const MUTED = "#52525b";
|
|
const SUBTLE = "#71717a";
|
|
const BORDER = "#e4e4e7";
|
|
const SURFACE = "#fafafa";
|
|
const ACTION = "#09090b";
|
|
const LINK = "#6d28d9";
|
|
const FONT = "-apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif";
|
|
|
|
function e(value: string): string {
|
|
return value
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """)
|
|
.replace(/'/g, "'");
|
|
}
|
|
|
|
function linkify(value: string): string {
|
|
return e(value).replace(
|
|
/https?:\/\/[^\s<]+/g,
|
|
(url) =>
|
|
`<a href="${url}" style="color:${LINK};text-decoration:underline;word-break:break-all">${url}</a>`,
|
|
);
|
|
}
|
|
|
|
function button(url: string, label: string): string {
|
|
const u = e(url);
|
|
const t = e(label);
|
|
return `<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="margin:8px 0 4px">
|
|
<tr><td style="border-radius:8px;background:${ACTION}">
|
|
<!--[if mso]><v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" href="${u}" style="height:44px;v-text-anchor:middle;width:280px" arcsize="18%" stroke="f" fillcolor="${ACTION}"><center style="color:#ffffff;font-family:Arial,sans-serif;font-size:15px;font-weight:bold">${t}</center></v:roundrect><![endif]-->
|
|
<!--[if !mso]><!--><a href="${u}" style="display:inline-block;padding:12px 24px;font-family:${FONT};font-size:15px;font-weight:600;line-height:20px;color:#ffffff;text-decoration:none;border-radius:8px">${t}</a><!--<![endif]-->
|
|
</td></tr></table>
|
|
<p style="margin:0 0 16px;font-size:12px;line-height:16px;color:${SUBTLE};word-break:break-all">${u}</p>`;
|
|
}
|
|
|
|
function factsTable(facts: { label: string; value: string }[]): string {
|
|
return `<table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="margin:0 0 16px;border-collapse:collapse;border-bottom:1px solid ${BORDER}">
|
|
${facts
|
|
.map(
|
|
(fact) =>
|
|
`<tr><td style="padding:8px 16px 8px 0;border-top:1px solid ${BORDER};color:${MUTED};font-size:14px;line-height:20px;vertical-align:top;white-space:nowrap">${e(fact.label)}</td><td style="padding:8px 0;border-top:1px solid ${BORDER};color:${INK};font-size:14px;line-height:20px;vertical-align:top">${linkify(fact.value)}</td></tr>`,
|
|
)
|
|
.join("")}
|
|
</table>`;
|
|
}
|
|
|
|
function paragraphs(items: string[]): string {
|
|
return items
|
|
.map(
|
|
(item) =>
|
|
`<p style="margin:0 0 16px;font-size:15px;line-height:24px;color:${INK}">${linkify(item).replace(/\n/g, "<br>")}</p>`,
|
|
)
|
|
.join("");
|
|
}
|
|
|
|
export function renderSportTemplate(input: TemplateSendInput): { html: string; text: string } {
|
|
const brand = input.brand;
|
|
const content = input.content;
|
|
const accent = brand.accent ?? "#7c3aed";
|
|
const title = content.title ?? input.subject;
|
|
const preheader = content.preheader ?? content.paragraphs?.[0]?.slice(0, 140) ?? title;
|
|
const logo = brand.logo_cid
|
|
? `<img src="cid:${brand.logo_cid}" width="40" height="40" alt="" style="display:block;border:0;width:40px;height:40px;object-fit:contain">`
|
|
: "";
|
|
const brandBlock = `<table role="presentation" cellpadding="0" cellspacing="0" border="0"><tr>${logo ? `<td style="padding-right:12px;vertical-align:middle">${logo}</td>` : ""}<td style="vertical-align:middle;font-size:15px;line-height:20px;font-weight:600;color:${INK}">${e(brand.name)}</td></tr></table>`;
|
|
const footer = content.footer ?? `${brand.name} · Diese Mail wurde automatisch versendet.`;
|
|
|
|
const html = `<!doctype html>
|
|
<html lang="de" xmlns:v="urn:schemas-microsoft-com:vml">
|
|
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="color-scheme" content="light"><meta name="supported-color-schemes" content="light"><title>${e(title)}</title></head>
|
|
<body style="margin:0;padding:0;background:${SURFACE};font-family:${FONT};color:${INK}">
|
|
<div style="display:none;max-height:0;overflow:hidden;opacity:0">${e(preheader)}</div>
|
|
<table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="background:${SURFACE}"><tr><td align="center" style="padding:24px 12px">
|
|
<table role="presentation" cellpadding="0" cellspacing="0" border="0" width="600" style="width:100%;max-width:600px;background:#ffffff;border:1px solid ${BORDER};border-radius:16px;border-collapse:separate;overflow:hidden">
|
|
<tr><td style="padding:24px 32px 16px;border-bottom:2px solid ${accent}">${brandBlock}</td></tr>
|
|
<tr><td style="padding:32px 32px 8px"><h1 style="margin:0 0 24px;font-size:22px;line-height:28px;font-weight:700;color:${INK}">${e(title)}</h1>
|
|
${content.paragraphs?.length ? paragraphs(content.paragraphs) : ""}
|
|
${content.facts?.length ? factsTable(content.facts) : ""}
|
|
${content.button ? button(content.button.url, content.button.label) : ""}
|
|
${content.note ? `<table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%" style="margin:8px 0 8px;background:#f5f3ff;border-left:3px solid ${accent};border-radius:8px"><tr><td style="padding:12px 16px;font-size:14px;line-height:22px;color:${INK}"><strong>Hinweis:</strong> ${linkify(content.note)}</td></tr></table>` : ""}
|
|
</td></tr>
|
|
<tr><td style="padding:16px 32px 24px;background:${SURFACE};border-top:1px solid ${BORDER};font-size:12px;line-height:16px;color:${SUBTLE}">${e(footer)}</td></tr>
|
|
</table></td></tr></table></body></html>`;
|
|
|
|
const textParts = [
|
|
title,
|
|
...(content.paragraphs ?? []),
|
|
...(content.facts ?? []).map((fact) => `${fact.label}: ${fact.value}`),
|
|
...(content.button ? [`${content.button.label}: ${content.button.url}`] : []),
|
|
...(content.note ? [`Hinweis: ${content.note}`] : []),
|
|
footer,
|
|
];
|
|
|
|
return { html, text: textParts.filter(Boolean).join("\n\n") };
|
|
}
|
|
|
|
export function templateToMail(
|
|
input: TemplateSendInput,
|
|
fromDomain: string,
|
|
): NormalizedMail {
|
|
if (input.brand.logo_cid) {
|
|
const hasLogo = input.attachments?.some((item) => item.content_id === input.brand.logo_cid);
|
|
if (!hasLogo) {
|
|
throw new MailValidationError(
|
|
`Logo-Anhang mit content_id "${input.brand.logo_cid}" fehlt`,
|
|
);
|
|
}
|
|
}
|
|
const rendered = renderSportTemplate(input);
|
|
return normalizeMail(
|
|
{
|
|
from: input.from,
|
|
to: input.to,
|
|
cc: input.cc,
|
|
bcc: input.bcc,
|
|
reply_to: input.reply_to,
|
|
subject: input.subject,
|
|
html: rendered.html,
|
|
text: rendered.text,
|
|
attachments: input.attachments,
|
|
tags: input.tags,
|
|
},
|
|
fromDomain,
|
|
);
|
|
}
|