PUT /api/v1/mailboxes/:id/folders/:id had no validation at all: any caller could rename a system folder (inbox, sent, draft, archive, trash, spam) to anything, rename it to an empty string, or collide its name with another folder and trigger an unhandled SQLite UNIQUE-constraint exception. deleteFolder already protected system folders via is_deletable -- give updateFolder the same guard, reject empty names, and turn the UNIQUE collision into a clean 409 instead of a 500. Also notes that Cloudflare R2/AI/Durable Object infra is now actually provisioned and the Worker is live at https://agentic-inbox.subasiarhan3.workers.dev (fails closed pending the owner-only Access + domain/Email Routing setup). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
506 lines
23 KiB
TypeScript
506 lines
23 KiB
TypeScript
// Copyright (c) 2026 Cloudflare, Inc.
|
|
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
|
|
// https://opensource.org/licenses/Apache-2.0
|
|
|
|
import { getAgentByName } from "agents";
|
|
import { type Context, Hono } from "hono";
|
|
import { cors } from "hono/cors";
|
|
import PostalMime from "postal-mime";
|
|
import { z } from "zod";
|
|
import { sendEmail } from "./email-sender";
|
|
import { storeAttachments, chunkArray, type StoredAttachment } from "./lib/attachments";
|
|
import {
|
|
validateSender,
|
|
SenderValidationError,
|
|
generateMessageId,
|
|
buildThreadingHeaders,
|
|
listMailboxes,
|
|
} from "./lib/email-helpers";
|
|
import { maybeSendAutoReply, maybeForwardEmail, type AutoReplySettings, type ForwardingSettings } from "./lib/auto-respond";
|
|
import { SendEmailRequestSchema } from "./lib/schemas";
|
|
import { handleReplyEmail, handleForwardEmail } from "./routes/reply-forward";
|
|
import { Folders } from "../shared/folders";
|
|
import type { Env } from "./types";
|
|
import { requireMailbox, type MailboxContext } from "./lib/mailbox";
|
|
|
|
type AppContext = Context<MailboxContext>;
|
|
|
|
// -- Request body schemas (kept for validation) ---------------------
|
|
|
|
const CreateMailboxBody = z.object({
|
|
email: z.string().email(),
|
|
name: z.string().min(1),
|
|
settings: z.record(z.any()).optional(), // unvalidated — agentSystemPrompt goes straight to AI
|
|
});
|
|
|
|
const DraftBody = z.object({
|
|
to: z.string().optional(),
|
|
cc: z.string().optional(),
|
|
bcc: z.string().optional(),
|
|
subject: z.string().optional(),
|
|
body: z.string(),
|
|
in_reply_to: z.string().optional(),
|
|
thread_id: z.string().optional(),
|
|
draft_id: z.string().optional(),
|
|
});
|
|
|
|
// -- Helpers --------------------------------------------------------
|
|
|
|
function slugify(text: string) { // can return "" for non-alphanumeric input
|
|
return text.toString().toLowerCase()
|
|
.replace(/\s+/g, "-").replace(/[^\w-]+/g, "")
|
|
.replace(/--+/g, "-").replace(/^-+/, "").replace(/-+$/, "");
|
|
}
|
|
|
|
function intQuery(c: AppContext, key: string): number | undefined {
|
|
const v = c.req.query(key);
|
|
if (!v) return undefined;
|
|
const n = Number(v);
|
|
return Number.isNaN(n) ? undefined : n;
|
|
}
|
|
|
|
function boolQuery(c: AppContext, key: string): boolean | undefined {
|
|
const v = c.req.query(key);
|
|
if (v === undefined || v === "") return undefined;
|
|
return v === "true" || v === "1";
|
|
}
|
|
|
|
// -- App & middleware -----------------------------------------------
|
|
|
|
const app = new Hono<MailboxContext>();
|
|
app.use("/api/*", cors({
|
|
origin: (origin) => {
|
|
// Same-origin requests have no Origin header — allow them.
|
|
if (!origin) return origin;
|
|
// In development, allow localhost for Vite dev server.
|
|
try {
|
|
const url = new URL(origin);
|
|
if (url.hostname === "localhost" || url.hostname === "127.0.0.1") return origin;
|
|
} catch { /* invalid origin */ }
|
|
// Block all other cross-origin requests. The app is served from the
|
|
// same origin as the API, so legitimate browser requests never send
|
|
// an Origin header. Returning undefined omits Access-Control-Allow-Origin.
|
|
return undefined;
|
|
},
|
|
}));
|
|
app.use("/api/v1/mailboxes/:mailboxId/*", requireMailbox);
|
|
|
|
// -- Config ---------------------------------------------------------
|
|
|
|
app.get("/api/v1/config", (c) => {
|
|
const domainsRaw = c.env.DOMAINS || "";
|
|
const domains = domainsRaw.split(",").map((d) => d.trim()).filter(Boolean);
|
|
const emailAddresses = c.env.EMAIL_ADDRESSES ?? [];
|
|
return c.json({ domains, emailAddresses });
|
|
});
|
|
|
|
// -- Mailboxes ------------------------------------------------------
|
|
|
|
app.get("/api/v1/mailboxes", async (c) => {
|
|
const allMailboxes = await listMailboxes(c.env.BUCKET);
|
|
return c.json(allMailboxes.map((m) => ({ ...m, name: m.id })));
|
|
});
|
|
|
|
app.post("/api/v1/mailboxes", async (c) => {
|
|
const { name, settings, email: rawEmail } = CreateMailboxBody.parse(await c.req.json());
|
|
const email = rawEmail.toLowerCase();
|
|
const allowedAddresses = (c.env.EMAIL_ADDRESSES ?? []) as string[];
|
|
if (allowedAddresses.length > 0 && !allowedAddresses.map((a) => a.toLowerCase()).includes(email)) {
|
|
return c.json({ error: "Mailbox creation is restricted to configured EMAIL_ADDRESSES" }, 403);
|
|
}
|
|
const key = `mailboxes/${email}.json`;
|
|
if (await c.env.BUCKET.head(key)) return c.json({ error: "Mailbox already exists" }, 409);
|
|
const defaultSettings = { fromName: name, forwarding: { enabled: false, email: "" }, signature: { enabled: false, text: "" }, autoReply: { enabled: false, subject: "", message: "" } };
|
|
const finalSettings = { ...defaultSettings, ...settings };
|
|
await c.env.BUCKET.put(key, JSON.stringify(finalSettings));
|
|
const stub = c.env.MAILBOX.get(c.env.MAILBOX.idFromName(email));
|
|
await stub.getFolders();
|
|
return c.json({ id: email, email, name, settings: finalSettings }, 201);
|
|
});
|
|
|
|
app.get("/api/v1/mailboxes/:mailboxId", async (c) => {
|
|
const mailboxId = c.req.param("mailboxId")!;
|
|
const obj = await c.env.BUCKET.get(`mailboxes/${mailboxId}.json`);
|
|
if (!obj) return c.json({ error: "Not found" }, 404);
|
|
return c.json({ id: mailboxId, name: mailboxId, email: mailboxId, settings: await obj.json() });
|
|
});
|
|
|
|
app.put("/api/v1/mailboxes/:mailboxId", async (c) => {
|
|
const mailboxId = c.req.param("mailboxId")!;
|
|
const { settings } = (await c.req.json()) as { settings: Record<string, unknown> };
|
|
const key = `mailboxes/${mailboxId}.json`;
|
|
if (!(await c.env.BUCKET.head(key))) return c.json({ error: "Not found" }, 404);
|
|
await c.env.BUCKET.put(key, JSON.stringify(settings));
|
|
return c.json({ id: mailboxId, name: mailboxId, email: mailboxId, settings });
|
|
});
|
|
|
|
app.delete("/api/v1/mailboxes/:mailboxId", async (c) => {
|
|
const mailboxId = c.req.param("mailboxId")!;
|
|
const key = `mailboxes/${mailboxId}.json`;
|
|
if (!(await c.env.BUCKET.head(key))) return c.json({ error: "Not found" }, 404);
|
|
|
|
// Wipe the mailbox's Durable Object (emails, attachments, folders) and
|
|
// collect every attachment's R2 key BEFORE removing the R2 settings
|
|
// file, so a mailbox later recreated with this same email address never
|
|
// resurrects the deleted mailbox's history. See purgeAllData() for why
|
|
// this previously-missing step mattered (idFromName makes the DO keyed
|
|
// by email address permanent across delete/recreate).
|
|
const stub = c.env.MAILBOX.get(c.env.MAILBOX.idFromName(mailboxId));
|
|
const attachmentRows = (await (stub as any).purgeAllData()) as StoredAttachment[];
|
|
for (const batch of chunkArray(attachmentRows, 1000)) {
|
|
await c.env.BUCKET.delete(batch.map((att) => `attachments/${att.email_id}/${att.id}/${att.filename}`));
|
|
}
|
|
|
|
await c.env.BUCKET.delete(key);
|
|
return c.body(null, 204);
|
|
});
|
|
|
|
// -- Emails ---------------------------------------------------------
|
|
|
|
app.get("/api/v1/mailboxes/:mailboxId/emails", async (c: AppContext) => {
|
|
const folder = c.req.query("folder");
|
|
const thread_id = c.req.query("thread_id");
|
|
const threaded = boolQuery(c, "threaded");
|
|
const page = intQuery(c, "page");
|
|
const limit = intQuery(c, "limit");
|
|
const sortColumn = c.req.query("sortColumn") as any;
|
|
const sortDirection = c.req.query("sortDirection") as "ASC" | "DESC" | undefined;
|
|
const stub = c.var.mailboxStub;
|
|
|
|
if (threaded && folder) {
|
|
const emails = await (stub as any).getThreadedEmails({ folder, page, limit });
|
|
const totalCount = await (stub as any).countThreadedEmails(folder);
|
|
return c.json({ emails, totalCount });
|
|
}
|
|
const emails = await stub.getEmails({ folder, thread_id, page, limit, sortColumn, sortDirection });
|
|
if (folder) {
|
|
const totalCount = await stub.countEmails({ folder, thread_id });
|
|
return c.json({ emails, totalCount });
|
|
}
|
|
return c.json(emails);
|
|
});
|
|
|
|
app.post("/api/v1/mailboxes/:mailboxId/emails", async (c: AppContext) => {
|
|
const mailboxId = c.req.param("mailboxId")!;
|
|
const body = SendEmailRequestSchema.parse(await c.req.json());
|
|
const { to, cc, bcc, from, subject, html, text, attachments, in_reply_to, references, thread_id } = body;
|
|
|
|
let toStr: string, fromEmail: string, fromDomain: string;
|
|
try {
|
|
({ toStr, fromEmail, fromDomain } = validateSender(to, from, mailboxId));
|
|
} catch (e) {
|
|
if (e instanceof SenderValidationError) return c.json({ error: e.message }, 400);
|
|
throw e;
|
|
}
|
|
|
|
const { messageId, outgoingMessageId } = generateMessageId(fromDomain);
|
|
const stub = c.var.mailboxStub;
|
|
const rateLimitError = await (stub as any).checkSendRateLimit();
|
|
if (rateLimitError) return c.json({ error: rateLimitError }, 429);
|
|
const attachmentData = await storeAttachments(c.env.BUCKET, messageId, attachments);
|
|
|
|
// Actually deliver the email BEFORE recording it as sent. Previously the
|
|
// real send was fired via waitUntil with a swallowed .catch(console.error),
|
|
// so a real delivery failure was invisible: the email was still stored in
|
|
// Sent and the client was told "sent" even though it never left.
|
|
try {
|
|
await sendEmail(c.env.EMAIL, {
|
|
to, cc, bcc, from, subject, html, text,
|
|
attachments: attachments?.map((att) => ({ content: att.content, filename: att.filename, type: att.type, disposition: att.disposition || "attachment", contentId: att.contentId })),
|
|
...(in_reply_to ? { headers: buildThreadingHeaders(in_reply_to, references || []) } : {}),
|
|
});
|
|
} catch (e) {
|
|
console.error("Email delivery failed:", (e as Error).message);
|
|
return c.json({ error: `Failed to send email: ${(e as Error).message}` }, 502);
|
|
}
|
|
|
|
await stub.createEmail(Folders.SENT, {
|
|
id: messageId, subject, sender: fromEmail, recipient: toStr,
|
|
cc: cc ? (Array.isArray(cc) ? cc.join(", ") : cc).toLowerCase() : null,
|
|
bcc: bcc ? (Array.isArray(bcc) ? bcc.join(", ") : bcc).toLowerCase() : null,
|
|
date: new Date().toISOString(), body: html || text || "",
|
|
in_reply_to: in_reply_to || null, email_references: references ? JSON.stringify(references) : null,
|
|
thread_id: thread_id || in_reply_to || messageId, message_id: outgoingMessageId,
|
|
raw_headers: JSON.stringify([
|
|
{ key: "from", value: typeof from === "string" ? from : `${from.name} <${from.email}>` },
|
|
{ key: "to", value: Array.isArray(to) ? to.join(", ") : to },
|
|
...(cc ? [{ key: "cc", value: Array.isArray(cc) ? cc.join(", ") : cc }] : []),
|
|
...(bcc ? [{ key: "bcc", value: Array.isArray(bcc) ? bcc.join(", ") : bcc }] : []),
|
|
{ key: "subject", value: subject }, { key: "date", value: new Date().toISOString() },
|
|
{ key: "message-id", value: `<${outgoingMessageId}>` },
|
|
]),
|
|
}, attachmentData);
|
|
|
|
return c.json({ id: messageId, status: "sent" }, 200);
|
|
});
|
|
|
|
app.post("/api/v1/mailboxes/:mailboxId/drafts", async (c: AppContext) => {
|
|
const mailboxId = c.req.param("mailboxId")!;
|
|
const { to, cc, bcc, subject, body, in_reply_to, thread_id, draft_id } = DraftBody.parse(await c.req.json());
|
|
const stub = c.var.mailboxStub;
|
|
const messageId = crypto.randomUUID();
|
|
const now = new Date().toISOString();
|
|
|
|
// Create the new draft BEFORE deleting the one it supersedes. Autosave
|
|
// calls this endpoint repeatedly with draft_id set to the prior save's
|
|
// id; deleting first meant a failure in createEmail() (DO unavailable,
|
|
// a storage error, etc) silently destroyed the user's latest edits with
|
|
// nothing left to recover. Creating first means the same failure just
|
|
// leaves the old draft in place instead of losing data.
|
|
await stub.createEmail(Folders.DRAFT, {
|
|
id: messageId, subject: subject || "", sender: mailboxId.toLowerCase(),
|
|
recipient: (to || "").toLowerCase(), cc: cc?.toLowerCase() || null, bcc: bcc?.toLowerCase() || null,
|
|
date: now, body, in_reply_to: in_reply_to || null, email_references: null,
|
|
thread_id: thread_id || in_reply_to || messageId,
|
|
}, []);
|
|
|
|
if (draft_id) {
|
|
try {
|
|
await stub.deleteEmail(draft_id);
|
|
} catch (e) {
|
|
// Non-fatal: the new draft is already saved, so at worst this
|
|
// leaves a harmless superseded duplicate instead of losing data.
|
|
console.error("Failed to delete superseded draft:", (e as Error).message);
|
|
}
|
|
}
|
|
|
|
return c.json({ id: messageId, status: "draft", subject: subject || "", recipient: to || "", date: now }, 201);
|
|
});
|
|
|
|
app.get("/api/v1/mailboxes/:mailboxId/emails/:id", async (c: AppContext) => {
|
|
const email = await c.var.mailboxStub.getEmail(c.req.param("id")!);
|
|
if (!email) return c.json({ error: "Email not found" }, 404);
|
|
return new Response(JSON.stringify(email), {
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
});
|
|
|
|
app.put("/api/v1/mailboxes/:mailboxId/emails/:id", async (c: AppContext) => {
|
|
const { read, starred } = (await c.req.json()) as { read?: boolean; starred?: boolean };
|
|
const email = await c.var.mailboxStub.updateEmail(c.req.param("id")!, { read, starred });
|
|
return email ? c.json(email) : c.json({ error: "Email not found" }, 404);
|
|
});
|
|
|
|
app.delete("/api/v1/mailboxes/:mailboxId/emails/:id", async (c: AppContext) => {
|
|
const id = c.req.param("id")!;
|
|
const attachments = await c.var.mailboxStub.deleteEmail(id);
|
|
if (attachments === null) return c.json({ error: "Not found" }, 404);
|
|
if (attachments.length > 0) await c.env.BUCKET.delete(attachments.map((att: any) => `attachments/${id}/${att.id}/${att.filename}`));
|
|
return c.body(null, 204);
|
|
});
|
|
|
|
app.post("/api/v1/mailboxes/:mailboxId/emails/:id/move", async (c: AppContext) => {
|
|
const { folderId } = (await c.req.json()) as { folderId: string };
|
|
const success = await c.var.mailboxStub.moveEmail(c.req.param("id")!, folderId);
|
|
return success ? c.json({ status: "moved" }) : c.json({ error: "Folder not found" }, 400);
|
|
});
|
|
|
|
// -- Threads --------------------------------------------------------
|
|
|
|
app.get("/api/v1/mailboxes/:mailboxId/threads/:threadId", async (c: AppContext) => {
|
|
return c.json(await (c.var.mailboxStub as any).getThreadEmails(c.req.param("threadId")!));
|
|
});
|
|
|
|
app.post("/api/v1/mailboxes/:mailboxId/threads/:threadId/read", async (c: AppContext) => {
|
|
await c.var.mailboxStub.markThreadRead(c.req.param("threadId")!);
|
|
return c.json({ status: "marked_read" });
|
|
});
|
|
|
|
// -- Reply / Forward ------------------------------------------------
|
|
|
|
app.post("/api/v1/mailboxes/:mailboxId/emails/:id/reply", handleReplyEmail);
|
|
app.post("/api/v1/mailboxes/:mailboxId/emails/:id/forward", handleForwardEmail);
|
|
|
|
// -- Folders --------------------------------------------------------
|
|
|
|
app.get("/api/v1/mailboxes/:mailboxId/folders", async (c: AppContext) => c.json(await c.var.mailboxStub.getFolders()));
|
|
|
|
app.post("/api/v1/mailboxes/:mailboxId/folders", async (c: AppContext) => {
|
|
const { name } = (await c.req.json()) as { name: string };
|
|
const slug = slugify(name);
|
|
if (!slug) return c.json({ error: "Folder name must contain alphanumeric characters" }, 400);
|
|
const f = await c.var.mailboxStub.createFolder(slug, name);
|
|
return f ? c.json(f, 201) : c.json({ error: "Folder with this name already exists" }, 409);
|
|
});
|
|
|
|
app.put("/api/v1/mailboxes/:mailboxId/folders/:id", async (c: AppContext) => {
|
|
const { name } = (await c.req.json()) as { name: string };
|
|
const trimmed = (name || "").trim();
|
|
if (!trimmed) return c.json({ error: "Folder name is required" }, 400);
|
|
|
|
const result = await (c.var.mailboxStub as any).updateFolder(c.req.param("id")!, trimmed);
|
|
if ("error" in result) {
|
|
if (result.error === "not_found") return c.json({ error: "Folder not found" }, 404);
|
|
if (result.error === "system_folder") return c.json({ error: "Cannot rename a system folder" }, 403);
|
|
return c.json({ error: "A folder with this name already exists" }, 409);
|
|
}
|
|
return c.json(result);
|
|
});
|
|
|
|
app.delete("/api/v1/mailboxes/:mailboxId/folders/:id", async (c: AppContext) => {
|
|
const ok = await c.var.mailboxStub.deleteFolder(c.req.param("id")!);
|
|
return ok ? c.body(null, 204) : c.json({ error: "Folder not found or cannot be deleted" }, 400);
|
|
});
|
|
|
|
// -- Search ---------------------------------------------------------
|
|
|
|
app.get("/api/v1/mailboxes/:mailboxId/search", async (c: AppContext) => {
|
|
const searchOpts: Record<string, unknown> = {
|
|
query: c.req.query("query") || "", folder: c.req.query("folder"), from: c.req.query("from"),
|
|
to: c.req.query("to"), subject: c.req.query("subject"), date_start: c.req.query("date_start"),
|
|
date_end: c.req.query("date_end"), is_read: boolQuery(c, "is_read"),
|
|
is_starred: boolQuery(c, "is_starred"), has_attachment: boolQuery(c, "has_attachment"),
|
|
};
|
|
const stub = c.var.mailboxStub as any;
|
|
const emails = await stub.searchEmails({ ...searchOpts, page: intQuery(c, "page"), limit: intQuery(c, "limit") });
|
|
const totalCount = await stub.countSearchResults(searchOpts);
|
|
return c.json({ emails, totalCount });
|
|
});
|
|
|
|
// -- Attachments ----------------------------------------------------
|
|
|
|
app.get("/api/v1/mailboxes/:mailboxId/emails/:emailId/attachments/:attachmentId", async (c: AppContext) => {
|
|
const emailId = c.req.param("emailId")!;
|
|
const attachmentId = c.req.param("attachmentId")!;
|
|
const attachment = await c.var.mailboxStub.getAttachment(attachmentId);
|
|
if (!attachment) return c.json({ error: "Attachment not found" }, 404);
|
|
const obj = await c.env.BUCKET.get(`attachments/${emailId}/${attachmentId}/${attachment.filename}`);
|
|
if (!obj) return c.json({ error: "Attachment file not found" }, 404);
|
|
const headers = new Headers();
|
|
headers.set("Content-Type", attachment.mimetype);
|
|
const sanitized = attachment.filename.replace(/[\x00-\x1f"\\]/g, "_");
|
|
headers.set("Content-Disposition", `attachment; filename="${sanitized}"; filename*=UTF-8''${encodeURIComponent(attachment.filename)}`);
|
|
return new Response(obj.body, { headers });
|
|
});
|
|
|
|
// -- Receive inbound email ------------------------------------------
|
|
|
|
const MAX_EMAIL_SIZE = 25 * 1024 * 1024;
|
|
|
|
async function streamToArrayBuffer(stream: ReadableStream, streamSize: number) {
|
|
if (streamSize > MAX_EMAIL_SIZE) throw new Error(`Email too large: ${streamSize} bytes exceeds ${MAX_EMAIL_SIZE} byte limit`);
|
|
if (streamSize <= 0) throw new Error(`Invalid stream size: ${streamSize}`);
|
|
const result = new Uint8Array(streamSize);
|
|
let bytesRead = 0;
|
|
const reader = stream.getReader();
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
if (bytesRead + value.length > streamSize) { reader.cancel(); throw new Error(`Stream exceeds declared size`); }
|
|
result.set(value, bytesRead);
|
|
bytesRead += value.length;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
async function receiveEmail(event: ForwardableEmailMessage, env: Env, ctx: ExecutionContext) {
|
|
const rawEmail = await streamToArrayBuffer(event.raw, event.rawSize);
|
|
const parsedEmail = await new PostalMime().parse(rawEmail);
|
|
|
|
const allowedAddresses = ((env.EMAIL_ADDRESSES ?? []) as string[]).map((a) => a.toLowerCase());
|
|
const allRecipients = (parsedEmail.to || []).map((t) => t.address?.toLowerCase()).filter(Boolean) as string[];
|
|
const ccRecipients = (parsedEmail.cc || []).map((e) => e.address?.toLowerCase()).filter(Boolean) as string[];
|
|
const bccRecipients = (parsedEmail.bcc || []).map((e) => e.address?.toLowerCase()).filter(Boolean) as string[];
|
|
|
|
// Resolve the mailbox from the SMTP envelope recipient (RCPT TO, i.e.
|
|
// `event.to`), not the To: header. The header names the list address
|
|
// for mailing-list traffic and is absent/irrelevant for BCC-only
|
|
// delivery and some forwarders, while the envelope is what Email
|
|
// Routing actually matched and is the only field that survives those
|
|
// cases. The header-parsed recipients above are kept for display only.
|
|
const mailboxId = event.to.toLowerCase();
|
|
|
|
if (allowedAddresses.length > 0 && !allowedAddresses.includes(mailboxId)) {
|
|
console.log(`Ignoring email: ${mailboxId} not in EMAIL_ADDRESSES.`);
|
|
return;
|
|
}
|
|
|
|
const mailboxSettingsObj = await env.BUCKET.get(`mailboxes/${mailboxId}.json`);
|
|
if (!mailboxSettingsObj) { console.log(`Ignoring email for ${mailboxId}: mailbox does not exist`); return; }
|
|
const mailboxSettings = (await mailboxSettingsObj.json()) as {
|
|
forwarding?: ForwardingSettings;
|
|
autoReply?: AutoReplySettings;
|
|
};
|
|
|
|
const messageId = crypto.randomUUID();
|
|
|
|
const stub = env.MAILBOX.get(env.MAILBOX.idFromName(mailboxId));
|
|
|
|
const attachmentData: StoredAttachment[] = [];
|
|
if (parsedEmail.attachments) {
|
|
for (const att of parsedEmail.attachments) {
|
|
const attId = crypto.randomUUID();
|
|
const filename = (att.filename || "untitled").replace(/[\/\\:*?"<>|\x00-\x1f]/g, "_");
|
|
await env.BUCKET.put(`attachments/${messageId}/${attId}/${filename}`, att.content);
|
|
attachmentData.push({ id: attId, email_id: messageId, filename, mimetype: att.mimeType,
|
|
size: typeof att.content === "string" ? att.content.length : att.content.byteLength,
|
|
content_id: att.contentId || null, disposition: att.disposition || "attachment" });
|
|
}
|
|
}
|
|
|
|
const extractMsgId = (s: string) => { const m = s.match(/<([^>]+)>/); return m ? m[1] : s.trim().split(/\s+/)[0]; };
|
|
const inReplyTo = parsedEmail.inReplyTo ? extractMsgId(parsedEmail.inReplyTo) : null;
|
|
const emailReferences = parsedEmail.references ? parsedEmail.references.split(/\s+/).filter(Boolean).map(extractMsgId) : [];
|
|
let threadId = emailReferences[0] || inReplyTo || messageId;
|
|
|
|
if (!inReplyTo && emailReferences.length === 0) {
|
|
const subjectThread = await (stub as any).findThreadBySubject(parsedEmail.subject || "", parsedEmail.from?.address || undefined);
|
|
if (subjectThread) threadId = subjectThread;
|
|
}
|
|
|
|
const originalMessageId = parsedEmail.messageId ? extractMsgId(parsedEmail.messageId) : null;
|
|
|
|
// The To: header may be absent or may name a list/alias address rather
|
|
// than this mailbox (the cases this fix targets) — fall back to the
|
|
// envelope recipient so the stored recipient field is never empty.
|
|
const displayRecipients = allRecipients.length > 0 ? allRecipients : [mailboxId];
|
|
|
|
await stub.createEmail(Folders.INBOX, {
|
|
id: messageId, subject: parsedEmail.subject || "",
|
|
sender: (parsedEmail.from?.address || "").toLowerCase(), recipient: displayRecipients.join(", "),
|
|
cc: ccRecipients.join(", ") || null, bcc: bccRecipients.join(", ") || null,
|
|
date: new Date().toISOString(), // uses receive time, not the email's Date header
|
|
body: parsedEmail.html || parsedEmail.text || "",
|
|
in_reply_to: inReplyTo, email_references: emailReferences.length > 0 ? JSON.stringify(emailReferences) : null,
|
|
thread_id: threadId, message_id: originalMessageId, raw_headers: JSON.stringify(parsedEmail.headers),
|
|
}, attachmentData);
|
|
|
|
// Resolve the agent stub via the SDK (not a raw DO .get()) so the
|
|
// set-name handshake runs first. Mailboxes that have never been opened
|
|
// in the UI have no persisted DO name yet; calling .fetch() on a raw
|
|
// stub for them 500s with "Missing namespace or room headers" because
|
|
// partyserver's Server.fetch() requires a name before it will route.
|
|
// getAgentByName() performs that handshake before returning the stub.
|
|
const senderAddress = (parsedEmail.from?.address || "").toLowerCase();
|
|
ctx.waitUntil(
|
|
getAgentByName(env.EMAIL_AGENT, mailboxId)
|
|
.then((agentStub) =>
|
|
agentStub.fetch(new Request("https://agents/onNewEmail", {
|
|
method: "POST", headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ mailboxId, emailId: messageId, sender: senderAddress, subject: parsedEmail.subject || "", threadId }),
|
|
})),
|
|
)
|
|
.catch((e) => console.error("Auto-draft trigger failed:", (e as Error).message)),
|
|
);
|
|
|
|
// Inbound forwarding and the vacation responder were previously defined
|
|
// in mailbox settings (and settable via the API) but never actually
|
|
// acted on when mail arrived — the settings were stored and silently
|
|
// ignored. Wire both up for real, off the request's critical path.
|
|
ctx.waitUntil(
|
|
maybeForwardEmail({
|
|
env, mailboxId, forwarding: mailboxSettings.forwarding,
|
|
subject: parsedEmail.subject || "", senderAddress,
|
|
bodyHtml: parsedEmail.html || parsedEmail.text || "", bodyText: parsedEmail.text || "",
|
|
}).catch((e) => console.error("Auto-forward failed:", (e as Error).message)),
|
|
);
|
|
ctx.waitUntil(
|
|
maybeSendAutoReply({
|
|
env, stub, mailboxId, autoReply: mailboxSettings.autoReply,
|
|
senderAddress, threadId, headers: parsedEmail.headers,
|
|
}).catch((e) => console.error("Auto-reply failed:", (e as Error).message)),
|
|
);
|
|
}
|
|
|
|
export { app, receiveEmail };
|