Skip to content
Sign inGet all access
Chapter 3·16 minFree

The Message Itself

Headers, body, and the envelope that is not either.

Everything after DATA is the message. None of it is the envelope.

Headers, blank line, body

The same shape as HTTP — Name: value lines, one empty line, then content:

EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: [email protected]
To: [email protected]
Subject: Shape

This is the body.
.
QUIT
Output250 Ok: queued as <id>@mailhog.example221 Bye

The four a client shows you

From, To, Subject, Date — and a client makes its own decisions about the rest.

None of the four is required. Chapter 2’s examples had only Subject: and were queued without complaint.

Two senders

MAIL FROM: is the envelope sender; From: is a header. They are unrelated:

EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: [email protected]
Subject: TwoSenders

body
.
QUIT
Output221 Bye
curl -s localhost:8025/api/v2/messages | python3 -c '
import json, sys
d = json.load(sys.stdin)
m = [x for x in d["items"]
     if "TwoSenders" in str(x["Content"]["Headers"].get("Subject", ""))][0]
h = m["Content"]["Headers"]
print("From:        ", h["From"][0])
print("Return-Path: ", h["Return-Path"][0])'
OutputFrom: [email protected]Return-Path: <[email protected]>

Return-Path was stamped from the envelope. Bounces go to bounces@; the reader sees noreply@.

Two recipient lists

RCPT TO: routes the message; To: is decoration. Send to two, name one:

EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: [email protected]
To: [email protected]
Subject: BccTest

body
.
QUIT
Output221 Bye
curl -s localhost:8025/api/v2/messages | python3 -c '
import json, sys
d = json.load(sys.stdin)
m = [x for x in d["items"]
     if "BccTest" in str(x["Content"]["Headers"].get("Subject", ""))][0]
print("envelope rcpt:", [r["Mailbox"] + "@" + r["Domain"] for r in m["To"]])
print("header To:    ", m["Content"]["Headers"]["To"])'
Outputenvelope rcpt: ['[email protected]', '[email protected]']header To: ['[email protected]']

That is Bcc — a RCPT TO: with no matching header line, and no Bcc header anywhere.

A Bcc recipient cannot see the others because the information was never in the message — your client strips the header before sending.

Spoofing, demonstrated

Nothing checks that the two senders agree:

EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: "Your Bank" <[email protected]>
To: [email protected]
Subject: Spoofed

Please confirm your details.
.
QUIT
Output250 Ok: queued as <id>@mailhog.example221 Bye
curl -s localhost:8025/api/v2/messages | python3 -c '
import json, sys
d = json.load(sys.stdin)
m = [x for x in d["items"]
     if "Spoofed" in str(x["Content"]["Headers"].get("Subject", ""))][0]
print("envelope from:", m["From"]["Mailbox"] + "@" + m["From"]["Domain"])
print("header From:  ", m["Content"]["Headers"]["From"][0])'
Outputenvelope from: [email protected]header From: "Your Bank" <[email protected]>

Message-ID and threading

A client supplies its own, and the server keeps it:

EHLO test.local
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
From: [email protected]
Subject: Re: Thread
Message-ID: <[email protected]>
In-Reply-To: <[email protected]>
References: <[email protected]> <[email protected]>

reply
.
QUIT
Output221 Bye
curl -s localhost:8025/api/v2/messages | python3 -c '
import json, sys
d = json.load(sys.stdin)
m = [x for x in d["items"]
     if "Thread" in str(x["Content"]["Headers"].get("Subject", ""))][0]
h = m["Content"]["Headers"]
for k in ("Message-ID", "In-Reply-To", "References"):
    print(k + ":", h[k][0])'
OutputMessage-ID: <[email protected]>In-Reply-To: <[email protected]>References: <[email protected]> <[email protected]>

In-Reply-To names the parent, References the chain back to the root. Clients rebuild the tree from those two.

Received headers

Each hop adds one at the top, so the list reads newest-first:

curl -s localhost:8025/api/v2/messages | python3 -c '
import json, sys
d = json.load(sys.stdin)
m = [x for x in d["items"]
     if "Thread" in str(x["Content"]["Headers"].get("Subject", ""))][0]
print(m["Content"]["Headers"]["Received"][0].split("\n")[0].strip())'
Outputfrom test.local by mailhog.example (MailHog)

One hop, because MailHog is the only server. A real delivery stacks four or five, read bottom to top.

Each one carries a timestamp, so the gap between two is where the message waited. That is how you find a slow hop.

Return-Path and Reply-To

Three addresses can appear on one message, answering three questions:

Header Answers Set by
Return-Path where do bounces go the receiving server, from the envelope
From who wrote this the sender
Reply-To where should a reply go the sender

Headers the server adds

You wrote two headers. Four came out:

curl -s localhost:8025/api/v2/messages | python3 -c '
import json, sys
d = json.load(sys.stdin)
m = [x for x in d["items"]
     if "TwoSenders" in str(x["Content"]["Headers"].get("Subject", ""))][0]
print("\n".join(sorted(m["Content"]["Headers"])))'
OutputFromMessage-IDReceivedReturn-PathSubject

Three were added in transit. The first two you can never set; the third you can, and the server left yours alone above.