// `weft test tests/email.test.weft`: messages, MIME, the providers' payloads and templates. packages/integration/email sends // through both providers' APIs against a scripted server, and checks the MIME with Python's parser. import { assert, assertEq, assertPanics, mount } from "std/testing" import { stringify } from "std/json" import { allowlist, clearOutbox, isValidEmail, memory, outbox, send, validate } from "../email" import type { EmailError, Message } from "../email" import { encodeHeader, formatAddress, quotedPrintable, toMime } from "../mime" import * as postmark from "../postmark" import * as resend from "../resend" import { render } from "std/app" import { document } from "../template" import Broken from "./Broken" import Escapes from "./Escapes" import Everything from "./Everything" import Mailto from "./Mailto" import Unsafe from "./Unsafe" fn message(): Message { return { from: "info@example.com", to: ["ana@example.com"], subject: "Hello", text: "Hi" } } fn edit(change: (mut m: Message) => void): Message { let mut m = message() change(&m) return m } fn problem(m: Message): string { let _, err = try validate(m) return match err { InvalidMessage { field } => field, null => "valid", _ => "other", } } // ---- validation ---- test "a good message is valid" { assertEq(problem(message()), "valid") assertEq( problem( edit((mut m) => { m.to = [{ email: "ana@example.com", name: "Ana Novak" }] m.html = "
Hi
" }), ), "valid", ) } test "addresses" { for good of ["a@b.co", "first.last+tag@sub.example.com", "čebela@example.si"] { assert(isValidEmail(good), good) } for bad of [ "", "a", "a@", "@b.co", "a@@b.co", "a b@c.co", "a@b..co", "a@.b.co", "a@b.co.", "a { m.to = [] }), ), "to", ) assertEq( problem( edit((mut m) => { m.subject = " " }), ), "subject", ) assertEq( problem( edit((mut m) => { m.text = "" }), ), "html", ) assertEq( problem( edit((mut m) => { m.from = "nobody" }), ), "from", ) assertEq( problem( edit((mut m) => { m.to = ["nobody"] }), ), "to", ) assertEq( problem( edit((mut m) => { m.cc = ["nobody"] }), ), "cc", ) assertEq( problem( edit((mut m) => { m.bcc = ["nobody"] }), ), "bcc", ) assertEq( problem( edit((mut m) => { m.replyTo = "nobody" }), ), "replyTo", ) } test "header injection is refused" { assertEq( problem( edit((mut m) => { m.subject = "Hi\r\nBcc: victim@example.com" }), ), "subject", ) assertEq( problem( edit((mut m) => { m.subject = "Hi\nBcc: victim@example.com" }), ), "subject", ) assertEq( problem( edit((mut m) => { m.from = { email: "a@b.co", name: "A\r\nBcc: v@x.y" } }), ), "from", ) assertEq( problem( edit((mut m) => { m.to = [{ email: "a@b.co", name: "A\nX: y" }] }), ), "to", ) assertEq( problem( edit((mut m) => { m.headers = [["X-Tag", "a\r\nBcc: v@x.y"]] }), ), "headers", ) assertEq( problem( edit((mut m) => { m.headers = [["Bad Name", "a"]] }), ), "headers", ) assertEq( problem( edit((mut m) => { m.headers = [["X:Y", "a"]] }), ), "headers", ) assertEq( problem( edit((mut m) => { m.attachments = [{ filename: "a\r\nb.txt", content: Bytes.of([1]) }] }), ), "attachments", ) } // ---- sending ---- test "send validates, then hands the message to the transport" { clearOutbox() let sent = try await send(memory(), message()) assertEq(sent.provider, "memory") assertEq(outbox().length, 1) let _, err = try await send( memory(), edit((mut m) => { m.to = [] }), ) assert(err != null) assertEq(outbox().length, 1) } test "an allowlist keeps other recipients from being sent to" { clearOutbox() let staging = allowlist(memory(), ["info@vidni.si"]) let skipped = try await send(staging, message()) assert(skipped.skipped) assertEq(outbox().length, 0) let sent = try await send( staging, edit((mut m) => { m.to = ["INFO@vidni.si "] }), ) else null assert(sent == null, "an address with a space is invalid") let allowed = try await send( staging, edit((mut m) => { m.to = ["INFO@vidni.si"] }), ) assert(!allowed.skipped) assertEq(outbox().length, 1) // One recipient outside the list keeps the whole message back. let mixed = try await send( staging, edit((mut m) => { m.to = ["info@vidni.si"] m.cc = ["ana@example.com"] }), ) assert(mixed.skipped) let open = try await send(allowlist(memory(), ["*"]), message()) assert(!open.skipped) } // ---- MIME ---- test "headers stay ASCII" { assertEq(encodeHeader("Hello"), "Hello") assertEq(encodeHeader("Račun št. 5"), "=?UTF-8?B?UmHEjXVuIMWhdC4gNQ==?=") // A long subject is split between characters, each encoded word at most 75 characters long. for word of encodeHeader("č".repeat(100)).split("\r\n ") { assert(word.length <= 75, word) } assertEq(formatAddress("a@b.co"), "a@b.co") assertEq( formatAddress({ email: "a@b.co", name: "Ana \"N\" Novak" }), "\"Ana \\\"N\\\" Novak\" ", ) assertEq(formatAddress({ email: "a@b.co", name: "Žiga" }), "=?UTF-8?B?xb1pZ2E=?= ") } test "quoted-printable" { assertEq(quotedPrintable("a=b"), "a=3Db") assertEq(quotedPrintable("č"), "=C4=8D") assertEq(quotedPrintable("line one\nline two"), "line one\r\nline two") assertEq(quotedPrintable("trailing space "), "trailing space=20") for line of quotedPrintable("x".repeat(500)).split("\r\n") { assert(line.length <= 76, line) } // A multi-byte character isn't split across a soft line break. for line of quotedPrintable("č".repeat(100)).split("\r\n") { assert(line.length <= 76 && !line.replace("=", "").endsWith("C4="), line) } } test "a text message" { let mime = toMime(message(), { id: "fixed", date: "Tue, 17 Nov 2026 10:00:00 +0000" }) assertEq( mime, "From: info@example.com\r\nTo: ana@example.com\r\nSubject: Hello\r\nDate: Tue, 17 Nov 2026 10:00:00 +0000\r\nMessage-ID:Hi
a\nb
" }), { id: "x" }, ) assert(!mime.includes("secret@example.com")) assert(!mime.replaceAll("\r\n", "").includes("\n")) } test "a file name outside ASCII" { let mime = toMime( edit((mut m) => { m.attachments = [{ filename: "račun.pdf", content: Bytes.of([1]) }] }), { id: "x" }, ) assert(mime.includes("filename*=UTF-8''ra%C4%8Dun.pdf")) } // ---- the providers ---- fn full(): Message { return { from: { email: "info@example.com", name: "Vidni" }, to: ["ana@example.com", { email: "bor@example.com", name: "Bor" }], cc: ["c@example.com"], bcc: ["d@example.com"], replyTo: "owner@example.com", subject: "Račun", html: "Hi
", text: "Hi", headers: [["List-Unsubscribe", "Hi
\",\"text\":\"Hi\",\"headers\":{\"List-Unsubscribe\":\"Hi
\",\"TextBody\":\"Hi\",\"Headers\":[{\"Name\":\"List-Unsubscribe\",\"Value\":\"