A mailbox lets people and systems send email to an extension: replies to RFIs, invoices a vendor forwards, or reports another service mails out. It is email’s counterpart of a webhook.
A mailbox has two parts:
- An email handler decides about each email sent to the mailbox’s address, while the sending server waits. It reads the email and decides: accept it, bounce it, or drop it.
- A tool runs once for each email the email handler accepts, after the sending server has its answer. It’s an ordinary call of one of the extension’s tools, which takes what the email handler accepted as its input.
Mailbox declarations in index.ts
Import mailbox from @buildr/sdk and pass your mailboxes under mailboxes in defineExtension:
import {
accept,
array,
defineExtension,
ignore,
mailbox,
object,
reject,
string,
tool,
} from "@buildr/sdk";
const recordReply = tool(
{
description: "Records a reply to an RFI",
input: object({ from: string(), subject: string(), body: string(), files: array(string()) }),
},
async (ctx, reply) => {
// Update the repo with ctx.forge, as any tool does.
return null;
},
);
const rfiReplies = mailbox(
{
instructions: "Send RFI replies here, or CC this address on the thread.",
handleEmail: (ctx, email) => {
if (email.subject.toLowerCase().startsWith("out of office")) return ignore();
if (email.text === null) return reject("Send the reply as plain text.");
return accept({
from: email.from,
subject: email.subject,
body: email.text,
files: email.attachments.map((file) => file.attachmentBlobId),
});
},
},
recordReply,
);
export default defineExtension({}, { tools: { recordReply }, mailboxes: { rfiReplies } });mailbox takes these options:
handleEmail: The email handler, described below. Required.instructions: What the address is for, in at most 4000 characters, whichforge mailboxes list-mailboxesshows each member.
The email handler
handleEmail(ctx, email) gets the email as its headers and bodies say. ctx.address is the address it was sent to, and ctx.receivedAt is when Forge received it. email has:
from,fromName,to,cc,replyTo,subject,date: As the headers say. The sender’s own server writesFrom, so nothing proves who sent an email; check it against what you expect before you trust it.messageIdandinReplyTo: TheMessage-IDandIn-Reply-Toheaders, which thread a reply to an email the extension sent withctx.email.textandhtml: The bodies, ornullfor one the email doesn’t have.headers: Each header’s first value, by its lowercase name, with any encoded words, such as=?UTF-8?B?…?=, decoded as the subject’s are.attachments: Each file the email carried, up to 16 MiB each, which Forge saved as an attachment in the repo before the handler ran:attachmentBlobId,filename,contentType, andsize. Name anattachmentBlobIdin a record’s attachment field to keep the file; one that no record names expires after 7 days, as any upload does.
It returns one of:
accept(payload, { dedupeKey }): Runs the tool withpayload, which must fit the tool’s input. An email whosededupeKey(1 to 256 characters), orMessage-IDwhen there’s none, was accepted before is the same delivery, so a sender that sends it again runs nothing more.reject(reason): Bounces the email to its sender withreason, of at most 512 characters.ignore(): Drops the email without a bounce.
The email handler has no network and no ctx.forge: it checks and decides, and the tool does the work. An email handler that throws leaves a failed delivery that says why, and the sending server tries again later.
Before any handler sees it, Forge drops mail that its Auto-Submitted header marks as automatic, as most out-of-office replies and bounces are, and mail from Forge’s own no-reply address. That stops most loops between a mailbox and a tool that sends email. An automatic reply without the header still reaches the handler, so have it ignore() the ones it recognizes, as the example above does. An email over 32 MiB, or with an attachment over 16 MiB, bounces before the handler runs.
Addresses
A repo gets an address for a mailbox when a member subscribes it:
forge mailboxes create-mailbox-subscription --repo "$JOB" --extension integrations.rfis --mailbox rfiRepliesAn address reads <name>.<id>@<domain>, such as rfi-replies.k3m7x2p6qa@mail.buildr-forge.com. The name defaults to the mailbox’s, and a member can choose it when they subscribe with --data '{"name":"job-101-rfis"}': at most 40 lowercase letters, digits, and hyphens, starting and ending with a letter or digit. Only the id routes the email, so the name never has to be unique. The address is the repo’s for good: subscribing again answers the same one.
A pull request’s preview has a domain of its own, such as rfi-replies.k3m7x2p6qa@pr-848.mail.forge-pr.dev.
forge mailboxes list-mailboxes shows each mailbox main runs and its address, and forge mailboxes list-mailbox-deliveries shows its latest deliveries.
A delivery’s tool acts as the extension itself, with only org connections.