Skip to content
Visual explainer 1h 30m to learn 3 steps

Email: SMTP, IMAP & authentication

Explain how mail is relayed with SMTP and protected by SPF, DKIM and DMARC.

By Invicly Tech, , in Computer Networking. About 11 minutes to read.

Topic 30 of 51

In short

Email is relayed between servers with SMTP, which looks up the recipient domain's MX records to find where to deliver. Users read mail with IMAP, which keeps messages on the server, or the older POP3. SPF, DKIM and DMARC records in DNS let receivers check that mail really comes from the domain it claims.

Explained simply

Email works much like the postal service. You hand your letter to your local post office (your mail provider), which finds the post office of the recipient's town and delivers it there. The recipient later visits their mailbox to read it. Different protocols handle each of those steps.

SMTP (Simple Mail Transfer Protocol) is the delivery truck. Your mail app uses it to hand a message to your provider, and providers use it to pass messages to each other. To find where to deliver, the sending server looks up the MX (mail exchanger) records of the recipient's domain in DNS.

Reading mail is a separate job. IMAP keeps messages on the server and syncs folders and read status across your phone, laptop and webmail. POP3 is older: it downloads messages to one device, and usually deletes them from the server.

SMTP was designed in a friendlier era, so anyone can claim to send mail from any address. Three DNS-based checks fight this: SPF lists which servers may send for a domain, DKIM adds a digital signature, and DMARC tells receivers what to do when checks fail. Big providers now require all three from bulk senders.

Press Play on a diagram to walk through it one step at a time.

FlowPath of an email
  1. 1
    Mail client

    Submits via SMTP on port 587 with auth.

  2. 2
    Sender's server

    Looks up MX for the recipient domain.

  3. 3
    SMTP relay

    Delivers to the recipient MX on port 25.

  4. 4
    Receiver checks

    SPF, DKIM, DMARC, spam filters.

  5. 5
    Mailbox

    Recipient reads via IMAP on 993.

MapEmail protocols and their ports
Email
  • Sending (SMTP)
    • 25: server to server
    • 587: submission, STARTTLS
    • 465: submission, implicit TLS
  • Reading
    • IMAP 143 and 993
    • POP3 110 and 995
  • Authentication (DNS)
    • SPF TXT
    • DKIM at selector._domainkey
    • DMARC at _dmarc
  • Transport security
    • STARTTLS
    • MTA-STS
    • DANE

In depth

An SMTP conversation, line by line

SMTP is a text protocol of commands and three-digit replies. The server greets with 220. The client says EHLO with its own name, and the server lists extensions such as STARTTLS, AUTH and SIZE. On port 587 the client upgrades with STARTTLS and logs in with AUTH. Then come the envelope commands: MAIL FROM gives the return address for bounces, and RCPT TO gives each recipient, each confirmed with 250. DATA gets a 354 reply, then the client sends the message headers and body and ends with a line holding a single dot. The server answers 250 with a queue ID, and QUIT closes with 221.

Replies starting with 4 are temporary failures (try again later, for example 421 or 451), and 5 are permanent (550 mailbox unavailable). Sending servers keep 4xx mail in a queue and retry for days before bouncing.

SequenceSending one message with SMTP
Sending server
Receiving MX
  1. 1220 mx.example.org ESMTP ready
    Receiving MX to Sending server: 220 mx.example.org ESMTP ready
  2. 2EHLO mail.example.com
    Sending server to Receiving MX: EHLO mail.example.com
  3. 3250-STARTTLS, 250 SIZE ...
    Receiving MX to Sending server: 250-STARTTLS, 250 SIZE ...
  4. 4MAIL FROM with sender alice@example.com
    Sending server to Receiving MX: MAIL FROM with sender alice@example.com
  5. 5RCPT TO with recipient bob@example.org
    Sending server to Receiving MX: RCPT TO with recipient bob@example.org
  6. 6DATA, headers and body, then a lone dot
    Sending server to Receiving MX: DATA, headers and body, then a lone dot
  7. 7250 OK queued as 4F2A1C
    Receiving MX to Sending server: 250 OK queued as 4F2A1C
  8. 8QUIT, reply 221
    Sending server to Receiving MX: QUIT, reply 221

Envelope versus header: two kinds of From

A message has two sets of addresses. The envelope (MAIL FROM and RCPT TO) is what servers use for delivery and bounces; the recipient normally never sees it, though it is saved as the Return-Path header on arrival. The header From is what the user sees in their mail app. They do not have to match, which is exactly how mailing lists and forwarding work, and also how spoofing works.

This matters for authentication. SPF checks the envelope domain (MAIL FROM), not the visible From. DKIM signs the message with a domain named in the d= tag of the signature, which can be any domain. DMARC is the piece that ties them back to what the user sees: it passes only if SPF or DKIM passes for a domain that aligns with (matches) the header From domain.

CompareWhat each check looks at
SPF
  • Checks the sending IP
  • Against the envelope MAIL FROM domain
  • Breaks when mail is forwarded
  • TXT record at the domain
DKIM
  • Checks a signature over headers and body
  • Domain from the d= tag
  • Survives simple forwarding
  • Key at selector._domainkey
DMARC
  • Requires SPF or DKIM to pass and align
  • Aligned with the visible From
  • Sets a policy: none, quarantine, reject
  • TXT record at _dmarc

Writing SPF, DKIM and DMARC records

SPF is one TXT record at the domain, such as v=spf1 include:_spf.google.com ip4:203.0.113.25 -all. Mechanisms list allowed senders, and the final all says what to do with the rest: -all fails them, ~all marks them as soft fail. SPF allows at most 10 DNS lookups (each include, a, mx and redirect counts), and going over makes SPF fail with a permanent error, a common problem when many services are added. A domain must have only one SPF record.

DKIM keys live at selector._domainkey.domain, for example google._domainkey.example.com, as TXT v=DKIM1; k=rsa; p=(the public key). The selector lets you run several keys and rotate them. Use 2048-bit RSA keys.

DMARC lives at _dmarc.domain: v=DMARC1; p=none; rua=mailto:dmarc@example.com. Start with p=none to collect aggregate reports, fix every legitimate sender you find, then move to p=quarantine and finally p=reject.

  • Only one SPF TXT record per domain
  • SPF has a 10-lookup limit
  • DKIM: selector._domainkey.example.com
  • DMARC: _dmarc.example.com, start at p=none

Ports and encryption

Port 25 is for server-to-server relay. Many home and cloud networks block outgoing port 25 to stop spam from infected machines, which is why your app should never send mail directly from a laptop or new VM; use your provider's submission service or a mail API. Port 587 is for message submission by clients: plain connection upgraded with STARTTLS, then AUTH. Port 465 is submission with implicit TLS (TLS from the first byte), and current guidance (RFC 8314) prefers it.

For reading, IMAP uses 143 (STARTTLS) and 993 (implicit TLS); POP3 uses 110 and 995. Between servers, STARTTLS on port 25 is opportunistic: an attacker could strip it. MTA-STS (a policy published over HTTPS) and DANE (TLSA records protected by DNSSEC) let a domain demand encrypted delivery.

Why good mail still lands in spam

Authentication proves who sent a message, not that it is wanted. Receivers also score sender reputation (of the IP and the domain), complaint rates, content and engagement. Since 2024, Gmail and Yahoo require bulk senders (over about 5,000 messages a day to Gmail) to have SPF, DKIM and a DMARC record, to align the From domain, to support one-click unsubscribe, and to keep spam complaints below 0.3%.

Other checks: the sending IP should have a PTR (reverse DNS) record whose name resolves back to the same IP. A brand-new IP or domain needs warming up with small volumes first. Forwarding breaks SPF, so DKIM is the check that usually saves forwarded mail; ARC headers help receivers trust intermediaries.

The Authentication-Results header added by the receiver shows the verdicts, such as spf=pass dkim=pass dmarc=pass. Reading it is the first step for any deliverability problem.

Learn it in 3 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Trace mail from client to recipient MX~30m
  2. 2
    Compare SMTP, IMAP and POP3~30m
  3. 3
    Explain SPF, DKIM and DMARC records~30m

Key points

  • SPF lists servers allowed to send for a domain.
  • DKIM adds a signature verified with a public key in DNS.
  • DMARC tells receivers what to do when SPF or DKIM fail, and where to report.

Key terms

Tap a card to flip it. 0/4 known

Examples

bash
dig gmail.com MX +short
dig gmail.com TXT +short | grep spf
dig _dmarc.gmail.com TXT +short

Auditing a domain's mail authentication

Marketing says their newsletters go to spam. Step 1: check MX, SPF, DMARC and the DKIM key for the selector their mail service uses (you can find the selector in the s= tag of a DKIM-Signature header). Step 2: count SPF lookups; here three includes plus mx is fine. Step 3: send a test to a Gmail account, open Show original, and read the SPF, DKIM and DMARC verdicts.

bash
d=example.com
dig $d MX +short
dig $d TXT +short | grep spf1
# "v=spf1 mx include:_spf.google.com include:sendgrid.net -all"
dig _dmarc.$d TXT +short
# "v=DMARC1; p=quarantine; rua=mailto:dmarc@example.com"
dig s1._domainkey.$d TXT +short
# "k=rsa; p=MIIBIjANBgkqh..."

Testing SMTP submission by hand

An app cannot send mail through the company server. Step 1: check that port 587 is reachable and that STARTTLS works with openssl. Step 2: type EHLO and see if AUTH is offered after TLS. Step 3: if the connection times out, a firewall blocks the port; if AUTH fails with 535, the credentials are wrong. The swaks tool can run the whole test in one line.

bash
openssl s_client -starttls smtp -connect smtp.example.com:587 -crlf -quiet
# then type:
# EHLO test.local
# 250-AUTH PLAIN LOGIN   <- auth is offered
# QUIT

swaks --to bob@example.org --from app@example.com \
  --server smtp.example.com:587 --tls \
  --auth LOGIN --auth-user app@example.com

Quick reference

  • SMTP relay 25; submission 587 (STARTTLS) or 465 (implicit TLS)
  • IMAP 143/993; POP3 110/995
  • SMTP flow: EHLO, MAIL FROM, RCPT TO, DATA, lone dot, QUIT
  • 4xx = temporary, retry later; 5xx = permanent failure
  • SPF checks envelope MAIL FROM; DMARC needs alignment with the visible From
  • SPF: one record per domain, at most 10 DNS lookups
  • DMARC rollout: p=none, then quarantine, then reject
  • Read Authentication-Results in a received message to see spf, dkim and dmarc verdicts

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which domain does SPF actually check?

Question 2

2A sending server gets 451 from the receiver. What should it do?

Question 3

3Which check is most likely to survive when a message is forwarded by a mailing list that does not change it?

Question 4

4What is the safest first DMARC policy for a domain that has never had one?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Look up the MX, SPF and DMARC records of two well-known domains and compare their DMARC policies.

  2. Exercise 2medium

    Open the full headers of an email in your inbox and trace its path and authentication results.

  3. Exercise 3hard

    A domain uses Google Workspace, Mailchimp, Zendesk and its own server 203.0.113.25, and SPF now fails with permerror. Explain why and write a fixed plan.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I use IMAP or POP3?
Use IMAP for almost everything, because it keeps mail on the server and syncs across devices. POP3 only makes sense if you want mail downloaded to a single machine.
Why can't my cloud server send email directly?
Most cloud providers block outgoing port 25 by default to prevent spam, and new IPs have no reputation. Send through an email service or your provider's relay on port 587 or 465.
Do I need DMARC if I already have SPF and DKIM?
Yes. Without DMARC nothing links those checks to the visible From address, and receivers have no policy for failures. Gmail and Yahoo also require it for bulk senders.

Go deeper

ShareXLinkedInWhatsApp