Email for professionals and teams
Reply to the Reply-To address instead of From (#122)
Daniel Quah07c4d5662488
diff --git a/drizzle/migrations/0061_add_message_reply_to.sql b/drizzle/migrations/0061_add_message_reply_to.sqlnew file mode 100644index 0000000..fdc6394--- /dev/null+++ b/drizzle/migrations/0061_add_message_reply_to.sql@@ -0,0 +1 @@+ALTER TABLE `messages` ADD `reply_to` text;diff --git a/drizzle/migrations/meta/_journal.json b/drizzle/migrations/meta/_journal.jsonindex 809aecb..a227b8e 100644--- a/drizzle/migrations/meta/_journal.json+++ b/drizzle/migrations/meta/_journal.json@@ -372,6 +372,13 @@"when": 1788850801023,"tag": "0060_add_license_seat_limit","breakpoints": true+ },+ {+ "idx": 53,+ "version": "6",+ "when": 1788850802023,+ "tag": "0061_add_message_reply_to",+ "breakpoints": true}]}diff --git a/src/components/message-actions/message-actions.tsx b/src/components/message-actions/message-actions.tsxindex 66ad508..1276327 100644--- a/src/components/message-actions/message-actions.tsx+++ b/src/components/message-actions/message-actions.tsx@@ -105,6 +105,7 @@ export function MessageActions({fromAddr: senderAddress,toAddr: "",ccAddr: null,+ replyTo: null,providerMessageId: null,references: null,threadId: null,diff --git a/src/components/message-actions/types.d.ts b/src/components/message-actions/types.d.tsindex ef9792f..d577c38 100644--- a/src/components/message-actions/types.d.ts+++ b/src/components/message-actions/types.d.ts@@ -43,7 +43,7 @@ export type ReplyMode = "reply" | "replyAll";export type ReplyableMessage = Pick<Message,- "direction" | "fromAddr" | "toAddr" | "ccAddr" | "providerMessageId" | "references" | "threadId"+ "direction" | "fromAddr" | "toAddr" | "ccAddr" | "replyTo" | "providerMessageId" | "references" | "threadId">;export type ReplyRecipients = {diff --git a/src/components/message-actions/utils.ts b/src/components/message-actions/utils.tsindex 19e3325..0b7b878 100644--- a/src/components/message-actions/utils.ts+++ b/src/components/message-actions/utils.ts@@ -167,7 +167,8 @@ export function buildReplyQuoteHtml(}/**- * Who a reply goes to. A plain reply answers the sender; reply-all also keeps+ * Who a reply goes to. A plain reply answers the sender, or the Reply-To+ * addresses when the message names any (RFC 5322 §3.6.2); reply-all also keeps* everyone else on the To and Cc lines, minus the mailbox's own addresses so* the author is not mailing themselves. Replying to a sent message re-addresses* its original recipients.@@ -193,7 +194,8 @@ export function getReplyRecipients(return { to, cc };}- const to = unique([message.fromAddr]);+ const replyTo = unique(splitEmailAddressList(message.replyTo));+ const to = replyTo.length ? replyTo : unique([message.fromAddr]);if (mode !== "replyAll") return { to, cc: [] };const cc = unique([...splitEmailAddressList(message.toAddr), ...splitEmailAddressList(message.ccAddr)]);return { to, cc };diff --git a/src/db/schema/index.ts b/src/db/schema/index.tsindex 5a8572d..3a7faf5 100644--- a/src/db/schema/index.ts+++ b/src/db/schema/index.ts@@ -241,6 +241,8 @@ export const messages = sqliteTable(toAddr: text("to_addr").notNull(),ccAddr: text("cc_addr"),bccAddr: text("bcc_addr"),+ /** Full Reply-To header, comma-joined. Replies go here instead of `fromAddr` when set. */+ replyTo: text("reply_to"),subject: text("subject"),snippet: text("snippet"),textBody: text("text_body"),diff --git a/src/hooks/types.d.ts b/src/hooks/types.d.tsindex e29e681..97b78f6 100644--- a/src/hooks/types.d.ts+++ b/src/hooks/types.d.ts@@ -16,6 +16,8 @@ export type Message = {toAddr: string;ccAddr?: string | null;bccAddr?: string | null;+ /** Comma-separated Reply-To list; replies go here instead of `fromAddr` when set. */+ replyTo?: string | null;fromContactName?: string | null;fromContactHasAvatar?: boolean;toContactName?: string | null;diff --git a/src/lib/agent/reply.ts b/src/lib/agent/reply.tsindex 965f4b3..cdc7a59 100644--- a/src/lib/agent/reply.ts+++ b/src/lib/agent/reply.ts@@ -7,8 +7,9 @@ import type { AgentToolContext } from "./types";import type { messages } from "@/db/schema";export async function getReplyRecipients(context: AgentToolContext, source: typeof messages.$inferSelect, replyAll: boolean) {- let recipient = normalizeEmailAddress(source.fromAddr);- if (source.rawR2Key) {+ let recipient = getEmailAddressList(source.replyTo).at(0) ?? normalizeEmailAddress(source.fromAddr);+ // Messages stored before the reply_to column existed only carry it in the raw MIME.+ if (!source.replyTo && source.rawR2Key) {const object = await context.env.BUCKET.get(source.rawR2Key);if (object) {const parsed = await PostalMime.parse(await object.arrayBuffer());diff --git a/src/lib/email/inbound.ts b/src/lib/email/inbound.tsindex b5384f8..78c4685 100644--- a/src/lib/email/inbound.ts+++ b/src/lib/email/inbound.ts@@ -182,6 +182,7 @@ export async function processInboundMessage(fromAddr,toAddr,ccAddr: parsed.ccAddr,+ replyTo: parsed.replyTo,subject: parsed.subject,snippet,textBody: parsed.text,diff --git a/src/lib/email/parse.ts b/src/lib/email/parse.tsindex ba3a25e..ac7370f 100644--- a/src/lib/email/parse.ts+++ b/src/lib/email/parse.ts@@ -17,6 +17,8 @@ export type ParsedEmail = {ccAddr: string | null;/** Full Bcc header, comma-joined. Only present on mail composed locally, never on delivered mail. */bccAddr: string | null;+ /** Full Reply-To header, comma-joined. */+ replyTo: string | null;inReplyTo: string | null;references: string[];date: Date | null;@@ -35,6 +37,7 @@ export async function parseRawMime(raw: ArrayBuffer): Promise<ParsedEmail> {toAddr: formatPostalAddressList(email.to, null),ccAddr: formatPostalAddressList(email.cc, null),bccAddr: formatPostalAddressList(email.bcc, null),+ replyTo: formatPostalAddressList(email.replyTo, null),inReplyTo: normalizeMessageId(email.inReplyTo),references: parseMessageIdList(email.references),date: date && !Number.isNaN(date.getTime()) ? date : null,diff --git a/src/lib/import/service.ts b/src/lib/import/service.tsindex c4a0f56..5a05342 100644--- a/src/lib/import/service.ts+++ b/src/lib/import/service.ts@@ -81,6 +81,7 @@ async function importMessageToMailbox(fromAddr,toAddr,ccAddr: parsed.ccAddr,+ replyTo: parsed.replyTo,subject: parsed.subject,snippet: buildSnippet(parsed.text, parsed.html),textBody: parsed.text,diff --git a/src/lib/jmap/email-objects.ts b/src/lib/jmap/email-objects.tsindex e367eee..6b54687 100644--- a/src/lib/jmap/email-objects.ts+++ b/src/lib/jmap/email-objects.ts@@ -122,7 +122,7 @@ export function buildEmailObject(row: MessageRow, attachments: AttachmentRow[],to: toAddressObjects(row.toAddr),cc: toAddressObjects(row.ccAddr),bcc: toAddressObjects(row.bccAddr),- replyTo: null,+ replyTo: toAddressObjects(row.replyTo),subject: row.subject,sentAt,hasAttachment: attached.length > 0,diff --git a/src/lib/spam/feedback.ts b/src/lib/spam/feedback.tsindex c139d69..2a79941 100644--- a/src/lib/spam/feedback.ts+++ b/src/lib/spam/feedback.ts@@ -25,7 +25,7 @@ export async function applySpamFeedback(env: CloudflareEnv, user: SessionUser, mconst parsed = {subject: message.subject, text: message.textBody, html: message.htmlBody,messageId: message.providerMessageId, fromAddr: message.fromAddr, toAddr: message.toAddr,- ccAddr: message.ccAddr, bccAddr: message.bccAddr, inReplyTo: message.inReplyTo,+ ccAddr: message.ccAddr, bccAddr: message.bccAddr, replyTo: message.replyTo, inReplyTo: message.inReplyTo,references: message.references?.split(/\s+/).filter(Boolean) ?? [], date: message.createdAt,attachments: [],};diff --git a/tests/reply-recipients.test.mjs b/tests/reply-recipients.test.mjsnew file mode 100644index 0000000..c8fb48b--- /dev/null+++ b/tests/reply-recipients.test.mjs@@ -0,0 +1,73 @@+import assert from "node:assert/strict";+import { mkdtempSync, rmSync } from "node:fs";+import { dirname, join } from "node:path";+import test, { after } from "node:test";+import { fileURLToPath, pathToFileURL } from "node:url";+import { build } from "esbuild";++const root = join(dirname(fileURLToPath(import.meta.url)), "..");+const directory = mkdtempSync(join(root, "node_modules", "mailflare-reply-recipients-"));+after(() => rmSync(directory, { recursive: true, force: true }));+await build({+ stdin: {+ contents: `+ export { getReplyRecipients } from "./src/components/message-actions/utils.ts";+ export { parseRawMime } from "./src/lib/email/parse.ts";+ `,+ resolveDir: root,+ sourcefile: "reply-recipients-entry.ts",+ },+ outfile: join(directory, "entry.mjs"),+ bundle: true,+ platform: "node",+ format: "esm",+ target: "node24",+ tsconfig: join(root, "tsconfig.json"),+ packages: "external",+ logLevel: "silent",+});+const { getReplyRecipients, parseRawMime } = await import(pathToFileURL(join(directory, "entry.mjs")).href);++const own = ["[email protected]"];+const inbound = (fields) => ({+ direction: "inbound",+ fromAddr: "Website <[email protected]>",+ toAddr: "[email protected]",+ ccAddr: null,+ replyTo: null,+ providerMessageId: null,+ references: null,+ threadId: null,+ ...fields,+});++test("a reply answers the sender when there is no Reply-To", () => {+ assert.deepEqual(getReplyRecipients(inbound({}), own, "reply"), { to: ["Website <[email protected]>"], cc: [] });+});++test("a reply goes to every Reply-To address instead of the sender", () => {+ const message = inbound({ replyTo: "Visitor <[email protected]>, [email protected]" });+ assert.deepEqual(getReplyRecipients(message, own, "reply"), { to: ["Visitor <[email protected]>", "[email protected]"], cc: [] });+});++test("reply-all keeps the other recipients but not the From the Reply-To replaced", () => {+ const message = inbound({ replyTo: "[email protected]", toAddr: "[email protected], [email protected]", ccAddr: "[email protected]" });+ assert.deepEqual(getReplyRecipients(message, own, "replyAll"), { to: ["[email protected]"], cc: ["[email protected]", "[email protected]"] });+});++test("a Reply-To naming only the mailbox itself falls back to the sender", () => {+ const message = inbound({ replyTo: "[email protected]" });+ assert.deepEqual(getReplyRecipients(message, own, "reply").to, ["Website <[email protected]>"]);+});++test("the parser keeps the full Reply-To header", async () => {+ const raw = new TextEncoder().encode([+ "From: Website <[email protected]>",+ "To: [email protected]",+ "Reply-To: Visitor <[email protected]>, [email protected]",+ "Subject: Enquiry",+ "",+ "Hi",+ ].join("\r\n")).buffer;+ assert.equal((await parseRawMime(raw)).replyTo, '"Visitor" <[email protected]>, [email protected]');+});