# Outlook - Attachments

> 通过 Microsoft Graph API 下载和管理 Microsoft Outlook 邮件附件。

Source: https://docs.sommatic.ai/zh/nodes/connectors.microsoft.outlook.attachments/

- 标识: `connectors.microsoft.outlook.attachments`
- 类别: 连接器
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

通过 Microsoft Graph API 下载和管理 Microsoft Outlook 邮件附件。

专为企业环境中大批量邮件处理设计的附件处理节点。支持按类型、大小过滤，对附件较多的邮件分页处理，并可将大文件以流式方式写入磁盘。

**操作：**
- **List：** 获取所有附件的元数据（名称、类型、大小），不下载内容。
- **Download：** 以 base64 形式下载附件内容，或直接写入临时文件。
- **Download All：** 下载某封邮件的全部附件，可选过滤。

## 配置

- `connection_id` (string, 必填): 用于访问 Microsoft Graph API 的凭据。接受 `oauth2`（access + refresh token）、`service-account`（tenant id + client id + client secret）或 `object`（高级 Graph 配置）类型的凭据。
- `mailbox_mode` (string, 可选): 默认值: `token_owner`. 可选值: `token_owner`, `other`.
- `mailbox_email` (string, 可选, 支持表达式): 显示条件: mailbox_mode = `other`.
- `message_source` (string, 可选): Incoming：使用上一个节点传来的邮件 ID。Custom：手动指定消息 ID。 默认值: `incoming`. 可选值: `incoming`, `custom`.
- `custom_message_id` (string, 可选, 支持表达式): 显示条件: message_source = `custom`.
- `filter_content_type` (string, 可选, 支持表达式): 可选的 MIME 类型过滤（例如“application/pdf”“image/”）。支持前缀匹配。
- `filter_min_size` (number, 可选): 附件最小大小（字节）。可用于过滤掉内嵌图片。
- `filter_max_size` (number, 可选): 附件最大大小（字节）。
- `output_mode` (string, 可选): Content：在载荷中返回 base64。File：写入 /tmp/ 并返回文件路径（大附件推荐使用）。 默认值: `content`. 可选值: `content`, `file`.

配置中会显示通用执行设置，但该节点目前不执行重试（`retryOnFail`、`maxTries`）。所有节点均未实现 `continueOnFail`。

## 输出端口

- `success` (默认): Attachments Found
- `no_attachments`: No Attachments
- `error`: Error

## 输入契约

- `id` (string, 可选): 来自触发器或上一个节点的消息 ID。

## 输出契约

- `attachments` (array, 可选): 附件列表，包含元数据以及内容或路径。
- `count` (number, 可选): 找到的附件数量。

## 在表达式中

后续节点可通过 `$("Outlook - Attachments").item.json.<路径>` 读取此节点输出中的内容（请把 `Outlook - Attachments` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `success` 返回的路径，依据“端口”一节所示的输出。

- `$("Outlook - Attachments").item.json.attachments` (array) = `[…] (1)`: 附件列表，包含元数据以及内容或路径。
- `$("Outlook - Attachments").item.json.count` (number) = `1`: 找到的附件数量。


## 工作原理

通过 Microsoft Graph 下载一封 Outlook 邮件的附件，按 MIME 类型和大小过滤，并以 base64 内容或编排器上的文件形式返回。

1. 解析节点配置，必须提供 `connection_id`。获取 Graph 令牌：凭据密钥中包含 `client_id`、`client_secret`、`tenant_id` 时，以 client credentials 方式申请应用令牌；否则直接使用密钥中的 `token` 或 `access_token`。
2. `mailbox_mode` 为 `other` 时访问 `/users/<mailbox_email>`，否则访问 `/me`。
3. `message_source` 为 `custom` 时从 `custom_message_id` 获取邮件 ID，否则取输入项的 `id`。
4. 列出邮件的全部附件，并跟随 `@odata.nextLink` 分页。
5. 保留 `contentType` 以 `filter_content_type` 开头、且 `size` 位于 `filter_min_size` 与 `filter_max_size` 之间的附件。
6. 过滤后没有附件时返回 `no_attachments`。否则返回 `success`：每个附件附带 `content_bytes`（base64）；若为 `file` 模式，则将每个附件写入新的临时目录并返回其 `file_path`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-get-pdfs",
  "label": "Get PDF attachments",
  "operator_slug": "connectors.microsoft.outlook.attachments",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "mailbox_mode": "other",
    "mailbox_email": "support@example.com",
    "message_source": "incoming",
    "filter_content_type": "application/pdf",
    "filter_min_size": 1024,
    "output_mode": "content"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "attachments": [
      {
        "id": "AAMkAGI2THVSAAA=",
        "name": "invoice-2026-0418.pdf",
        "content_type": "application/pdf",
        "size": 48213,
        "content_bytes": "JVBERi0xLjcKJc...",
        "is_inline": false
      }
    ],
    "count": 1
  },
  "port": "success"
}
```

## 输出端口 (代码中的行为)

### `success`

至少有一个附件通过过滤。`file` 模式下每项包含 `file_path` 而不是 `content_bytes`。

```json
{
  "attachments": [
    {
      "id": "AAMkAGI2THVSAAA=",
      "name": "invoice-2026-0418.pdf",
      "content_type": "application/pdf",
      "size": 48213,
      "content_bytes": "JVBERi0xLjcKJc...",
      "is_inline": false
    }
  ],
  "count": 1
}
```

### `no_attachments`

邮件没有附件，或没有附件通过过滤。

```json
{
  "attachments": [],
  "count": 0
}
```

### `error`

校验失败（输出为信封 `{ ok, errors, port }`），或 Graph 调用失败（输出为包含 Graph 错误体的 `{ error, response }`）。

```json
{
  "ok": false,
  "errors": [
    "Failed to resolve Microsoft 365 access token"
  ],
  "port": "error"
}
```

## 代码中的行为

- `mailbox_mode`: 应用令牌（client credentials）没有 `/me`，此时请使用 `other` 并填写 `mailbox_email`。
- `filter_content_type`: 对 `contentType` 进行区分大小写的前缀匹配（例如 `image/` 或 `application/pdf`）。
- `output_mode`: `file` 模式以附件名写入编排器的临时目录；同名附件会互相覆盖，节点不会清理这些文件。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `Microsoft 365 credential is required` | `error` | `connection_id` 为空。 |
| `Failed to resolve Microsoft 365 access token` | `error` | 凭据无法解析、令牌申请失败，或密钥中既没有 client credentials 也没有令牌。具体原因不会返回。 |
| `Message ID is required. Ensure the previous node outputs an email with an ID, or select "Custom" and provide a Message ID.` | `error` | 输入项中没有 `id`，也未设置自定义 ID。 |
| `Request failed with status code <status>` | `error` | Graph 拒绝了调用：邮件 ID 或邮箱不存在时为 404，令牌过期或缺少 `Mail.Read` 权限时为 401/403。具体错误见 `response`。 |

## 说明与限制

- 凭据中保存的 OAuth 访问令牌按原样使用，不会刷新；过期后 Graph 返回 401。
- 项目附件和引用附件不包含 `contentBytes`：`content_bytes` 为 `null`；在 `file` 模式下虽然会返回 `file_path`，但不会写出文件。
- `content` 模式会将每个附件以 base64 放入工作流状态；大文件请优先使用 `file`。节点不会重试。


## 契约（JSON Schema）

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

```json
{
  "slug": "connectors.microsoft.outlook.attachments",
  "name": "Outlook - Attachments",
  "category": "integrations",
  "version": "1.0.0",
  "description": "Download and manage email attachments from Microsoft Outlook via Microsoft Graph API.\n\n  Dedicated attachment handler for corporate environments with high-volume email processing. Supports filtering by type, size, pagination for emails with many attachments, and streaming to disk for large files.\n\n  **Operations:**\n  - **List:** Get metadata of all attachments (name, type, size) without downloading content.\n  - **Download:** Download attachment content as base64 or write directly to temporary files.\n  - **Download All:** Download all attachments from a message with optional filtering.",
  "capabilities": [
    "email",
    "attachments",
    "microsoft",
    "outlook",
    "office365"
  ],
  "group": [
    {
      "key": "microsoft",
      "name": "Microsoft",
      "icon_url": "/icons/integrations/microsoft/microsoft.svg"
    },
    {
      "key": "microsoft365",
      "name": "Microsoft 365",
      "icon_url": "/icons/integrations/microsoft/microsoft365.svg"
    }
  ],
  "icon_url": "/icons/integrations/microsoft/outlook.svg",
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "Microsoft 365 Credential",
        "description": "Credential for Microsoft Graph API access. Accepts credentials of type `oauth2` (access + refresh token), `service-account` (tenant id + client id + client secret), or `object` (advanced Graph config).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "oauth2",
            "service-account",
            "object"
          ],
          "types": [
            "outlook",
            "microsoft365",
            "oauth2"
          ]
        }
      },
      "mailbox_mode": {
        "type": "string",
        "title": "Mailbox",
        "enum": [
          "token_owner",
          "other"
        ],
        "default": "token_owner"
      },
      "mailbox_email": {
        "type": "string",
        "title": "Mailbox Email",
        "format": "expression",
        "displayOptions": {
          "show": {
            "mailbox_mode": [
              "other"
            ]
          }
        }
      },
      "message_source": {
        "type": "string",
        "title": "Message Source",
        "enum": [
          "incoming",
          "custom"
        ],
        "default": "incoming",
        "description": "Incoming: uses the email ID from the previous node. Custom: specify a message ID manually."
      },
      "custom_message_id": {
        "type": "string",
        "title": "Message ID",
        "format": "expression",
        "displayOptions": {
          "show": {
            "message_source": [
              "custom"
            ]
          }
        }
      },
      "filter_content_type": {
        "type": "string",
        "title": "Filter by Type",
        "description": "Optional MIME type filter (e.g., \"application/pdf\", \"image/\"). Supports prefix matching.",
        "format": "expression"
      },
      "filter_min_size": {
        "type": "number",
        "title": "Min Size (bytes)",
        "description": "Minimum attachment size in bytes. Useful for filtering out inline images."
      },
      "filter_max_size": {
        "type": "number",
        "title": "Max Size (bytes)",
        "description": "Maximum attachment size in bytes."
      },
      "output_mode": {
        "type": "string",
        "title": "Output Mode",
        "enum": [
          "content",
          "file"
        ],
        "default": "content",
        "description": "Content: returns base64 in payload. File: writes to /tmp/ and returns file paths (recommended for large attachments)."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    },
    "required": [
      "connection_id"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "id": {
        "type": "string",
        "description": "Message ID from the trigger or previous node."
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "attachments": {
        "type": "array",
        "description": "List of attachments with metadata and content/path."
      },
      "count": {
        "type": "number",
        "description": "Number of attachments found."
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Attachments Found",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "no_attachments",
      "label": "No Attachments",
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "tags": [
    "email",
    "attachments",
    "microsoft",
    "outlook",
    "download"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "Microsoft 365 Credential",
      "description": "Credential for Microsoft Graph API access. Accepts credentials of type `oauth2` (access + refresh token), `service-account` (tenant id + client id + client secret), or `object` (advanced Graph config).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "oauth2",
          "service-account",
          "object"
        ],
        "types": [
          "outlook",
          "microsoft365",
          "oauth2"
        ]
      }
    },
    "mailbox_mode": {
      "type": "string",
      "title": "Mailbox",
      "enum": [
        "token_owner",
        "other"
      ],
      "default": "token_owner"
    },
    "mailbox_email": {
      "type": "string",
      "title": "Mailbox Email",
      "format": "expression",
      "displayOptions": {
        "show": {
          "mailbox_mode": [
            "other"
          ]
        }
      }
    },
    "message_source": {
      "type": "string",
      "title": "Message Source",
      "enum": [
        "incoming",
        "custom"
      ],
      "default": "incoming",
      "description": "Incoming: uses the email ID from the previous node. Custom: specify a message ID manually."
    },
    "custom_message_id": {
      "type": "string",
      "title": "Message ID",
      "format": "expression",
      "displayOptions": {
        "show": {
          "message_source": [
            "custom"
          ]
        }
      }
    },
    "filter_content_type": {
      "type": "string",
      "title": "Filter by Type",
      "description": "Optional MIME type filter (e.g., \"application/pdf\", \"image/\"). Supports prefix matching.",
      "format": "expression"
    },
    "filter_min_size": {
      "type": "number",
      "title": "Min Size (bytes)",
      "description": "Minimum attachment size in bytes. Useful for filtering out inline images."
    },
    "filter_max_size": {
      "type": "number",
      "title": "Max Size (bytes)",
      "description": "Maximum attachment size in bytes."
    },
    "output_mode": {
      "type": "string",
      "title": "Output Mode",
      "enum": [
        "content",
        "file"
      ],
      "default": "content",
      "description": "Content: returns base64 in payload. File: writes to /tmp/ and returns file paths (recommended for large attachments)."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  },
  "required": [
    "connection_id"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "description": "Message ID from the trigger or previous node."
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "attachments": {
      "type": "array",
      "description": "List of attachments with metadata and content/path."
    },
    "count": {
      "type": "number",
      "description": "Number of attachments found."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Attachments Found",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "no_attachments",
    "label": "No Attachments",
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "supports_condition": false
  }
]
```
