What a mail server is and how it works
A mail server is not one program but a set of roles described in RFC 5598: accepting messages from users, relaying them between servers, delivering them to a mailbox, and giving access to the mailbox over IMAP. In this reference, Postfix, Dovecot and Rspamd fill those roles.
Roles and programs
The mail user agent (MUA) is the user’s program: Thunderbird, a mobile app or a web interface. The server performs all the other roles.
| Role | What it does | Covered in the stack by |
|---|---|---|
| Accepting mail from your own users (mail submission agent, MSA) | accepts a message on ports 587 and 465 after the user logs in | Postfix; Dovecot checks the login (SASL) |
| Relaying between servers (mail transfer agent, MTA) | holds the queue, finds the recipient’s server and hands the message over by SMTP; accepts other servers’ mail on port 25 | Postfix |
| Delivery to a mailbox (mail delivery agent, MDA) | receives the message from Postfix over LMTP and puts it in the mailbox | Dovecot |
| Storage and access | stores messages in Maildir format and serves them to clients over IMAP | Dovecot |
| Filter | checks SPF, DKIM and DMARC, scores spam, signs outgoing messages with DKIM | Rspamd, through the milter protocol |
DNS and TLS are just as much a part of the system: DNS says where to deliver messages and whom to trust, and TLS encrypts the connections.
The path of a message from sender to mailbox
The client hands the message to its own server on port 587 or 465 and authenticates with a login. From there the server finds the recipient itself: the message waits in the queue while Postfix looks up the recipient domain’s MX record in DNS and connects to the server it names on port 25.
- The client connects to port 587 or 465 of its server and logs in (MSA).
- Postfix accepts the message and puts it in the outgoing MTA queue.
- Postfix looks up the recipient domain’s MX record in DNS.
- It connects to the server it finds on port 25.
- The recipient’s Postfix passes the message to Rspamd: SPF, DKIM, DMARC and anti-spam.
- If the checks pass, Postfix hands the message to Dovecot over LMTP.
- Dovecot puts the message in Maildir.
- The recipient’s client collects the message over IMAP.
Envelope and message
SMTP transfers an object in two parts (RFC 5321): the envelope and the content. The envelope consists of the MAIL FROM (sender address) and RCPT TO (recipient address) commands, and the server uses them to decide where to deliver the message. The message is the headers and body defined by RFC 5322, including the From: and To: that a person sees. The two sets of addresses can differ, for example with mailing lists and blind copies. SPF and DMARC are built on this: SPF checks the envelope address, and DMARC compares the domain in From: with the domain that passed SPF (from the envelope) or DKIM.
The servers’ dialogue looks like this: each client command gets a reply with a three-digit code. 2xx means success, 3xx means go on, 4xx is a temporary error and 5xx is a permanent one. The codes are covered in the “Common errors and diagnostics” section.
S: 220 mx.example.org ESMTP PostfixC: EHLO mail.example.comS: 250-mx.example.orgS: ...C: MAIL FROM:<anna@example.com>S: 250 2.1.0 OkC: RCPT TO:<bob@example.org>S: 250 2.1.5 OkC: DATAS: 354 End data with <CR><LF>.<CR><LF>C: From: Anna <anna@example.com>C: To: Bob <bob@example.org>C: Subject: HelloC:C: Hi, Bob!C: .S: 250 2.0.0 Ok: queued as 4F3A21BC: QUITS: 221 2.0.0 ByeIMAP and POP3
A client reads messages over IMAP or POP3. IMAP keeps messages and folders on the server, so a phone and a computer see the same mailbox; POP3 downloads messages from the server. This reference sets up IMAP. IMAP cannot send mail: that is what ports 587 and 465 are for.
What you need for your own server
- A domain with managed DNS: you must be able to create MX, TXT and PTR records.
- A static public IP address with a PTR record, which your hosting provider issues.
- Port 25 open in both directions.
- Time to build the reputation of a new IP address: the first messages may land in spam (see the “Deliverability and reputation” section).
What to know beforehand
- DNS: the “TCP/IP and DNS” reference, section “DNS records”.
- TLS and certificates: the “TLS and PKI” reference.
- Basic Linux and systemd: packages, services, logs.
Section sources
the section is written from these pages- Internet Mail Architecture (RFC 5598)rfc-editor.org/rfc/rfc5598.html(opens in a new tab)
- Simple Mail Transfer Protocol, Basic Structure (RFC 5321)rfc-editor.org/rfc/rfc5321.html(opens in a new tab)
- Postfix Architecture Overviewpostfix.org/OVERVIEW.html(opens in a new tab)
- Internet Message Access Protocol, IMAP4rev2 (RFC 9051)rfc-editor.org/rfc/rfc9051.html(opens in a new tab)