Skip to main content
Code Matrix

The Journey of an Email: From “Send” to Inbox | How Email Works

Clicking Send triggers a relay across SMTP servers, DNS MX records, SPF/DKIM/DMARC cryptographic checks, and IMAP sync. Trace every step before an email lands.

Published 2026-08-11Updated 2026-10-109 min read753 words
Code Matrix Video WalkthroughOpen on YouTube
Table of Contents (6 sections)

Email feels so simple that we take it for granted: write a message to alex@example.org, click Send, and moments later it chimes inside their inbox on the opposite side of the planet.

Unlike instant messaging apps owned by a single company, email is a federated open protocol created decades before modern smartphones—yet still delivering hundreds of billions of messages daily through a relay of SMTP servers, DNS MX lookups, and cryptographic spam defenses.

1. Why Clicking "Send" Does Not Talk Directly to the Recipient

When you email alex@example.org from a @gmail.com account, your phone does not connect to Alex's phone. Instead, it hands the message off like an international postal relay:

  1. Submission (SMTP Port 587 / 465): Your mail app or web client passes the formatted MIME message (headers, UTF-8 body, Base64-encoded attachments) over TLS to your Outgoing Mail Transfer Agent (MTA)—for instance, smtp.gmail.com.
  2. DNS MX Lookup: Google's mail server needs to learn which machine handles incoming email for example.org. It queries DNS not for an A web record, but for the domain's MX (Mail Exchanger) records, which return hostnames ranked by priority.
  3. Server-to-Server Relay (SMTP Port 25 + STARTTLS): Your outgoing server connects directly to the recipient's incoming MTA, negotiates TLS encryption, and streams the message across.

You can query real MX records for any domain directly in Node.js using node:dns/promises:

javascript
import dns from "node:dns/promises";

async function lookupMailServers(domain) {
  const records = await dns.resolveMx(domain);
  records.sort((a, b) => a.priority - b.priority);

  console.log(`MX route order for ${domain}:`);
  for (const mx of records) {
    console.log(`  Priority ${mx.priority} -> ${mx.exchange}`);
  }
}

lookupMailServers("gmail.com");

Notice the priority integer: lower numbers win first (5 before 10), giving every domain automatic primary and backup mail gateways if one data center goes offline.

2. How Inboxes Fight Spoofing: SPF, DKIM, and DMARC

Because original SMTP (Simple Mail Transfer Protocol) trusted any sender claiming From: ceo@company.com, spammers abused it effortlessly. Modern mail servers gatekeep every incoming envelope behind three cryptographic checks:

ProtocolStored WhereWhat It Proves
SPF (Sender Policy Framework)DNS TXT recordLists exact IP addresses authorized to send mail on behalf of the domain
DKIM (DomainKeys Identified Mail)Email Header + DNS TXTSender signs headers and body with a private key; receiver verifies integrity with the domain's public key
DMARCDNS TXT (_dmarc.)Tells receiving servers whether to monitor, quarantine (spam folder), or outright reject failures

If an attacker spoofs your bank's domain from an unauthorized server, SPF fails and DKIM signature verification breaks—allowing the recipient's inbox to drop the phishing message straight into quarantine before you ever see it.

3. SMTP Delivers; IMAP Fetches

Notice the clean division of labor in email architecture:

  • SMTP pushes mail from sender client -> outgoing MTA -> incoming MTA (store-and-forward queue). If the recipient server is temporarily offline, the sending MTA retries with exponential backoff for up to several days rather than losing your message.
  • IMAP (Internet Message Access Protocol) or proprietary REST/push APIs sync mail between the recipient's mail server and their laptop, phone, and tablet—keeping read/unread flags, folders, and search state synchronized everywhere.

4. Transport Encryption and MIME Attachments Under the Hood

When SMTP started in the early 1980s, messages crossed the network in plain ASCII text—meaning neither binary PDF attachments nor encrypted transport existed natively. Two upgrades still govern every message you send today:

  • Opportunistic STARTTLS + MTA-STS: After opening a TCP connection on port 25, mail servers negotiate STARTTLS to wrap the SMTP conversation inside TLS encryption. Because downgrade attacks could strip STARTTLS, modern domains publish an MTA-STS policy alongside HTTPS DNS rules forcing strict TLS certificate validation between relays.
  • MIME & Base64 Encoding: Because SMTP headers and legacy gateways assume 7-bit printable ASCII characters, binary attachments (images, PDFs, ZIP archives) get encoded into Base64 text segments bounded by MIME boundary tokens—explaining why attaching a 10 MB file swells raw mail size by roughly 33% (~13.3 MB) in transit.

5. Why Transactional App Emails Need Dedicated Infrastructure

Developers launching an app often try sending password resets directly from a web container or shared VPS IP. Within days, Gmail and Outlook bounce or junk those messages because IP reputation, reverse DNS (PTR) matching, DKIM key rotation, and bounce-rate suppression lists take months of warm-up signals to earn. Separating transactional queues (Postmark, SES, or isolated dedicated MTAs) protects domain reputation whenever marketing newsletters or user-triggered notifications spike.

Summary

Clicking Send launches a resilient federated handoff: your client encodes MIME attachments over authenticated SMTP, your relay resolves MX DNS priorities, upgrades to TLS via STARTTLS / MTA-STS, passes SPF/DKIM/DMARC identity verification, and persists inside recipient mail storage until IMAP syncs it cleanly across their devices.

Code Matrix Editorial

Engineering Education & Technical Publications

Code Matrix creates visual technical guides, systems breakdowns, and structured curricula connecting software fundamentals to the physical infrastructure of the modern world.

Continue Reading

Related Deep Dives

Browse all →