# Read Email (IMAP)

> Read emails from IMAP server.

Source: https://docs.sommatic.ai/en/nodes/io.imap/

- Slug: `io.imap`
- Family: Input and output
- Version: 1.0.0
- Status: Available
- Execution: async

Read emails from IMAP server.

## Configuration

- `connection_id` (string, required): Credential for the IMAP mail server. Accepts credentials of type `basic-auth` (username + password + host + port).
- `box` (string, optional): Default: `INBOX`.
- `searchCriteria` (array, optional): Criteria like UNSEEN, ALL, or ['SINCE', date] Default: `["UNSEEN"]`.
- `markSeen` (boolean, optional): Default: `true`.

## Output ports

- `out` (default): List of found emails
- `empty`: No emails found matching criteria
- `error`: Connection or fetch error

## Input contract

- `connection_id` (string, required)
- `box` (string, optional)
- `searchCriteria` (array, optional)
- `markSeen` (boolean, optional)

## Output contract

- `emails` (array, optional): List of found emails

## In expressions

What a later node can read from this node's output with `$("Read Email (IMAP)").item.json.<path>` (write your node's label instead of `Read Email (IMAP)`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Read Email (IMAP)").item.json.emails` (array) = `[…] (1)`: List of found emails


## How it works

Connects to an IMAP mailbox, searches one folder with IMAP criteria and returns the matching messages with subject, sender, date, raw text body and attributes; no match leaves through `empty`.

1. Resolves the config. `connection_id`, `box` (default `INBOX`), `searchCriteria` (default `["UNSEEN"]`) and `markSeen` (default true) fall back to the same keys of the incoming payload; an empty `box` or empty criteria array counts as not set.
2. Resolves the credential: `host`, `port` (default 993) and `tls` (default true) from its config; `username` (or `user`) and `password` (or `pass`) from its secret. Authentication waits up to 10 s.
3. Opens `box`, runs the search and fetches the `HEADER` and `TEXT` parts of every match. With `markSeen`, fetched messages are flagged as read.
4. Maps each message to `{ subject, from, date, body, attributes }` and closes the connection.
5. At least one message leaves through `out` as `{ emails }`; none leaves through `empty` with `{}`.

## In a workflow

### Node

```json
{
  "node_id": "node-read-invoices",
  "label": "Read invoice mailbox",
  "operator_slug": "io.imap",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "box": "INBOX",
    "searchCriteria": [
      "UNSEEN"
    ],
    "markSeen": true
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "emails": [
      {
        "subject": "Invoice INV-2026-0042",
        "from": "Billing <billing@example.com>",
        "date": "Wed, 25 Nov 2026 14:12:09 +0000",
        "body": "Hello,\r\nPlease find the invoice attached.\r\n",
        "attributes": {
          "uid": 1842,
          "flags": [
            "\\Seen"
          ],
          "date": "2026-11-25T14:12:09.000Z"
        }
      }
    ]
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The search matched at least one message. `subject`, `from` and `date` are the first value of each header; `body` is the raw `TEXT` part (MIME source for multipart messages); `attributes` holds the IMAP `uid`, `flags` and `date`.

```json
{
  "emails": [
    {
      "subject": "Invoice INV-2026-0042",
      "from": "Billing <billing@example.com>",
      "date": "Wed, 25 Nov 2026 14:12:09 +0000",
      "body": "Hello,\r\nPlease find the invoice attached.\r\n",
      "attributes": {
        "uid": 1842,
        "flags": [
          "\\Seen"
        ],
        "date": "2026-11-25T14:12:09.000Z"
      }
    }
  ]
}
```

### `empty`

The search matched no messages.

```json
{}
```

### `error`

Missing `connection_id`, credential, connection, login, folder or search failure. Data is `{ error, details }`, where `details` carries the error `code`, `stack` and its raw serialization.

```json
{
  "error": "Invalid credentials (Failure)",
  "details": {
    "code": null,
    "stack": "<stack>",
    "raw": "<serialized error>"
  }
}
```

## Behaviour in the code

- `searchCriteria`: IMAP search criteria as an array, for example `["UNSEEN"]`, `["ALL"]` or `["UNSEEN", ["SINCE", "2026-11-01"]]`. An empty array falls back to the payload, then to `["UNSEEN"]`.
- `markSeen`: Only an explicit `false` keeps messages unread. With the default `UNSEEN` criteria, this is what keeps the next run from returning the same messages.
- `connection_id`: Accepts `basic-auth` credentials. A value containing `:` or a JSON object is treated as a raw `host:port` for local tests, with the user and password read from the incoming payload.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `connection_id is required (check config or config.payload)` | `error` | No credential in the config nor in the incoming payload. Select an IMAP credential. |
| `<IMAP server error>` | `error` | Wrong user or password, the folder in `box` does not exist, or the criteria are invalid. For mail providers that require app passwords, store one in the credential. |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | The credential does not exist or this workflow cannot use it. |

## Notes and limits

- All matching messages are fetched in one run, with no limit or pagination; narrow `searchCriteria` on large mailboxes.
- Attachments are not extracted: they stay inside the raw MIME `body`.
- Not idempotent with `markSeen: true`: a run changes the mailbox state.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "io.imap",
  "name": "Read Email (IMAP)",
  "category": "io",
  "version": "1.0.0",
  "description": "Read emails from IMAP server.",
  "capabilities": [
    "email",
    "imap",
    "communication"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "IMAP Credential",
        "description": "Credential for the IMAP mail server. Accepts credentials of type `basic-auth` (username + password + host + port).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "basic-auth"
          ],
          "types": [
            "imap"
          ]
        }
      },
      "box": {
        "type": "string",
        "default": "INBOX"
      },
      "searchCriteria": {
        "type": "array",
        "default": [
          "UNSEEN"
        ],
        "description": "Criteria like UNSEEN, ALL, or ['SINCE', date]"
      },
      "markSeen": {
        "type": "boolean",
        "default": true
      }
    },
    "required": [
      "connection_id"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "box": {
        "type": "string"
      },
      "searchCriteria": {
        "type": "array"
      },
      "markSeen": {
        "type": "boolean"
      }
    },
    "required": [
      "connection_id"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "emails": {
        "type": "array",
        "description": "List of found emails"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Emails",
      "description": "List of found emails",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "empty",
      "label": "No Emails",
      "description": "No emails found matching criteria",
      "is_default": false,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Connection or fetch error",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io"
  ],
  "tags": [
    "email",
    "communication"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "IMAP Credential",
      "description": "Credential for the IMAP mail server. Accepts credentials of type `basic-auth` (username + password + host + port).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "basic-auth"
        ],
        "types": [
          "imap"
        ]
      }
    },
    "box": {
      "type": "string",
      "default": "INBOX"
    },
    "searchCriteria": {
      "type": "array",
      "default": [
        "UNSEEN"
      ],
      "description": "Criteria like UNSEEN, ALL, or ['SINCE', date]"
    },
    "markSeen": {
      "type": "boolean",
      "default": true
    }
  },
  "required": [
    "connection_id"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "box": {
      "type": "string"
    },
    "searchCriteria": {
      "type": "array"
    },
    "markSeen": {
      "type": "boolean"
    }
  },
  "required": [
    "connection_id"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "emails": {
      "type": "array",
      "description": "List of found emails"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Emails",
    "description": "List of found emails",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "empty",
    "label": "No Emails",
    "description": "No emails found matching criteria",
    "is_default": false,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Connection or fetch error",
    "is_default": false,
    "supports_condition": false
  }
]
```
