# Send Email (SMTP)

> 通过 SMTP 服务器发送邮件。

Source: https://docs.sommatic.ai/zh/nodes/io.smtp/

- 标识: `io.smtp`
- 类别: 输入与输出
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

通过 SMTP 服务器发送邮件。

## 配置

- `connection_id` (string, 必填): SMTP 服务器的凭据。接受 `basic-auth`（用户名 + 密码 + 主机 + 端口）或 `connection-string`（smtp:// / smtps:// URI）类型的凭据。
- `to` (string, 必填): 收件人邮箱（可多个）
- `subject` (string, 必填)
- `text` (string, 可选): 纯文本正文
- `html` (string, 可选): HTML 正文

## 输出端口

- `out` (默认): 邮件已被中继服务器接收
- `error`: 发送失败

## 输入契约

- `connection_id` (string, 必填)
- `to` (string, 必填)
- `subject` (string, 必填)
- `text` (string, 可选)
- `html` (string, 可选)

## 输出契约

- `result` (object, 可选): 发送信息

## 在表达式中

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

- `$("Send Email (SMTP)").item.json.result` (object) = `{…}`: 发送信息
- `$("Send Email (SMTP)").item.json.result.accepted` (array) = `[…] (1)` (编辑器不提示)
- `$("Send Email (SMTP)").item.json.result.rejected` (array) = `[…] (0)` (编辑器不提示)
- `$("Send Email (SMTP)").item.json.result.envelope` (object) = `{…}` (编辑器不提示)
- `$("Send Email (SMTP)").item.json.result.messageId` (string) = `"<5f1c2a7e-8b1d-4a3e-9c0f-2d6e7b8a9c10@exampl…"` (编辑器不提示)
- `$("Send Email (SMTP)").item.json.result.response` (string) = `"250 2.0.0 OK queued"` (编辑器不提示)


## 工作原理

通过凭据描述的 SMTP 服务器发送一封邮件，并返回中继服务器的投递报告。

1. 解析配置。`connection_id`、`to`、`subject`、`text` 和 `html` 缺失时回退到输入 payload 中的同名键；`connection_id`、`to` 和 `subject` 为必填。
2. 解析凭据：`host`、`port`（默认 587）、`secure`（默认 false）和 `from` 取自凭据配置；`username`（或 `user`）和 `password`（或 `pass`）取自密钥。`from` 默认为用户名。
3. 打开 SMTP 传输，仅在存在用户名时进行认证。
4. 使用 `from`、`to`、`subject`、`text` 和 `html` 发送邮件，并以 `{ result }` 从 `out` 返回中继报告。

## 在工作流中

### 节点

```json
{
  "node_id": "node-send-alert",
  "label": "Send alert email",
  "operator_slug": "io.smtp",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "to": "ops@example.com",
    "subject": "Order {{ $json.order_id }} requires review",
    "text": "Order {{ $json.order_id }} exceeded the approval limit.",
    "html": "<p>Order <strong>{{ $json.order_id }}</strong> exceeded the approval limit.</p>"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": {
      "accepted": [
        "ops@example.com"
      ],
      "rejected": [],
      "envelope": {
        "from": "no-reply@example.com",
        "to": [
          "ops@example.com"
        ]
      },
      "messageId": "<5f1c2a7e-8b1d-4a3e-9c0f-2d6e7b8a9c10@example.com>",
      "response": "250 2.0.0 OK queued"
    }
  },
  "port": "out"
}
```

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

### `out`

中继已接受邮件。`result` 为邮件库的发送报告：`accepted`、`rejected`、`envelope`、`messageId`、`response`，以及耗时字段。

```json
{
  "result": {
    "accepted": [
      "ops@example.com"
    ],
    "rejected": [],
    "envelope": {
      "from": "no-reply@example.com",
      "to": [
        "ops@example.com"
      ]
    },
    "messageId": "<5f1c2a7e-8b1d-4a3e-9c0f-2d6e7b8a9c10@example.com>",
    "response": "250 2.0.0 OK queued"
  }
}
```

### `error`

缺少必填字段、无法解析凭据，或连接、登录、发送失败。

```json
{
  "error": "Invalid login: 535 5.7.8 Authentication credentials invalid"
}
```

## 代码中的行为

- `connection_id`: 请使用在配置中包含 `host`、`port`、`secure` 和 `from` 的 `basic-auth` 凭据。来自已保存凭据的 `connection-string` 密钥不会被解析。以 `{` 开头或包含 `://`、`@` 的值会被就地解析（JSON 对象或 `smtp://user:pass@host:port` URI），仅用于本地测试。
- `html`: 两者都设置时与 `text` 一同发送；邮件客户端会显示 HTML 部分。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `to is required` | `error` | 缺少收件人。`to` 可填写一个地址或以逗号分隔的多个地址。（`connection_id is required` 和 `subject is required` 对应其他必填字段。） |
| `Invalid login: <server response>` | `error` | 服务器拒绝了用户名或密码。要求应用专用密码的服务商需要在凭据中保存该密码。 |
| `<TLS or connection error>` | `error` | `secure` 与端口不匹配：465 端口请使用 `secure: true`，587 端口请使用 `secure: false`（会自动协商 STARTTLS）。 |

## 说明与限制

- 此节点不支持附件、`cc`、`bcc` 和 `replyTo`。
- `out` 表示中继已接受邮件，并不代表已送达收件箱。不具备幂等性：每次运行都会发送一封新邮件；不会重试。


## 契约（JSON Schema）

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

```json
{
  "slug": "io.smtp",
  "name": "Send Email (SMTP)",
  "category": "io",
  "version": "1.0.0",
  "description": "Send emails via SMTP server.",
  "capabilities": [
    "email",
    "smtp",
    "communication"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "SMTP Credential",
        "description": "Credential for the SMTP server. Accepts credentials of type `basic-auth` (username + password + host + port) or `connection-string` (smtp:// / smtps:// URI).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "basic-auth",
            "connection-string"
          ],
          "types": [
            "smtp"
          ]
        }
      },
      "to": {
        "type": "string",
        "description": "Recipient email(s)"
      },
      "subject": {
        "type": "string"
      },
      "text": {
        "type": "string",
        "description": "Text body"
      },
      "html": {
        "type": "string",
        "description": "HTML body"
      }
    },
    "required": [
      "connection_id",
      "to",
      "subject"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "to": {
        "type": "string"
      },
      "subject": {
        "type": "string"
      },
      "text": {
        "type": "string"
      },
      "html": {
        "type": "string"
      }
    },
    "required": [
      "connection_id",
      "to",
      "subject"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "object",
        "description": "Send info"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Sent",
      "description": "Email accepted by relay",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Sending failed",
      "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": "SMTP Credential",
      "description": "Credential for the SMTP server. Accepts credentials of type `basic-auth` (username + password + host + port) or `connection-string` (smtp:// / smtps:// URI).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "basic-auth",
          "connection-string"
        ],
        "types": [
          "smtp"
        ]
      }
    },
    "to": {
      "type": "string",
      "description": "Recipient email(s)"
    },
    "subject": {
      "type": "string"
    },
    "text": {
      "type": "string",
      "description": "Text body"
    },
    "html": {
      "type": "string",
      "description": "HTML body"
    }
  },
  "required": [
    "connection_id",
    "to",
    "subject"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "to": {
      "type": "string"
    },
    "subject": {
      "type": "string"
    },
    "text": {
      "type": "string"
    },
    "html": {
      "type": "string"
    }
  },
  "required": [
    "connection_id",
    "to",
    "subject"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "object",
      "description": "Send info"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Sent",
    "description": "Email accepted by relay",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Sending failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
