Web and appsApplicationRFC 5321

SMTP: sending an email

The short text conversation mail servers use to hand over an email.

Ports and use: TCP 25 (server to server), 587 (from your mail app)

In one sentenceSMTP is a polite back and forth: say hello, name the sender and receiver, send the message, say goodbye.

Think of it like thisIt is a courier at a reception desk: "I have a parcel from Asha for Ravi" - "Go ahead" - "Here it is" - "Received".

Watch it happen

11 steps and 11 byte-by-byte decodes. Use the buttons or the arrow keys. Hover or focus a field to find its bytes.

  1. Sending server
  2. Receiving server
  1. 220 greetingReceiving server to Sending server
  2. EHLOSending server to Receiving server
  3. 250 capabilitiesReceiving server to Sending server
  4. MAIL FROMSending server to Receiving server
  5. 250 OKReceiving server to Sending server
  6. RCPT TOSending server to Receiving server
  7. 250 OKReceiving server to Sending server
  8. DATASending server to Receiving server
  9. 354 go aheadReceiving server to Sending server
  10. The messageSending server to Receiving server
  11. 250 queued, then QUITReceiving server to Sending server

Step 1 of 11 220 greeting

Receiving server → Sending server

After the TCP connection opens, the receiving server speaks first and says it is ready.

Server greeting 34 bytes

220 mail.example.com ESMTP ready
FieldTextWhat it means
Greeting220 mail.example.com ESMTP ready220 means "ready". It names the server and the protocol.

Step 2 of 11 EHLO

Sending server → Receiving server

The sender introduces itself. EHLO asks the server to list its extra features.

Client hello 25 bytes

EHLO laptop.example.org
FieldTextWhat it means
CommandEHLO laptop.example.orgEHLO followed by the sender's name.

Step 3 of 11 250 capabilities

Receiving server → Sending server

The server replies with a list. Lines that start with 250 and a dash mean "more lines follow".

Server features 95 bytes

250-mail.example.com greets laptop.example.org
250-SIZE 35882577
250-STARTTLS
250 8BITMIME
FieldTextWhat it means
Line 1250-mail.example.com greets laptop.example.orgThe first line names both sides.
Line 2250-SIZE 35882577Largest message accepted, in bytes.
Line 3250-STARTTLSThe server can switch to an encrypted connection. Modern senders use it before going on.
Line 4250 8BITMIMEThe final line uses 250 and a space.

Step 4 of 11 MAIL FROM

Sending server → Receiving server

The sender names the return address, called the envelope sender.

Sender 30 bytes

MAIL FROM:<[email protected]>
FieldTextWhat it means
CommandMAIL FROM:<[email protected]>Who is sending, and where to return errors.

Step 5 of 11 250 OK

Receiving server → Sending server

The server accepts the sender.

Reply 21 bytes

250 2.1.0 Sender OK
FieldTextWhat it means
Reply250 2.1.0 Sender OKA 250 reply means success. The 2.1.0 is a more detailed status.

Step 6 of 11 RCPT TO

Sending server → Receiving server

The sender names who the message is for. This can repeat for each recipient.

Recipient 28 bytes

RCPT TO:<[email protected]>
FieldTextWhat it means
CommandRCPT TO:<[email protected]>Where to deliver the message.

Step 7 of 11 250 OK

Receiving server → Sending server

The server agrees to accept mail for that address.

Reply 24 bytes

250 2.1.5 Recipient OK
FieldTextWhat it means
Reply250 2.1.5 Recipient OKThe server will take this message.

Step 8 of 11 DATA

Sending server → Receiving server

The sender says it is ready to send the actual message.

Command 6 bytes

DATA
FieldTextWhat it means
CommandDATAThe message follows.

Step 9 of 11 354 go ahead

Receiving server → Sending server

The server says to send the message and finish with a line containing only a full stop.

Reply 46 bytes

354 Start mail input; end with <CRLF>.<CRLF>
FieldTextWhat it means
Reply354 Start mail input; end with <CRLF>.<CRLF>354 means "continue".

Step 10 of 11 The message

Sending server → Receiving server

Headers, a blank line, the body, and a single full stop on its own line to mark the end.

Message 96 bytes

From: Asha <[email protected]>
To: Ravi <[email protected]>
Subject: Hello

See you at 5.
.
FieldTextWhat it means
Header: FromFrom: Asha <[email protected]>The name shown to the reader. It can differ from the envelope sender, which is why spoofing is possible.
Header: ToTo: Ravi <[email protected]>The names shown to the reader.
Header: SubjectSubject: HelloThe subject line.
Blank lineSeparates the headers from the body.
BodySee you at 5.The text of the email.
End marker.A line with a single full stop ends the message.

Step 11 of 11 250 queued, then QUIT

Receiving server → Sending server

The server accepts responsibility for delivery. The sender then sends QUIT and the server answers 221 and closes.

Reply 28 bytes

250 2.0.0 Queued as 4F2A91
FieldTextWhat it means
Reply250 2.0.0 Queued as 4F2A91The server has stored the message and gave it an ID you can use to trace it.

Where you meet it

  • Every outgoing email from apps, websites and mail programs.
  • Password reset and notification emails sent by services.
  • Mail passing between servers on its way to your inbox.

Common mistakes

  • Assuming the From header proves who sent the mail. Use SPF, DKIM and DMARC to check.
  • Sending over port 25 from a home network, which many providers block.
  • Skipping STARTTLS, so mail travels as readable text.
  • Treating a 250 reply as "delivered". It only means "accepted for delivery".

Try it yourself

Run these on your own computer and compare the output with the lesson above.

  • openssl s_client -starttls smtp -connect smtp.example.com:587Opens an encrypted SMTP session you can type commands into. Replace the host with a server you are allowed to test.
  • dig example.com MX +shortShows which servers receive mail for a domain.

Back to Network Lab

Addresses and values in the examples are made up for teaching (the 203.0.113.x range is reserved for documentation). Real traffic carries more options and details. Check the standard named above for the full rules.