How to build an email agent
This guide builds an email agent: a flow that starts when a message lands in your mailbox, hands it to an Agent, and sends the agent’s answer back as a reply in the same thread, from the same address the mail arrived on.
The whole agent is three nodes: an Email trigger wired to Start, then Start → Agent → Channel / Email.
Prerequisites
Section titled “Prerequisites”- A mailbox connected to your workspace that can send as well as receive. An OAuth mailbox needs send permission granted, an IMAP mailbox needs its SMTP host set, and a forward-address mailbox is receive-only, so it can trigger the agent but not carry its reply.
- A model for the agent to run on: a workspace key for your provider, set on the AI models page, or a secret holding an API key.
1. Wire the flow
Section titled “1. Wire the flow”- Create a flow and add an Email trigger node (under Triggers — start the flow in the node picker). Set its
mailboxIdto your mailbox and connect its trigger handle to Start. Mail is only dispatched into flows whose trigger is wired to Start. - Wire
Start → Agent → Channel / Email.
One mailbox can feed several flows: every flow with an armed Email trigger pointing at it runs on each message. The exception is overlap: while a previous run of a flow is still in progress, a new message is skipped rather than queued, unless you switch off the trigger’s withoutOverlapping option. See overlap protection.
2. Configure the agent
Section titled “2. Configure the agent”On the Agent node, pick the Provider and Model. Leave Credential empty to use the workspace key for that provider, or select a secret holding your own API key.
Set systemMessage to the agent’s standing instructions. The one rule that matters most here: the agent’s answer goes straight into the reply body, so tell it to output only that body.
You are the support agent for Acme Bikes, answering customer email.
Write a complete, ready-to-send reply to the message you receive:- Answer in the language the customer wrote in.- Be concise and concrete. If you are missing something you need, such as an order number, ask for it.- Sign off as "Acme Bikes support".
Output only the body text of the reply. No subject line, no greetingheaders, and do not quote the original message back.Set prompt to the inbound message, pulled from the trigger’s $start payload:
From: {!! $start->payload->from !!}Subject: {!! $start->payload->subject !!}
{!! $start->payload->text !!}Use {!! !!} rather than {{ }} here: the subject and body can contain characters like & or quotes, and {{ }} would HTML-escape them before the model sees them. See expressions.
The agent emits its answer under content, so the next node reads it as $prev->content.
3. Send the reply in-thread
Section titled “3. Send the reply in-thread”On the Channel / Email node:
| Field | Value |
|---|---|
To (to) |
{{ $start->payload->from }} |
Subject (subject) |
Re: {!! $start->payload->subject !!} |
Text (text) |
{!! $prev->content !!} |
In reply to (replyToMessageId, under More settings) |
{{ $start->payload->messageId }} |
Leave Send through empty: the reply goes out from one of the workspace’s mailboxes. In reply to is what makes it a reply instead of a new mail: it threads the message into the recipient’s existing conversation, and it lets you leave mailboxId empty, because the reply is routed back through the mailbox the original message arrived on.
4. Arm the trigger and publish
Section titled “4. Arm the trigger and publish”- Click the Email trigger’s badge on the flow canvas and switch Active on.
- Publish the flow.
Triggered runs execute the published version. While nothing is published and the trigger is on, the version note on the toolbar reads Not published · its trigger runs the draft, so every saved edit is live. Once you publish, the note names the version that runs (for example Live: v3) and later edits stay in the draft until you publish again. The Active switch itself is not part of a version: publishing does not capture it, and a rollback does not flip it. See Draft, publish and versions and the badge states under Triggers.
Verify it works
Section titled “Verify it works”- Send an email to the connected address. Forwarded mail arrives as it is received; polled mailboxes (IMAP and OAuth) are checked about once a minute in a standard deployment.
- Under Runs, a run appears and completes; open it to see each node’s output, including the agent’s reply text.
- The reply arrives in your test inbox, threaded under your original message and sent from the mailbox’s own address.
Give the agent memory (optional)
Section titled “Give the agent memory (optional)”As built, each email is a fresh run and the agent remembers nothing between messages. To make follow-ups land in context, add a Chat Thread node (under AI in the picker) and connect it to the agent’s MEMORY handle:
| Field | Value |
|---|---|
channel |
email |
conversationId |
{{ $start->payload->from }} |
title |
{!! $start->payload->subject !!} |
Keying conversationId on the sender gives the agent one running conversation per correspondent, so “any update on my last question?” works. To keep one conversation per email thread instead, key it on {{ $start->conversationKey }}: every message in a thread carries the same key, and a new thread from the same sender starts a new conversation. The transcripts appear under Conversations in your workspace. See memory for the other memory layers an agent can carry.
Handle attachments (optional)
Section titled “Handle attachments (optional)”The payload lists each attachment with a stored id (see the payload reference). Three things you can do with it:
-
Hand the files to the agent: set the Agent’s
attachmentsfield to the attachments array. The agent reads PDFs, office documents and spreadsheets as text, and sees photos when its model can see images. A PDF can also be shown to the model as pictures of its pages, which is how a model reads a drawing; see Files the agent reads. Outlook also lists the images a message shows in its body, such as signature logos and pasted screenshots, as attachments. To pass only the files the sender attached, leave out the entries whoseisInlineistrue:{!! json_encode(array_values(array_filter($start->payload->attachments, fn ($a) => ! $a->isInline))) !!} -
Read a document’s content before the agent runs: insert a Read a File as Text node between Start and the Agent, set its source type to Storage object and its source to
{!! $start->payload->attachments[0]->id !!}. It extracts PDFs, office documents and spreadsheets to plain text, which the agent’s prompt can then include as{!! $prev->documents[0]->text !!}. The node fails when no attachment is present, so use it in a flow whose mail always carries one, or guard it with conditional logic. -
Forward the files with the reply: set the Channel / Email node’s
attachmentIdsto{!! $start->payload->attachments !!}to attach everything that arrived.
Troubleshooting
Section titled “Troubleshooting”- No run starts. Confirm the trigger’s badge shows it is armed, the trigger handle is connected to Start, the mailbox is active, and (for a forward mailbox) forwarding is confirmed at your provider. Polled mailboxes deliver on the next poll, not instantly. And a message that arrives while a previous run is still in progress is skipped by the trigger’s
withoutOverlappingoption, on by default. - Runs use an old version of the flow. Triggered runs execute the published version. The version note on the flow toolbar names the version that runs, so a note like
Live: v2 · draft has 3 changesmeans newer draft edits are not live until you publish them. - The run completes but no reply arrives. The mailbox cannot send: an OAuth connection without send permission, or an IMAP mailbox without an SMTP host. The mailbox page under Mailboxes reports which, and the run’s Channel / Email node shows the send error.
- The reply starts a new thread instead of replying.
replyToMessageIdis empty or does not hold the inboundmessageId. - The agent step fails. Open the run under Runs for the error, commonly a missing provider key or a model the provider does not offer.
Related
Section titled “Related”- Email trigger: the trigger’s configuration and full payload reference.
- Channel / Email: sending tiers, draft mode, and limits.
- How to connect a mailbox: the mailbox setup this guide builds on.
- How to build an AI agent: tools, memory, and testing in depth.
- How to add human approval: keep a person between the agent and the send button.