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 TOdo 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
DATAsection. The content ofDATAis what makes up your email message. -
DATAis 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-Pathin the format ofReturn-Path: <email>- The
<email>consists of the e-mail address that was captured in theMAIL FROMsection. MAIL FROMcan be empty if its a bounced message, if that is the case,Return-Pathshould still be prepended with a blank<>as a value
- The
-
Prepend
Receivedheader to indicate the DateTime our mail server received the mail.- The
Receivedheader should contain remote server of the sending mail server with a reverse lookup - There can be multiple
Receivedheaders 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 firstReceivedheader)
- The
-
Prepend any other post-processing custom headers 2. eg. spf-check header
Received-SPF3. spam scores etc
-
-
The
Dateheader is an RFC requirement but its usually sent by the sender. If thisDateis missing, your mail server needs to add it in accordance to the RFC5322 format (Fri, 21 Mar 2025 12:34:56 +0000) -
Toheader 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 haveTobut haveCcorBccrecords.
Summary
From the SMTP server perspective, we really only need to care about 3 things
- Is
MAIL FROMa bounce? If it is, make sureReturn-Pathis 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
Headerbut we may need to add/prepend new headers as necessary.