Receive Email on Your Own Addresses
Create addresses on your reply domain, such as support@inbound.example.com, and receive every email sent to them in your application. Each message arrives as an inbound webhook event, and you can also read the messages through the API. This guide builds one common setup: a mailbox for each chat channel, so that a reply sent to channel-c123@inbound.example.com lands in the right channel.
For every method and field, see the Mailboxes reference. For the webhook payload, see Inbound Events.
Before You Start
- An Outbound admin has turned on Inbound for your account. Ask Outbound support if you are not sure.
- Your API key is set in the environment, as in Configuration.
Step 1: Check Which Domains You Can Use
Each mailbox lives on a reply domain. listDomains() returns the domains you can use, and customNames tells you which of them let you choose the localPart.
const { domains } = await outbound.mailboxes.listDomains();
const custom = domains.find((d) => d.customNames);
console.log(custom?.hostname ?? 'No domain for custom names. Leave out localPart to get a generated address.');If the domain is shared with other accounts, leave out localPart when you create a mailbox and Outbound generates one for you. The rest of this guide works either way, but you should store the mailbox.id that comes back, not the address.
Step 2: Create a Mailbox
Create one mailbox for each chat channel. Keep your own channel ID in metadata. Outbound returns it with every inbound event for the mailbox.
const { mailbox } = await outbound.mailboxes.create({
localPart: 'channel-c123',
replyDomain: 'inbound.example.com',
label: 'Support channel',
metadata: { channelId: 'C123' },
});
await db.channels.update('C123', { inboundMailboxId: mailbox.id });
console.log(mailbox.address); // 'channel-c123@inbound.example.com'Give mailbox.address to the people who should write to the channel. Store mailbox.id with the channel, because you need it to read messages. A mailbox with the same address as a sender's plain reply address, such as admin@reply.yourcompany.com, receives the replies to that sender's emails instead of Conversations.
Step 3: Subscribe a Webhook to Inbound
Create a webhook that receives only the inbound event. Store the secret, because it is shown only once.
const { webhook, secret } = await outbound.webhooks.create({
url: 'https://yourapp.com/webhooks/outbound-inbound',
events: ['inbound'],
});
console.log(webhook.id);Save secret as OUTBOUND_WEBHOOK_SECRET in your environment.
Step 4: Handle the Event
Outbound sends a batch of events to your endpoint. Verify the signature first, using the raw request body, as in Conversations. Then handle each inbound event.
import express from 'express';
import { Outbound, type IncomingWebhookPayloadV2 } from '@masters-union/outbound-sdk';
const outbound = new Outbound({ apiKey: process.env.OUTBOUND_API_KEY! });
const app = express();
app.post('/webhooks/outbound-inbound', express.raw({ type: 'application/json' }), async (req, res) => {
const valid = Outbound.verifyWebhookSignature(
req.body,
req.header('X-Webhook-Signature') ?? '',
process.env.OUTBOUND_WEBHOOK_SECRET!,
);
if (!valid) return res.status(401).send('invalid signature');
const payload: IncomingWebhookPayloadV2 = JSON.parse(req.body.toString());
for (const event of payload.events) {
if (event.event !== 'inbound' || !event.eventId) continue;
const isNew = await db.processedEvents.insertIfNew(event.eventId);
if (!isNew) continue;
const channelId = String(event.metadata.channelId);
const { details } = event;
let html: string | null = null;
if (details.hasHtml && needsHtml(details.text ?? null)) {
const { html: body } = await outbound.mailboxes.getMessageHtml(event.messageId);
html = body;
}
await db.channelMessages.create({
channelId,
from: details.from,
subject: event.subject,
text: details.text,
html,
});
}
res.sendStatus(200);
});
app.listen(3000);Here is what the handler does:
- Verifies the signature before it reads the body. Requests with a bad signature get a
401. - Dedupes on
eventId. Outbound retries a delivery that fails, so the same event can arrive twice.eventIdis the same on every retry, so store it and skip events you have seen. - Reads the channel from
event.metadata.channelId.metadataholds the values you set on the mailbox, so it is typed asRecord<string, unknown>. Convert the value before you use it. - Reads the message from
event.details:fromis the sender,event.subjectis the subject, andtextis the plain text body. - Fetches the HTML only when you need it. The event carries
hasHtml, andgetMessageHtml()makes a second request. The example uses the plain text unless your ownneedsHtml()check asks for the HTML.
Outbound treats a non-2xx response or a timeout as a failed delivery and retries it, so keep the handler fast. If the work is slow, put it on a queue and return 200 first.
Gmail Forwarding
You can send mail from a Gmail account to a mailbox with a forwarding filter. The mailbox receives each forwarded message like any other message, and the handler above processes it.
- Add the forwarding address in Gmail. Open Gmail settings, go to Forwarding and POP/IMAP, and add
support@inbound.example.com. - Confirm the address. Gmail sends a confirmation message to the address before it forwards anything. That message arrives in your mailbox, so it triggers an
inboundevent like any other. Read the confirmation link or code fromevent.details.text, or find the message withlistMessages(), and confirm the address in Gmail. - Create a filter. Go to Filters and Blocked Addresses, create a filter for the mail you want, and choose Forward it to with the address.
Use a plus tag for each source, such as support+gmail@inbound.example.com for one Gmail account and support+billing@inbound.example.com for another. Each tag reaches the same mailbox, and details.tag tells you which one a message used. Gmail treats each address as a separate forwarding address, so confirm each tag once.
Forwarded mail keeps its original From address. If you restrict the mailbox (see below), allow the original sender's address or domain. You can also allow the address of the forwarding account.
Restrict Who Can Write In
By default a mailbox accepts mail from anyone. To accept mail only from people you trust, set allowedSenders when you create the mailbox or change it later:
await outbound.mailboxes.update(mailbox.id, {
allowedSenders: ['*@yourcompany.com', 'alerts@vendor.com'],
});*@yourcompany.com allows anyone at that exact domain. alerts@vendor.com allows that address only. Mail from any other sender is not delivered, and no inbound event fires for it. See Allowed Senders for the matching rules.
Pause a Mailbox
To stop receiving mail for a while without losing the mailbox, turn it off. Its address and its messages are kept:
await outbound.mailboxes.update(mailbox.id, { enabled: false });Mail sent to a mailbox that is off is not delivered and fires no event. Set enabled back to true to start receiving mail again. See Update Mailbox.
Treat the Content as Untrusted
The subject, the text and the HTML come from the sender, not from you. Anyone who can email the address can write any content into it.
- Escape
textandsubjectbefore you show them in a page. Never insert them as HTML. - Render HTML only in a sandboxed iframe. Load it with
getMessageHtml()and setsandboxon the iframe, as in the Mailboxes reference. Remote images are blocked unless you ask for them. - Do not act on a message alone. A message that asks for a password reset or a payment should not trigger it without a check on your side.
For a complete example of an inbox that shows these messages safely, see Show Inbound Conversations in Your Application.