Files
2026-11-24 05:49:49 +00:00

150 lines
4.1 KiB
TypeScript

import { z } from "zod";
import {
domainOf,
formatMailbox,
parseMailbox,
parseMailboxList,
type Mailbox,
} from "./address";
const addressInput = z.union([z.string().min(3), z.array(z.string().min(3)).min(1).max(50)]);
const attachmentInput = 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 sendSchema = z
.object({
from: z.string().min(3),
to: addressInput,
cc: addressInput.optional(),
bcc: addressInput.optional(),
reply_to: addressInput.optional(),
subject: z.string().min(1).max(998),
html: z.string().max(2_000_000).optional(),
text: z.string().max(2_000_000).optional(),
headers: z.record(z.string(), z.string()).optional(),
attachments: z.array(attachmentInput).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(),
})
.refine((value) => Boolean(value.html || value.text), {
message: "html oder text ist erforderlich",
path: ["html"],
});
export const batchSchema = z.array(sendSchema).min(1).max(100);
export type SendInput = z.infer<typeof sendSchema>;
export type NormalizedMail = {
from: Mailbox;
to: Mailbox[];
cc: Mailbox[];
bcc: Mailbox[];
replyTo: Mailbox[];
subject: string;
html?: string;
text?: string;
headers: Record<string, string>;
attachments: { filename: string; content: Buffer; contentType?: string; contentId?: string }[];
tags: { name: string; value: string }[];
};
const RESERVED_HEADERS = new Set([
"from",
"to",
"cc",
"bcc",
"reply-to",
"subject",
"date",
"message-id",
"mime-version",
"content-type",
"content-transfer-encoding",
"return-path",
]);
export class MailValidationError extends Error {
constructor(message: string) {
super(message);
this.name = "MailValidationError";
}
}
export function normalizeMail(input: SendInput, fromDomain: string): NormalizedMail {
const from = parseMailbox(input.from);
if (!from) throw new MailValidationError("Ungültige from-Adresse");
const domain = domainOf(from.address);
if (domain !== fromDomain && !domain.endsWith(`.${fromDomain}`)) {
throw new MailValidationError(`Absenderdomain ${domain} ist nicht erlaubt`);
}
const to = parseMailboxList(input.to);
const cc = parseMailboxList(input.cc);
const bcc = parseMailboxList(input.bcc);
if (to.length + cc.length + bcc.length === 0) {
throw new MailValidationError("Mindestens ein Empfänger ist erforderlich");
}
const headers: Record<string, string> = {};
for (const [name, value] of Object.entries(input.headers ?? {})) {
const lower = name.toLowerCase();
if (RESERVED_HEADERS.has(lower)) continue;
if (!/^[A-Za-z0-9-]+$/.test(name)) continue;
if (/[\r\n]/.test(value)) throw new MailValidationError(`Ungültiger Header: ${name}`);
headers[name] = value;
}
const attachments = (input.attachments ?? []).map((item) => {
const content = Buffer.from(item.content, "base64");
if (content.length === 0) throw new MailValidationError("Leerer Anhang");
return {
filename: item.filename,
content,
contentType: item.content_type,
contentId: item.content_id,
};
});
const total = attachments.reduce((sum, item) => sum + item.content.length, 0);
if (total > 10 * 1024 * 1024) {
throw new MailValidationError("Anhänge sind zusammen größer als 10 MB");
}
return {
from,
to,
cc,
bcc,
replyTo: parseMailboxList(input.reply_to),
subject: input.subject.replace(/[\r\n]+/g, " "),
html: input.html,
text: input.text,
headers,
attachments,
tags: input.tags ?? [],
};
}
export function mailPreview(mail: NormalizedMail) {
return {
from: formatMailbox(mail.from),
to: mail.to.map(formatMailbox),
cc: mail.cc.map(formatMailbox),
bcc: mail.bcc.map(formatMailbox),
reply_to: mail.replyTo.map(formatMailbox),
subject: mail.subject,
html: mail.html,
text: mail.text,
headers: mail.headers,
tags: mail.tags,
};
}