SMTP 101


The Simple Mail Transfer Protocol (SMTP) is the most widely used protocol to send messages by Message Transfer Agents (MTA) on the internet. MTAs are client or server programs that perform email services, such as sending or receiving mail for a host computer. The protocol is defined in RFC 821 and RFC 1123, and was designed to transfer mail independently of any specific transmission subsystem. SMTP demands an ordered data stream of 7-bit US-ASCII characters, and has size limitations on certain objects discussed below.

The SMTP model is based on what is called a mail transaction. Sender and receiver MTAs send commands and replies in a structured, lock-step process. The sender MTA initiates the transaction steps by sending SMTP commands to the receiver. The receiver MTA replies to the sender with numeric reply codes, followed by a text string with additional information about the reply code.

The Mail Transaction

The sender MTA initiates a two-way TCP communication channel between it and the receiver MTA, generally on port 25. Once the connection is open, the receiver MTA sends reply code 220 indicating that it is ready. The sender MTA then sends the HELO command with the client host as an argument. The HELO command identifies the sender MTA to the receiver MTA, and the receiver MTA will respond with a reply code 250. This tells the sender MTA that the connection is open and ready to go. This step in the transaction identifies and confirms host addresses for both the sender and receiver MTAs.

Example:

Sender:     (Open the TCP connection to port 25.) 
Receiver:   220 starfleet.org Simple Mail Transfer Service Ready 
Sender:     HELO walton.net 
Receiver:   250 starfleet.org 

The mail transaction is initiated by the MAIL command from the sender MTA. This command causes the receiver MTA to reset its state tables and buffers, including the recipients and mail data. The syntax for the MAIL command is

MAIL <space> FROM:<reverse-path> <CRLF>

Example:

Sender:    MAIL FROM:<johnboy@walton.net> 
Receiver:   250 OK 

The reverse-path is the full reverse source route list, starting with the current client host and ending with the user mailbox. The reverse-path is modified by each MTA as the message travels toward its destination. Before transferring a message to the next relay host, the current host will remove its name from the beginning of the forward-path and adds its name to the beginning of the reverse path.

The forward-path is used in the next command, RCPT.

RCPT <space> TO:<forward-path> <CRLF> 

Example:

Sender:    RCPT TO:<riker@starfleet.org> 
Receiver:   250 OK 

If the mailbox address is acceptable to the receiver MTA, it transmits a reply code 250 to the sender MTA. If it cannot fulfill the request, it transmits a reply code of 550. One reason it may not be able to fulfill the request is because the mailbox is incorrect or non-existent.

The next step is for the sender MTA to issue the DATA command. There are no arguments to this command, it simply tells the receiver that the sender is ready to start sending the message. The receiver MTA will transmit a reply code 354 indicating that it is ready to receive the message. Once the sender MTA receives the correct reply code, it sends the mail data to the receiver. The mail data is followed by a <CRLF>.<CRLF> sequence indicating that there is no more data.

DATA <CRLF> 

Example:

Sender:    DATA 
Receiver:   354 Start mail input; end with <CRLF>.<CRLF> 
Sender:    yada yada yada... 
Sender:    blah blah blah... 
Sender:    . 
Receiver:   250 OK 

When the receiver MTA responds with the reply code 250, acknowledging receipt of the message, the sender MTA transmits the QUIT command. The receiver MTA sends the reply code 221, and the mail transaction is complete.

Sender:    QUIT 
Receiver:   221 starfleet.org Service closing transmission
channel 

SMTP Commands

The following are the SMTP commands. The commands marked "Required" are the ones specified by RFC 821 and RFC 1123 which must be part of a minimal SMTP implementation.

Command Required Description
HELO * Identifies server
MAIL * Initiates the mail transaction
RCPT * Identifies mail recipient
DATA * Initiates mail data transfer
RSET * Aborts the current mail transaction
NOOP * Receiver returns OK. Used to test the server connection
QUIT * Close the connection
VRFY * Verify recipient exists
SEND   Delivers message to one or more terminals
SOML   Delivers message to one or more terminals or mailboxes
SAML   Delivers message to one or more terminals and mailboxes
EXPN   Expand mailing list addresses
HELP   Requests Help info from receiver
TURN   Asks receiver to take role as server

SMTP Reply Codes

The following are the SMTP reply codes sent from the receiver MTA.

SMTP Reply Codes Description
211 System Status or system help reply
214 Help message
220 Service ready
221 Service closing transmission channel
250 Requested action OK and completed
251 User not local; will forward to
354 Start mail input; end with .
421 Service not available, closing connection
450 Mailbox unavailable, requested mail action not taken
451 Local error in processing, action aborted
452 Insufficient system storage, action not taken
500 Syntax error, command unrecognized
501 Syntax error in parameters or arguments
502 Command not implemented
503 Bad sequence of commands
504 Command parameter not implemented
550 Mailbox unavailable, action not taken
551 User not local; please try
552 Exceeded storage allocation, action aborted
553 Mailbox name not allowed, action not taken