import { encodeBase64 } from "@std/encoding/base64"; import { getLogger } from "./logger.ts"; export class Mail { message_id: string | null = null; aws_message_id: string | null = null; constructor(public raw: string) {} encodeUtf8(): Uint8Array { const utf8Encoder = new TextEncoder(); // TODO: A problem here is if mail is VERY long, this will block for a long time. // Maybe some task queue can be used. return utf8Encoder.encode(this.raw); } getRawBase64(): string { return encodeBase64(this.raw); } appendHeaders( rawMail: string, headers: [key: string, value: string][], ): string { const separatorMatch = rawMail.match(/(\r\n|\n)(\r\n|\n)/); if (separatorMatch == null) { throw new Error( "No header/body separator (2 successive EOLs) found. Cannot append headers.", ); } if (separatorMatch[1] !== separatorMatch[2]) { getLogger().warn("Different EOLs (\\r\\n and \\n) found in mail!"); } const headerStr = headers.map(([k, v]) => `${k}: ${v}${separatorMatch[1]}`) .join(""); const endOfHeadersIndex = separatorMatch.index! + separatorMatch[1].length; return rawMail.slice(0, endOfHeadersIndex) + headerStr + rawMail.slice(endOfHeadersIndex); } }