Mailboxes

env.MAILBOX gives your app's signed-in users their own email accounts, like a small Gmail inside your app: addresses, folders, threads, read and starred, search, sending and replying, with email shown safely.

How it works

  • An account belongs to one of your users and owns one or more addresses: name@your-app.cloudrizz.app, or an address on your own domain.
  • Email to those addresses is filed in the account's Inbox (Junk when the spam filter flags it), grouped into threads, and searchable.
  • Every account has Inbox, Drafts, Sent, Archive, Junk and Trash, plus folders your users create.
  • Like env.DB, deciding which signed-in user may open which account is up to your code: check account.owner before every call.
Every workspace. Mail is paid as usage, like email, and stored mail (with its search data) as storage.

Start from the webmail template

Ask your assistant for a new app with type: "webmail" and you get a working webmail to make your own: sign-up and sign-in with emailed codes (2FA), one mailbox per user at name@your-app.cloudrizz.app, folders, conversations, safe reading, compose with attachments, replies, drafts, search by words or meaning, and a phone layout. Deploy it to production and it receives mail.

  • Nobody can create a mailbox until you list who may: set WEBMAIL_ALLOWED_EMAILS to their sign-in emails (yours first) before you go live, or WEBMAIL_OPEN_SIGNUP to true for anyone (everyone who signs up can send mail from your domain). Previews let the first person in, for testing.
  • On your own domain, use a subdomain for the addresses (for example post.yourcompany.com) if the company already gets email elsewhere, such as Google Workspace or Outlook: the main domain's mail would otherwise move to the app. Verify your company domain for sending (it covers its subdomains), add the subdomain as a receiving domain (one MX record), then set MAIL_DOMAIN to it before anyone signs up.
  • Give the webmail itself a different web address (for example mail.yourcompany.com): the mail name needs MX records, and one name can't hold both those and a web address.
  • Its api/_README_WEBMAIL.md lists the safety rules it keeps, so your assistant keeps them when changing it.

Accounts and the inbox

const acc = await env.MAILBOX.createAccount({
  owner: user.id,
  addresses: ["ada@your-app.cloudrizz.app"],
  displayName: "Ada",
});

// One row per conversation, newest first
const { threads, nextCursor } = await env.MAILBOX.threads(acc.id, { folder: "inbox" });

// Folders with total and unread counts
const { folders } = await env.MAILBOX.folders(acc.id);

// A conversation, and one message
const { emails } = await env.MAILBOX.thread(acc.id, threads[0].threadId);
const msg = await env.MAILBOX.get(acc.id, emails[0].id);
await env.MAILBOX.markRead(acc.id, msg.id);

Showing email safely

Email comes from strangers: its HTML can hide scripts, fake login forms and tracking images. Never put msg.html on your page. Ask for a view link and show it in a sandboxed frame:

const src = await env.MAILBOX.viewUrl(acc.id, msg.id, { images: false });
// <iframe src={src} sandbox="allow-popups allow-popups-to-escape-sandbox"></iframe>
  • Scripts, forms, frames and event handlers are removed; links open in a new tab.
  • Remote images stay blocked (no tracking) until the reader asks: use { images: true }. They then load through a proxy that hides the reader's IP address. msg.hasRemoteImages tells you when to offer "Show images".
  • Images embedded in the email itself always show.
  • Attachments download with env.MAILBOX.attachmentResponse(accountId, emailId, attachmentId).

Organizing and search

await env.MAILBOX.star(acc.id, [id]);
await env.MAILBOX.move(acc.id, [id], "archive"); // or a folder id
await env.MAILBOX.delete(acc.id, [id]);          // to Trash; delete again to remove (files too)
await env.MAILBOX.createFolder(acc.id, "Customers");

const { emails: hits } = await env.MAILBOX.search(acc.id, "invoice telenor");

// By meaning: finds "Faktura fra Telenor" for this, best first, with a score
const { emails: close } = await env.MAILBOX.search(acc.id, "the phone bill from last spring", { mode: "meaning" });

Search covers the subject, the people, the text and the text of attachments (PDF, Word, text), in any language. Attachment text and search by meaning are added a few seconds after a message arrives or is sent. Spam and drafts are searched by words only.

Sending, replies and drafts

await env.MAILBOX.send(acc.id, { to: "kari@example.com", subject: "Hi", text: "Hello!" });

// Reply in the same conversation ("Re:" and threading are set for you)
await env.MAILBOX.send(acc.id, { replyTo: msg.id, to: msg.from.address, text: "Thanks!" });

const draft = await env.MAILBOX.saveDraft(acc.id, { to: "x@y.com", subject: "Later", text: "..." });
await env.MAILBOX.send(acc.id, { draftId: draft.id, to: "x@y.com", subject: "Later", text: "..." });

Sent messages go to the account's Sent folder, and answers to them land in the same thread. cc, bcc and attachments work as in env.EMAIL.

Previews and limits

  • Each preview has its own, separate mailbox data, so testing never touches real mailboxes or another branch's test mail. Previews don't receive real email: ask your assistant for a test delivery.
  • Each account has a storage quota (5 GB unless you set one); a full account stops receiving.
  • Trash empties itself after 30 days. Deleted mail is deleted with its files and attachments.
  • env.MAILBOX.exportMbox(accountId) returns the whole account as an .mbox download that mail programs can import, for data requests or moving elsewhere.
  • Your assistant can look into the app's mailboxes while building: the accounts, an account's folders and threads, or a search in it (message lists only, never full messages).