# smtp — Stateful TCP 25/587 Mail Relay Conversations (`smtp`)

> **FrameAssembly v3.2.2** · Protocol 62 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/smtp

- **Full Protocol Name:** Simple Mail Transfer Protocol
- **Keyword:** `smtp`
- **OSI Layer & Transport:** Layer 7 · TCP 25 (or dstport 587 Submission)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 5321
- **Wireshark Display Filter:** `smtp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 25 (or dstport 587 Submission) -> SMTP`

## Overview

Step through a real RFC 5321 mail relay (`220` greeting -> `ehlo` -> `mailfrom` -> `rcptto` -> `354` -> `body` -> `250 OK`) over stateful TCP `25` to test mail-gateway DLP and open-relay signatures.

Simple Mail Transfer Protocol (`smtp`, RFC 5321) relays email between Mail Transfer Agents (MTAs) on **TCP port `25`** and mail submission gateways on **TCP port `587`**. An SMTP envelope steps through server `220` greeting -> client `EHLO` -> `MAIL FROM:<...>` -> `RCPT TO:<...>` -> `DATA` (`354`) -> RFC 5322 message `body` -> `250` acceptance -> `QUIT` (`221`).

FrameAssembly gives you clean keywords for every envelope command (`ehlo`, `helo`, `mailfrom`, `rcptto`, `data`, `quit`, or custom `cmd`) plus `code` / `reply` for server responses and `body` for message content.

## Minimal Quickstart (`smtp_quickstart.frasm`)

```frameassembly
host app_srv { ip 10.20.10.15 }
host mail_gw { ip 10.20.10.25 }

compile {
    app_srv -> mail_gw tcp syn srcport 51025 dstport 25 wait 1ms
    app_srv <- mail_gw tcp syn ack wait 1ms
    app_srv -> mail_gw tcp ack wait 1ms
    app_srv <- mail_gw smtp code 220 reply "mx01.corp.internal ESMTP Postfix" wait 2ms
    app_srv -> mail_gw smtp ehlo "app01.corp.internal" wait 2ms
    app_srv <- mail_gw smtp code 250 reply "mx01.corp.internal"
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `25` (`psh ack`; use `dstport 587` for submission) and terminates SMTP lines with `
`.
- Formats `MAIL FROM:<...>` (`mailfrom`) and `RCPT TO:<...>` (`rcptto`) cleanly and terminates `body` payloads with `
.
` when needed.
- Tracks TCP sequence and acknowledgment numbers automatically across the full multi-step envelope.

## Practical Lab Example: Complete End-to-End RFC 5321 Email Delivery Transaction (`smtp_full_envelope.frasm`)

Walks through a full TCP 25 session from `220` banner and `EHLO` through `MAIL FROM`, `RCPT TO`, `DATA` (`354`), message headers/body, `250 Queued`, and `QUIT`.

```frameassembly
host alert_bot { ip 172.16.15.20 }
host smtp_relay { ip 172.16.15.25 }

compile {
    alert_bot -> smtp_relay tcp syn srcport 52025 dstport 25 win 65535 mss 1460 wait 1ms
    alert_bot <- smtp_relay tcp syn ack win 65535 mss 1460 wait 1ms
    alert_bot -> smtp_relay tcp ack wait 1ms
    alert_bot <- smtp_relay smtp code 220 reply "relay.corp.internal ESMTP" wait 2ms
    alert_bot -> smtp_relay smtp ehlo "alert-bot.corp.internal" wait 1ms
    alert_bot <- smtp_relay smtp code 250 reply "PIPELINING" wait 1ms
    alert_bot -> smtp_relay smtp mailfrom "<noc-alerts@corp.internal>" wait 1ms
    alert_bot <- smtp_relay smtp code 250 reply "2.1.0 Ok" wait 1ms
    alert_bot -> smtp_relay smtp rcptto "<oncall@corp.internal>" wait 1ms
    alert_bot <- smtp_relay smtp code 250 reply "2.1.5 Ok" wait 1ms
    alert_bot -> smtp_relay smtp data wait 1ms
    alert_bot <- smtp_relay smtp code 354 reply "End data with <CR><LF>.<CR><LF>" wait 2ms
    alert_bot -> smtp_relay smtp body "Subject: Core Link Down\r\nFrom: noc-alerts@corp.internal\r\n\r\nGi1/0/49 went down.\r\n." wait 2ms
    alert_bot <- smtp_relay smtp code 250 reply "2.0.0 Ok: queued as 9F4A1" wait 1ms
    alert_bot -> smtp_relay smtp quit wait 1ms
    alert_bot <- smtp_relay smtp code 221 reply "2.0.0 Bye"
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show smtp_full_envelope.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly smtp_full_envelope.frasm smtp_full_envelope.pcap
frameassembly smtp_full_envelope.frasm smtp_full_envelope.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse smtp_full_envelope.pcap smtp_full_envelope_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r smtp_full_envelope.pcap
```

## All `smtp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host smtp [ehlo ["<domain>"] | helo ["<domain>"] | mailfrom "<addr>" | rcptto "<addr>" | data | quit | cmd "<verb>" [param "<text>"]] [code <200..599>] [reply "<text>"] [body "<text>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `ehlo ["<domain>"] \| helo ["<domain>"]` | Extended or classic SMTP greeting + client hostname | `"localhost"` | Writes `EHLO <domain>\r\n` or `HELO <domain>\r\n`. |
| `mailfrom "<addr>" \| rcptto "<addr>"` | Quoted sender or recipient email address | `Omitted unless set` | Writes `MAIL FROM:<addr>\r\n` or `RCPT TO:<addr>\r\n`. |
| `data \| quit \| cmd "<verb>" [param "<text>"]` | Envelope command or custom verb (e.g. `cmd "STARTTLS"`) | `Omitted` | Writes `DATA\r\n`, `QUIT\r\n`, or `<verb> <param>\r\n`. |
| `code <200..599> [reply "<text>"]` | 3-digit SMTP reply code (`220`, `250`, `354`, `421`, `550`) + text | `Omitted on client commands` | Emits a server reply line (`250 2.0.0 Ok\r\n`). |
| `body "<text>"` | Quoted RFC 5322 email headers + message body | `Empty` | Transmits the email message payload following `354` `DATA` acceptance. |

## Cross-Layer Options Valid with `smtp`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 25 (or dstport 587 Submission)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window size, flags & RFC 7323 options | `psh ack, win 65535, automatic TCP seq/acknum` | Overrides automatic TCP sequence tracking, flags, or RFC 7323 options (`mss`, `wscale`, `sackperm`, `tsval`, `tsecr`). |

## Common Mistakes & Quick Fixes

### No Spaces or Underscores in mailfrom and rcptto

- **Avoid:** `alert_bot -> smtp_relay smtp mail_from "<a@b>" rcpt_to "<c@d>"`
- **Valid v3.2.2 Syntax:** `alert_bot -> smtp_relay smtp mailfrom "<a@b>"`

Send `mailfrom` and `rcptto` on separate packet lines (one SMTP command per packet, spelled with zero underscores).
