My Notes from dealing with Email & SMTP
Context I am currently working on a new simple email hosting product targeting solopreneurs and hobbyists and have been building out my own smtp server for this purpose.
These are just some of my notes.
Mail Headers
-
When processing email coming into your smtp server,
MAIL FROM
,RCPT TO
do not form the content of your incoming mail. Instead they are used by the receiving mail server just for the purpose of mail processing. -
An incoming e-mail is made up of 3 sections which are received by your mail server.
- MAIL FROM
- RCPT TO
- DATA
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
DATA
From: "Marketing Team" <[email protected]>
To: "User" <[email protected]>
Subject: Monthly Newsletter
Hello, this is a test email.
.
-
When we talk about email headers and body, these are things captured under the
DATA
section. The content ofDATA
is what makes up your email message. -
DATA
is made up of 2 sections- Header
- Header is everything before the first linebreak (eg. up to Subject in the above example)
- Body
- Everything else!
- Header
-
When we store emails, we’re only interested in
DATA
. The receiving mail server’s role is just to prepend certain email headers prior to storage. -
The mail server needs to only do the following prior to storage:
-
Prepend a new header
Return-Path
in the format ofReturn-Path: <email>
- The
<email>
consists of the e-mail address that was captured in theMAIL FROM
section. MAIL FROM
can be empty if its a bounced message, if that is the case,Return-Path
should still be prepended with a blank<>
as a value
- The
-
Prepend
Received
header to indicate the DateTime our mail server received the mail.- The
Received
header should contain remote server of the sending mail server with a reverse lookup - There can be multiple
Received
headers in the same email. But there is an order to it with the latest received mail server being at the top (in this case, our server needs to populate the firstReceived
header)
- The
-
Prepend any other post-processing custom headers 2. eg. spf-check header
Received-SPF
3. spam scores etc
-
-
The
Date
header is an RFC requirement but its usually sent by the sender. If thisDate
is missing, your mail server needs to add it in accordance to the RFC5322 format (Fri, 21 Mar 2025 12:34:56 +0000
) -
To
header can be blank and it is not to be confused with being the same asRCPT TO
. This can be blank because the mail may not haveTo
but haveCc
orBcc
records.
Summary
From the SMTP server perspective, we really only need to care about 3 things
- Is
MAIL FROM
a bounce? If it is, make sureReturn-Path
is correctly addressed - Is my mail server the recipient in
RCPT TO
. If its not, its ok to drop it. There’s no need to handle mail to email addresses that you are not expecting to. - We generally do not modify any existing headers in the
Header
but we may need to add/prepend new headers as necessary.