Skip to content
Docs

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.

  • 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. Create a flow and add an Email trigger node (under Triggers — start the flow in the node picker). Set its mailboxId to your mailbox and connect its trigger handle to Start. Mail is only dispatched into flows whose trigger is wired to Start.
  2. 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.

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 greeting
headers, 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.

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.

  1. Click the Email trigger’s badge on the flow canvas and switch Active on.
  2. 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.

  1. 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.
  2. Under Runs, a run appears and completes; open it to see each node’s output, including the agent’s reply text.
  3. The reply arrives in your test inbox, threaded under your original message and sent from the mailbox’s own address.

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.

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 attachments field 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 whose isInline is true:

    {!! 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 attachmentIds to {!! $start->payload->attachments !!} to attach everything that arrived.

  • 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 withoutOverlapping option, 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 changes means 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. replyToMessageId is empty or does not hold the inbound messageId.
  • 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.