# Process incoming emails

> Build an email triage workflow step by step with a connector trigger, Transform, and a Decision, then extend it.

## What you build

A workflow that starts automatically when a new email arrives, extracts key fields from the message, and routes it to the right action based on its content, for example logging support requests to a ticketing system or escalating urgent messages to a Slack channel.

## Before you start

- A connection to your mailbox, such as [Microsoft Outlook 365](connector-microsoft-outlook-365.md) or [Gmail](connector-gmail.md). [Create a connection](create-a-connection.md) describes how to create one.

## Nodes used

- Email integration trigger: starts the workflow when a new email arrives in the connected mailbox.
- [Transform](node-transform.md): extracts sender, subject, and body from the email payload.
- [Decision](node-decision.md): routes the email based on content.
- [Terminate](node-terminate.md): ends the workflow cleanly if the payload is malformed.

## Steps

:::tip Ask your coding agent
With a coding agent set up, you can skip the manual steps. Ask it:

```text
Build a Maestro Automate workflow that starts when a new email arrives in my mailbox connection, extracts the sender, subject, and body, and uses a Switch node to route support requests and urgent messages to different paths.
```

Then open the workflow on the canvas, check it against the steps below, and test it. [Set up your coding agent](set-up-your-coding-agent.md) describes the setup.
:::

### 1. Create a new workflow and add an email trigger

1. Create a project, as [Your first workflow](your-first-workflow.md#1-build-the-workflow) describes. The canvas opens with a **Manual Trigger** already on it, which stays for testing.
2. Select the **+** in the bottom toolbar, open the **Connector** category of the **Add node** panel, and find your email connector (Microsoft 365 or Gmail).
3. Select the **Email received** trigger.
4. In the properties panel, select the **Connection** to use and configure any filters (for example, a specific inbox folder).

### 2. Extract fields from the email

1. Select the **+** on the output handle of the email trigger, search for **Data Transform**, and select it.
2. Map the fields you need from the trigger's output payload:
   - `trigger.body.from` → `senderEmail`
   - `trigger.body.subject` → `emailSubject`
   - `trigger.body.body.content` → `emailBody`

The exact field names vary by connector. The trigger's output schema in the properties panel lists them.

### 3. Route based on content

1. Select the **+** on the output handle of the Data Transform node, search for **Decision**, and select it.
2. Set the condition to evaluate the email content. For example, to catch urgent messages: `$vars.emailSubject.toLowerCase().includes("urgent")`.
3. Connect the **True** branch to a high-priority action (for example, a Slack notification node).
4. Connect the **False** branch to a standard action (for example, creating a ticket in your support system).

### 4. Handle malformed payloads

Not every email has the expected fields. Malformed payloads are guarded against before they break the workflow:

1. Add a **Decision** node after the Data Transform and check that required fields are present, for example `$vars.senderEmail != null`.
2. Connect the valid branch to the routing Decision from step 3.
3. Connect the invalid branch to a **Script** node that logs the problem.
4. Connect the Script node to a **Terminate** node.

Any node that has an **Error** handle can also be connected to the same error path. The [Error handling](error-handling.md) page describes how.

### 5. Test and debug

To test without waiting for a real email, the Manual Trigger that came with the new project is used:

1. Configure the Manual Trigger with a sample input schema that matches the email payload shape.
2. Connect it to the Data Transform node, and select **Run Debug** (▶) in the bottom toolbar of the canvas with sample values.
3. Verify the routing logic in the execution trace.
4. Delete the Manual Trigger before deploying.

## Result

The workflow starts when a new email arrives, extracts the sender, subject, and body, and routes the message to the right action. Malformed payloads end on the error path and do not break the workflow.

## Extend this workflow

- Invoke an AI agent: before the Decision node, add an [Autonomous Agent](node-agent.md) node to classify the email with AI rather than a keyword match.
- Loop over attachments: if the email has attachments, add a [Loop](node-loop.md) node to process each one.
- Reply to the sender: after routing, add an email integration action node to send a confirmation reply to `{{ $vars.senderEmail }}`.

## Next steps

- [Deploy a workflow](deploy-a-workflow.md): make the workflow run without you.
- [Guides](guides.md): more workflows to build.
- [Community](community.md): share what you built, or ask other builders for help.
