SkybuDocs

Ferdigheter · Apper

Receive email in an app

Let an app receive email sent to a domain the user owns, delivered as a signed HTTP POST: give the app a path, add and verify the mail domain, give the user the TXT and MX records, and check each delivery's signature in the app's code.

skill://apps/receive-emailAgenter leser den med resources/read eller skills_read. Den er skrevet på engelsk, for agentene.

Skybu runs the mail server for every app. Mail sent to a domain the app has verified reaches the app as an HTTP POST of the raw message, signed so the app can tell it came from Skybu. The app never runs a mail server or opens port 25. Use this skill when an app should take replies, support mail, forwarded mailboxes or anything else sent by email.

In short

  1. get_app or list_projects to find the app's id. Only a server app can receive email, not a static site or a worker.
  2. Add a route to the app that takes POST with the raw message and checks Skybu-Signature (step 1), and deploy it.
  3. set_inbound_mail with that path. The app restarts with SKYBU_INBOUND_MAIL_SECRET.
  4. add_mail_domain with the domain. Give the user the TXT and MX records from the answer.
  5. verify_mail_domain once the user says the records are in place. Follow it with get_inbound_mail until the domain is receiving.

Step 1: the route in the app

Skybu sends each message as one request to the path you choose:

PartWhat it holds
BodyThe whole message as it arrived (Content-Type: message/rfc822), with Skybu's Received and Authentication-Results headers on top. Parse it with a MIME parser such as mailparser (Node) or email (Python). Trust only the topmost Authentication-Results (SPF, DKIM, DMARC); Skybu removes any lower one that claims to be its own.
Skybu-Mail-FromThe envelope sender (empty for bounces).
Skybu-Rcpt-ToThe envelope recipients, comma-separated: all at this app's domains. They can differ from the To header, for example for Bcc and forwarded mail.
Skybu-Remote-AddressThe address of the server that delivered it.
Skybu-Delivery-IdA new id for each delivery. A sender that tries again sends the same message under a new id; use the message's Message-ID to spot duplicates.
Skybu-Signaturet=<unix time>,v1=<hex>, sometimes with two v1 values (see rotating, below).

v1 is HMAC-SHA256 with SKYBU_INBOUND_MAIL_SECRET as the key, over five lines, each ending in a newline (\n): t, then the values of Skybu-Mail-From, Skybu-Rcpt-To, Skybu-Remote-Address and Skybu-Delivery-Id, followed by the raw body bytes. Accept the request only if one v1 matches and t is less than five minutes old. The route is also reachable from the internet like the rest of the app, so never skip the check. In Node:

import { createHmac, timingSafeEqual } from "node:crypto";

export function fromSkybu(headers, body) {
  const parts = String(headers["skybu-signature"] ?? "").split(",").map((part) => part.trim().split("="));
  const t = Number(parts.find(([name]) => name === "t")?.[1]);
  if (!Number.isInteger(t) || Math.abs(Date.now() / 1000 - t) > 300) return false;
  const fields = [t, ...["skybu-mail-from", "skybu-rcpt-to", "skybu-remote-address", "skybu-delivery-id"].map((name) => headers[name] ?? "")];
  const signed = Buffer.concat([Buffer.from(`${fields.join("\n")}\n`), body]);
  const wanted = Buffer.from(createHmac("sha256", process.env.SKYBU_INBOUND_MAIL_SECRET).update(signed).digest("hex"));
  return parts.some(([name, value]) => name === "v1" && value?.length === wanted.length && timingSafeEqual(Buffer.from(value), wanted));
}

Read the body as raw bytes, before any JSON or form parser touches it (express.raw({ type: "message/rfc822", limit: "26mb" }) in Express). Messages are at most 25 MiB.

What the route answers is what the sender's server hears:

The app answersThe sender getsUse it when
2xxDelivered (250).The message is stored or handled. Answer only then.
400, 410, 415, 422Refused for good (550); the sender is told.The message is unwanted or cannot be handled, ever.
413Too large (552).
Anything else, or no answer in 30 secondsTry again later (451).The app is busy, restarting or its database is down. The sender keeps the message and tries again for days.

Skybu stores no messages. Until the app answers 2xx, the message stays with the sender. A 401 or 403 is also answered with "try again later", so a deploy or a secret rotation in progress loses nothing.

Step 2: turn it on

{ "name": "set_inbound_mail", "arguments": { "appId": "<app id>", "path": "/inbound-email" } }

The path starts with / and has at most 200 characters. The app restarts with SKYBU_INBOUND_MAIL_SECRET. Refusals:

CodeWhy
invalid_inbound_mail_pathNot a path.
inbound_mail_static_siteThe app is a static site or a worker; it has no server to post to.
inbound_mail_not_configuredThis Skybu has no inbound email set up. Tell the user.

"path": null turns it off; mail to the app's domains is then refused.

Step 3: the mail domain

{ "name": "add_mail_domain", "arguments": { "appId": "<app id>", "hostname": "support.example.no" } }

The answer has the domain's id, its status (pending), a txt record and an mx record. Give the user both, to create at their DNS provider:

_skybu-challenge.support.example.no  TXT  "skybu-…"
support.example.no                   MX   10  mx.skybu.app

Take the MX host from mx.value, not from this example. The domain can be the same one the user sends mail from, or a subdomain kept for the app (in.example.no, support.example.no). An MX record replaces where all mail for that name goes, so warn the user before they change the MX of a domain whose mailboxes they use, such as their company domain. Suggest a subdomain then, or a forwarding rule in their mail service that sends one address on to an address at the app's domain.

CodeWhy
invalid_domain, domain_ip_address, domain_wildcard, domain_reservedNot a hostname, or one Skybu keeps; as for custom domains.
mail_domain_takenAnother app on Skybu already takes email for it.
mail_domain_limit_reachedThe app has 5 mail domains, the most it can have.

A mail domain is separate from the app's custom domains: it needs no CNAME and no certificate, and a domain can be both.

Step 4: verify and follow

{ "name": "verify_mail_domain", "arguments": { "mailDomainId": "<mail domain id>" } }

It checks the TXT record, and fails with mail_domain_verification_failed and the reason if it is missing. DNS changes can take a few minutes; wait and try again. A domain not verified within 7 days is removed.

Once verified, Skybu takes mail for the domain. Each domain then also has:

FieldMeaning
routingnull when the MX record points at Skybu; otherwise problem is no_record (no MX record yet) or elsewhere (hosts lists where it points instead).
receivingtrue once Skybu has seen the MX record point at it: mail to the domain reaches the app.

Skybu checks the MX record every few hours, and on every verify_mail_domain. get_inbound_mail shows the domains and the latest deliveries:

{ "name": "get_inbound_mail", "arguments": { "appId": "<app id>" } }

Each delivery has at, bytes, domain, result (delivered, refused, deferred or too_large), the app's appStatus and an error when the app could not be reached. No addresses or content are kept. A row of deferred deliveries means the app is not answering 2xx: read its logs.

Things to know

  • Leave the TXT record in place. Skybu checks it daily; after 3 days without it, mail to the domain is refused and the organisation's owners get an email. verify_mail_domain brings it back once the record is there.
  • rotate_inbound_mail_secret makes a new secret. The app restarts with it, and for 24 hours every delivery carries a v1 for both the new and the old secret, so a check against either passes.
  • The user can read the secret with skybu mail secret <app> or in the dashboard, to run the app on their own machine; every read is logged.
  • Only the production app receives email, not pull request previews. A members-only app receives it too: the signature, not sign-in, protects the route.
  • Skybu takes at most 50 recipients per message and 120 messages a minute per app; above that the sender tries again.
  • Addresses must be ASCII. A sender or recipient address with other characters is refused (553).
  • remove_mail_domain stops mail to the domain at once. Confirm with the user first.
  • The CLI has the same: skybu mail set <app> --path /inbound-email, skybu mail domain add <app> <domain>, skybu mail domain verify <domain>, skybu mail status <app>. In the dashboard it is the app's Email tab.