SMTP - Simple Mail Transfer Protocol

pcapkit.protocols.application.smtp.protocol contains SMTP, which implements an extractor for the Simple Mail Transfer Protocol (SMTP) [RFC 5321] at TCP port 25, and for Message Submission [RFC 6409], the same protocol at TCP port 587. Its lines are read through pcapkit.utilities.lines, which FTP shares.

Added in version 1.10.0.

class pcapkit.protocols.application.smtp.protocol.SMTP(file, length=None, /, *, layer=None, protocol=None, context=None)[source]

Bases: Application[SMTP, SMTP]

This class implements Simple Mail Transfer Protocol.

What a client sends after a 354 reply is mail data, but one segment carries no session state to tell it from commands. So a segment reads as the first of these that it is, whole:

  • a command, one line of a verb and its argument clause (RFC 5321 Section 4.1.1);

  • a reply, lines sharing one code, each before the last followed by a hyphen (RFC 5321 Section 4.2); if the last is too, the reply goes on in a later segment;

  • mail data, lines whose last, and only the last, holds a single period: the end of mail data indication (RFC 5321 Section 4.1.1.4), the one mark in the octets that they follow a 354.

Mail data in the segments before the one that ends it is therefore refused, as are octets that are not whole lines, and a line of it alone in a segment reads as a command if it can. Pipelined commands (RFC 2920) and several replies in one segment are refused too, so on a TCP port each such segment is kept as Raw.

Text is decoded with the charset chardet detects, which can misread a short run of non-ASCII octets; the octets still rebuild exactly, as FTP’s do.

Properties

property name: Literal['Simple Mail Transfer Protocol']

Name of current protocol.

property length: NoReturn

Header length of current protocol.

Raises:

UnsupportedCall – This protocol doesn’t support length.

Methods

read(length=None, **kwargs)[source]

Read Simple Mail Transfer Protocol (SMTP).

Parameters:
  • length (int | None) – Length of packet data.

  • **kwargs (Any) – Arbitrary keyword arguments.

Return type:

SMTP

Returns:

Parsed packet data.

Raises:

ProtocolError – If the packet is no command, reply or mail data.

make(*, cmmd=None, args=None, raw_cmmd=None, code=None, text=None, more=False, lines=None, charset=None, **kwargs)[source]

Make (construct) packet data.

A command takes cmmd, args and raw_cmmd, a reply code, text and more, both charset, and mail data lines alone. Each line is terminated with CRLF, and each is checked to read back as made.

Parameters:
Return type:

SMTP

Returns:

Constructed packet data.

Raises:

ProtocolError – If not exactly one of cmmd, code and lines is given, or with another’s keywords, or if the octets made would not read back as made.

Data Models

classmethod __index__()[source]

Numeral registry index of the protocol.

Return type:

int

Returns:

Numeral registry index of the protocol in IANA service names and port numbers: TCP port 25, the port of TCP.smtp as an int, which TCP.__proto__ dispatches here, as it does port 587 (The Registry Index).

Utilities

classmethod _make_data(data)[source]

Create key-value pairs from data for protocol construction.

Parameters:

data (SMTP) – protocol data

Return type:

dict[str, Any]

Returns:

Key-value pairs for protocol construction.

Auxiliary Data

class pcapkit.protocols.application.smtp.protocol.Type(*values)[source]

Bases: EnumLookup, StrEnum

SMTP packet type.

Built on EnumLookup, the lookup contract shared by every non-registry enumeration (#877).

COMMAND = 'command'

Command packet.

REPLY = 'reply'

Reply packet.

MAIL_DATA = 'mail_data'

Mail data packet.

Header Schemas

class pcapkit.protocols.application.smtp.schema.SMTP(data=NO_VALUE, *, __packet__=None, **kwargs)[source]

Bases: Schema

Header schema for SMTP packet.

data: bytes = <BytesField data>

Packet data.

Data Models

class pcapkit.protocols.application.smtp.data.SMTP(*args, **kwargs)[source]

Bases: Protocol

Data model for SMTP protocol.

type: Type

Type.

class pcapkit.protocols.application.smtp.data.Command(type, cmmd, args, charset, raw_cmmd)[source]

Bases: SMTP

Data model for an SMTP command (RFC 5321 Section 4.1.1).

type: Literal[Type.COMMAND]

Type.

cmmd: Command

Command verb.

args: str | None

Argument clause, None if the command carries none.

charset: str | None

Charset to encode args with, None for UTF-8.

raw_cmmd: bytes

Verb as received, in its original case; cmmd is the case-insensitive lookup of it.

class pcapkit.protocols.application.smtp.data.Reply(type, code, text, more, charset)[source]

Bases: SMTP

Data model for an SMTP reply (RFC 5321 Section 4.2).

type: Literal[Type.REPLY]

Type.

code: ReplyCode

Reply code, the same on every line.

text: tuple[str | None, ...]

Text of each line, in order; the last is None where that line closes the reply with its code alone, with no SP.

more: bool

Whether the last line carries a hyphen, so the reply goes on in a later segment.

charset: str | None

Charset to encode text with, None for UTF-8.

class pcapkit.protocols.application.smtp.data.MailData(type, lines, text)[source]

Bases: SMTP

Data model for the mail data a DATA command sends, up to and including the end of mail data indication (RFC 5321 Section 4.1.1.4).

type: Literal[Type.MAIL_DATA]

Type.

lines: tuple[bytes, ...]

Lines as sent, dot-stuffed, without their CRLF or the line ending the mail data.

text: tuple[bytes, ...]

The same lines with the dot-stuffing undone (RFC 5321 Section 4.5.2).

Constants

pcapkit.protocols.application.smtp.const.SMTP_COMMAND

An SMTP command line, CRLF excluded. A verb of letters, then SP and the argument clause where one follows (RFC 5321 Section 4.1.1); only the one SP is syntax, so trailing white space stays in the argument clause.

pcapkit.protocols.application.smtp.const.SMTP_REPLY

An SMTP reply line, CRLF excluded. A Reply-code (RFC 5321 Section 4.2), then a hyphen or SP and the text, or nothing where the line closes the reply.