SMTP is a plain text conversation. A client opens a connection, sends short commands such as EHLO, MAIL FROM, RCPT TO and DATA, and the server answers each one with a three digit reply code. A normal delivery uses the same handful of commands in the same order: greet, name the sender, name each recipient, send the message, quit.
This reference lists every command you are likely to meet, shows a complete session, and explains the replies and mistakes that cause most real world failures. The authoritative definition is RFC 5321; extensions such as STARTTLS and AUTH are defined in their own RFCs.
The SMTP commands at a glance
| Command | Purpose | Typical success reply |
|---|---|---|
HELO domain | Identify the client, original form, no extensions | 250 |
EHLO domain | Identify the client and request the extension list | 250 (multi-line) |
MAIL FROM:<address> | Start a transaction and set the envelope sender | 250 |
RCPT TO:<address> | Add one envelope recipient; repeat for more | 250 or 251 |
DATA | Begin the message content | 354, then 250 after the final dot |
RSET | Abort the current transaction, keep the connection | 250 |
VRFY | Ask whether a mailbox exists (often disabled) | 250, 252 or 5xx |
EXPN | Expand a mailing list (almost always disabled) | 250 or 5xx |
NOOP | Do nothing, keep the connection alive | 250 |
HELP | Return help text | 211 or 214 |
QUIT | End the session | 221 |
STARTTLS | Upgrade the connection to TLS (RFC 3207) | 220 |
AUTH mechanism | Authenticate the client (RFC 4954) | 235 |
A complete SMTP session
Lines starting with C: come from the client and S: from the server. The server banner arrives first, before the client says anything.
S: 220 mx.example.org ESMTP ready
C: EHLO client.example.com
S: 250-mx.example.org greets client.example.com
S: 250-SIZE 52428800
S: 250-8BITMIME
S: 250-STARTTLS
S: 250 ENHANCEDSTATUSCODES
C: MAIL FROM:<[email protected]>
S: 250 2.1.0 Sender ok
C: RCPT TO:<[email protected]>
S: 250 2.1.5 Recipient ok
C: DATA
S: 354 End data with <CR><LF>.<CR><LF>
C: From: Bob <[email protected]>
C: To: Alice <[email protected]>
C: Subject: Test
C: Date: Thu, 25 Jul 2024 10:00:00 +0000
C: Message-ID: <[email protected]>
C:
C: Hello Alice.
C: .
S: 250 2.0.0 Queued as 4F2A91
C: QUIT
S: 221 2.0.0 ByeHELO and EHLO
Every session starts with one of these. The argument should be the fully qualified domain name of the sending host, or an address literal such as [203.0.113.25] when it has no name. Modern clients always try EHLO first. A server that supports extensions answers with a multi-line 250 reply: every line except the last uses 250- and the last uses 250 with a space. Each line after the greeting advertises one extension. A server that does not understand EHLO returns a 5xx and the client falls back to HELO.
The name you send matters for deliverability. Receiving servers compare it with your reverse DNS and may reject or score down names that are not fully qualified or do not resolve. See reverse DNS and the HELO name and HELO and EHLO errors.
MAIL FROM and RCPT TO: the envelope
MAIL FROM opens a mail transaction and gives the envelope sender, also called the return path. Bounces and delivery status notifications go to this address, not to the From: header. A bounce message uses an empty sender, written MAIL FROM:<>, which prevents bounce loops. Extensions add parameters on the same line, for example MAIL FROM:<[email protected]> SIZE=12345 when the server advertised SIZE.
RCPT TO adds one recipient per command. A transaction can carry many recipients, and the server answers each separately, so some can be accepted while others are refused. RFC 5321 requires servers to accept at least 100 recipients per message, although many apply lower limits in practice. Envelope addresses are independent of the To, Cc and Bcc headers, which is how blind copies work. Read more in envelope from and Return-Path versus From.
DATA, dot stuffing and the end of message
After DATA the server replies 354 and from then on treats everything as message content until it sees a line containing only a period, that is CRLF.CRLF. The content is the headers, a blank line and the body, in the format of RFC 5322. The server only gives its real verdict after the final dot: the 250 means it accepted responsibility for delivery, and a 4xx or 5xx at that point means the message was refused after content checks such as spam filtering or DKIM policy.
To stop body text from ending the message early, any body line that begins with a period gets an extra leading period when sent and loses it on receipt. This is called dot stuffing, and libraries do it for you. If you write raw sessions by hand you only need to remember it for lines that start with a dot. Lines in the message must not exceed 998 characters excluding the line ending, which is a limit of the message format, not of the command set.
RSET, NOOP, VRFY, EXPN, HELP and QUIT
- RSET discards the sender, recipients and any data so far and returns the session to the state after
EHLO. Use it to start a new transaction on the same connection after an error. - NOOP does nothing except draw a 250. Pools use it to check that a connection is still alive. See SMTP connection pooling.
- VRFY asks whether an address is valid. Most public servers answer 252 (cannot verify, will accept) or refuse the command because it helps address harvesting. Never depend on it. See VRFY.
- EXPN asks for the members of a list and is disabled almost everywhere for the same reason.
- HELP returns free-form help text and is rarely useful.
- QUIT asks the server to close the connection; it answers 221 and closes. Always send it, rather than dropping the TCP connection, so queued mail is committed cleanly.
Extension commands: STARTTLS and AUTH
The extension list from EHLO tells you which extra commands are allowed. STARTTLS (RFC 3207) switches an existing plain connection to TLS. After a 220 reply both sides negotiate TLS, then the client must send EHLO again because everything learned before the upgrade is discarded. AUTH (RFC 4954) authenticates the client with a mechanism the server advertised, for example AUTH PLAIN. Both are normal on submission port 587, while port 465 uses TLS from the first byte, so there is no STARTTLS command at all. See which port to use and SMTP extensions.
| EHLO keyword | What it enables |
|---|---|
STARTTLS | Upgrade to TLS on the same connection |
AUTH PLAIN LOGIN | The authentication mechanisms the server accepts |
SIZE n | Maximum message size in bytes, and the SIZE= parameter on MAIL FROM |
8BITMIME | Allows 8-bit message bodies |
PIPELINING | Lets the client send several commands without waiting for each reply |
SMTPUTF8 | Allows internationalised addresses and headers |
ENHANCEDSTATUSCODES | Adds codes such as 5.1.1 after the three digit reply |
CHUNKING | Sends the message with BDAT instead of DATA |
Command order and what breaks it
- Connect and wait for the 220 banner.
- Send
EHLO. If advertised and required, runSTARTTLSand sendEHLOagain. - Authenticate with
AUTHif the server needs it. - Send
MAIL FROM, then one or moreRCPT TO. - Send
DATA, the message, and the lone dot. - Either start another transaction with
MAIL FROMor sendQUIT.
Servers enforce this order. Sending RCPT TO before MAIL FROM returns 503 (bad sequence of commands). A transaction cannot start before a successful greeting, and a client that sends nothing for several minutes is typically disconnected with a 421.
Troubleshooting common command errors
| Reply | Meaning | Fix |
|---|---|---|
| 500 or 502 | Command not recognised or not implemented | Check spelling; use only commands from the EHLO list |
| 501 | Syntax error in parameters, often missing angle brackets | Write MAIL FROM:<[email protected]> exactly |
| 503 | Commands in the wrong order | Follow the sequence above; use RSET to start clean |
| 530 or 5.7.0 | Authentication or TLS required first | Run STARTTLS and AUTH before MAIL FROM |
| 550 on RCPT TO | Recipient rejected | Check the address; see 550 errors |
| 552 | Message exceeds size limit | Compare with the advertised SIZE value |
| 554 after the dot | Content or policy rejection | Read the text of the reply for the reason |
| 421 | Server is closing the channel, try later | Retry with backoff. See 421 timeouts |
Commands are case insensitive, but the local part of an address may be case sensitive on the receiving side, so preserve it exactly as given. To watch these commands live on your own server, see testing an SMTP server from the command line.
When to use this knowledge
You rarely type SMTP commands in production, since libraries send them for you, but the command trace in a log is the clearest evidence of what went wrong. Read the last command and the reply to it, and you usually know whether the problem is the sender, the recipient, the content or the connection. Next, learn the reply codes in SMTP response codes.
Send through a relay that handles the protocol for you Use authenticated SMTP on ports 587 or 465 and keep full delivery logs for every command and reply.
References and further reading
Frequently asked questions
What is the difference between HELO and EHLO?
EHLO is the extended greeting that makes the server list its supported extensions such as STARTTLS, AUTH and SIZE. HELO is the original command with no extension negotiation, used only as a fallback.
What does MAIL FROM do in SMTP?
It starts a mail transaction and sets the envelope sender, the address that receives bounces. It is separate from the From header the recipient sees.
How does SMTP know the message has ended?
After DATA, the server treats everything as content until it sees a line with only a period. The server replies 250 once it accepts the message.
Do I have to send QUIT?
You should. QUIT ends the session cleanly with a 221 reply, while dropping the connection can leave the server unsure whether the transaction finished.
Why do I get 503 bad sequence of commands?
You sent a command out of order, for example RCPT TO before MAIL FROM or DATA before any recipient was accepted. Send RSET and repeat the commands in the correct order.