---
title: "Email with ctx.email"
description: "Send email from tools and workflow steps, with attachments and idempotency keys."
---

> Documentation Index
> Fetch the complete documentation index at: https://buildrforge.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Email with ctx.email

`ctx.email.send` sends an email to any address. Use it for notifications, digests, and reports that a person reads outside Forge.

Every email comes from Forge's no-reply address, under your extension's qualified name, such as `"integrations.timecards via Forge" <noreply@…>`. You can't set the sender. Set `replyTo` when replies should reach a person. Forge also adds `X-Forge-Org` and `X-Forge-Extension` headers, so a recipient's report traces back to the org and extension that sent the email.

`ctx.email` is available in two places:

- A tool's `ctx.email`.
- A workflow step's `s.email`, inside `step.do(name, async (s) => ...)`.

## Sending an email

`send` takes the email and, optionally, an idempotency key. The email follows the shape of Resend's `emails.send`, without `from`:

| Field | Type | Notes |
| --- | --- | --- |
| `to` | `string` or `string[]` | Required |
| `cc`, `bcc` | `string` or `string[]` | Optional |
| `replyTo` | `string` | Optional |
| `subject` | `string` | Required, up to 998 characters |
| `text`, `html` | `string` | An email needs one or both |
| `headers` | `Record<string, string>` | Optional. Headers that start with `X-Forge-` are Forge's own |
| `attachments` | `EmailAttachment[]` | Optional. See [Attachments](#attachments) |

`send` resolves to `{ id }`, the email's Message-ID, once Forge's email provider, Amazon SES, accepts the email.

```ts
import { defineExtension, object, string, tool } from "@buildr/sdk";

const sendDigest = tool(
  {
    description: "Emails the week's open RFIs to a project manager",
    input: object({ to: string(), week: string() }),
  },
  async (ctx, { to, week }) => {
    const rfis = (await ctx.forge.customRecords.listRepoCustomRecordsAtBranch({
      repo_id: ctx.repo.id,
      branch: ctx.branch,
      model_api_name: "rfi",
    })) as { data: unknown[] };
    const sent = await ctx.email.send(
      {
        to,
        subject: `Open RFIs for the week of ${week}`,
        text: `${rfis.data.length} RFIs are open.`,
      },
      { idempotencyKey: `rfi-digest-${week}-${to}` },
    );
    return { id: sent.id };
  },
);

export default defineExtension({}, { tools: { sendDigest } });
```

## Idempotency keys

An idempotency key is text of 1 to 256 characters. It names an email across your extension's calls in the same repo for 24 hours. A second send with the same key answers the first email's `id` and sends nothing, even if the second email differs. The same extension installed in two repos keeps a separate set of keys in each.

A key whose email was refused with `EmailRefused`, `EmailRateLimited`, or `EmailUnavailable` is free for another try, because the email didn't go out. After an `EmailFailed`, Forge can't tell whether the email went out, so it keeps the key for its 24 hours. A send with that key, or with a key whose email is still on its way, rejects with `EmailOutcomeUnknown` instead of risking a second email. To send the email again anyway, send it under a new key.

A workflow step's sends need no key of their own. A step that fails after its email went out runs again, so Forge gives each send in a step's work that names no key one derived from the run, the step's name, and which of the work's sends it is: the first, the second, and so on. Step names stay the same on every replay, so a retry derives the same keys, and the emails that went out before the step failed don't go out twice. A send that names its own key uses that key instead, and no key you pass can match a derived one.

```ts
await step.do("notify approver", (s) =>
  s.email.send({ to: approver, subject: "A change order waits for you", text: summary }),
);
```

A derived key depends on the order of the work's sends, so a step whose work sends in an order that changes between tries, such as one that loops over a list fetched in the step, should name each email's key itself.

## Attachments

Each attachment has these fields:

- `filename`: The file's name.
- `content`: The file's bytes, as a `Uint8Array` or an `ArrayBuffer`, or a string of them in base64.
- `contentType`: The file's MIME type. The default is `application/octet-stream`.
- `contentId`: Optional. Shows the file inline, as an image that the HTML names `cid:<contentId>`.

## Limits

| Limit | Value | What happens past it |
| --- | --- | --- |
| Recipients | 50 per email, across `to`, `cc`, and `bcc` | The send rejects with a `TypeError` |
| Attachments | 32 per email | The send rejects with a `TypeError` |
| Size | 5 MiB of text, HTML, and attachments | The send rejects with `EmailRefused` |
| Emails per call | 20 per tool call, and 20 per workflow step | Further sends reject with a `TypeError` |

## Errors

A send that Forge refuses rejects with a `ForgeError`, as a `ctx.forge` call does:

| Error | Status | Meaning |
| --- | --- | --- |
| `EmailRefused` | 400 | The email won't go out as it stands: an address is malformed or suppressed, a header isn't allowed, or the email is too large |
| `EmailRateLimited` | 429 | Forge's email provider is sending more slowly; a later try may succeed |
| `EmailUnavailable` | 503 | The email didn't go out, and retrying soon won't send it: the stage sends no email, sending is paused, or the day's quota is spent |
| `EmailFailed` | 502 | Forge can't tell whether the email went out. Its idempotency key, if it had one, stays held |
| `EmailOutcomeUnknown` | 409 | An earlier send with the same idempotency key began, and whether its email went out is unknown. Send under a new key to send it again |

A local development stage sends no real email. The server's local email transport checks each recipient address (`to`, `cc`, and `bcc`) and writes the email's text, HTML, and base64-encoded attachments to `.alchemy/local/email/text/`.

Source: https://buildrforge.com/docs/extensions/email/index.mdx
