# Send Email (SMTP)

> Send emails via SMTP server.

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

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

Send emails via SMTP server.

## Configuration

- `connection_id` (string, required): Credential for the SMTP server. Accepts credentials of type `basic-auth` (username + password + host + port) or `connection-string` (smtp:// / smtps:// URI).
- `to` (string, required): Recipient email(s)
- `subject` (string, required)
- `text` (string, optional): Text body
- `html` (string, optional): HTML body

## Output ports

- `out` (default): Email accepted by relay
- `error`: Sending failed

## Input contract

- `connection_id` (string, required)
- `to` (string, required)
- `subject` (string, required)
- `text` (string, optional)
- `html` (string, optional)

## Output contract

- `result` (object, optional): Send info

## In expressions

What a later node can read from this node's output with `$("Send Email (SMTP)").item.json.<path>` (write your node's label instead of `Send Email (SMTP)`). **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.

- `$("Send Email (SMTP)").item.json.result` (object) = `{…}`: Send info
- `$("Send Email (SMTP)").item.json.result.accepted` (array) = `[…] (1)` (Not suggested by the editor)
- `$("Send Email (SMTP)").item.json.result.rejected` (array) = `[…] (0)` (Not suggested by the editor)
- `$("Send Email (SMTP)").item.json.result.envelope` (object) = `{…}` (Not suggested by the editor)
- `$("Send Email (SMTP)").item.json.result.messageId` (string) = `"<5f1c2a7e-8b1d-4a3e-9c0f-2d6e7b8a9c10@exampl…"` (Not suggested by the editor)
- `$("Send Email (SMTP)").item.json.result.response` (string) = `"250 2.0.0 OK queued"` (Not suggested by the editor)


## How it works

Sends one email through an SMTP server described by a credential and returns the delivery report of the relay.

1. Resolves the config. `connection_id`, `to`, `subject`, `text` and `html` fall back to the same keys of the incoming payload; `connection_id`, `to` and `subject` are required.
2. Resolves the credential: `host`, `port` (default 587), `secure` (default false) and `from` from its config; `username` (or `user`) and `password` (or `pass`) from its secret. `from` defaults to the username.
3. Opens the SMTP transport, authenticating only when a user is present.
4. Sends the message with `from`, `to`, `subject`, `text` and `html`, and returns the relay report as `{ result }` through `out`.

## In a workflow

### Node

```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"
}
```

## Output ports (behaviour in the code)

### `out`

The relay accepted the message. `result` is the mail library's send report: `accepted`, `rejected`, `envelope`, `messageId`, `response`, plus timing fields.

```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`

A required field is missing, the credential cannot be resolved, or the connection, login or send failed.

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

## Behaviour in the code

- `connection_id`: Use a `basic-auth` credential with `host`, `port`, `secure` and `from` in its config. A `connection-string` secret is not parsed when it comes from a stored credential. A value that starts with `{` or contains `://` or `@` is parsed in place (a JSON object, or an `smtp://user:pass@host:port` URI) for local tests.
- `html`: Sent together with `text` when both are set; mail clients show the HTML part.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `to is required` | `error` | No recipient. `to` accepts one address or several separated by commas. (`connection_id is required` and `subject is required` report the other required fields.) |
| `Invalid login: <server response>` | `error` | The server rejected the user or password. Providers that require app passwords need one stored in the credential. |
| `<TLS or connection error>` | `error` | `secure` does not match the port: use `secure: true` with 465 and `secure: false` with 587 (STARTTLS is negotiated automatically). |

## Notes and limits

- Attachments, `cc`, `bcc` and `replyTo` are not supported by this node.
- `out` means the relay accepted the message, not that it reached the inbox. Not idempotent: every run sends a new email; there are no retries.


## Contracts (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
  }
]
```
