Keerthi Vibisan
Keerthi Vibisan
Software Engineer at Grootan working on IAM

How Emails Work: Behind the Scenes

Written by Human, Not by AI

Emails are part of modern communication, but have you ever thought how an email travels from your device to the recipient’s inbox ?

Let’s break it down step by step, Before we start you need to know the following

Mail Client

A mail client is an application that allows users to retrieve, store, manage and format emails to be sent.

Eg: Gmail, Yahoo Mail, Outlook Email

Mail Servers

A computer system/software program that sends and receives email. Think of it as an electronic post office (as this moves emails between mail clients).

There are two main types of mail servers:

  • Outgoing Mail Server / SMTP Server
  • Incoming Mail Server / POP3 or IMAP Server

Note

The Outgoing and Incoming mail servers are just software programs, so based on the need they can be run or hosted on same or separate servers.

Outgoing Mail Server (SMTP)

The Outgoing Mail Server follows SMTP (Simple Mail Transfer) protocol to send emails. We can think of SMTP as moving your email on and across networks. Without it, your emails wouldn’t go anywhere.

Transfers your email across networks to the recipient’s mail server.

Incoming Mail Server (POP3/IMAP)

For incoming mail servers, there are two main protocols — POP3 and IMAP.

  • POP3 (Post Office Protocol)

    POP3 is best for accessing emails from a single device, since it downloads emails and often deletes them from the server after download, saving server space but limiting access to those emails to the device they were downloaded to.

  • IMAP (Internet Message Access Protocol)

    These protocol retrieve the email message from the mail server and deliver it to the specified mail client (or clients).

Without one of these protocols working correctly, your email would not reach your device.

Other Components in Mail Servers

Mail Server has several component like authentication modules, queuing mechanisms, MDA, MTA, error handling and etc. The 2 main components are described below

MTA (Mail Transfer Agent)

MTA is responsible for sending and relaying emails between mail servers.

Functionality: - Handles the transport of emails across networks using the SMTP protocol.

Think of MTA as transport system (trucks, bikes, etc.) that moves letters between post offices.

MDA (Mail Delivery Agent)

MDA is responsible for receiving and delivering emails to the mailbox.

Functionality:

  • Takes emails from the receiving SMTP server and stores it in mailbox (in formats like Maildir or Mbox).
  • Prepares emails for retrieval by mail clients using IMAP or POP3 protocols.

Think of MDA like post workers/sorting offices that delivers the letters to the correct mailbox.

Real-Time analogyYou can think of the Mail server as a delivery service. A delivery service has several components like the truck (MTA), post masters (MDA), other staffs, warehouse, and other stuffs needed to manage the entire delivery process.

The Journey of an Email

EmailJourney.png

1. Composing and Sending the Email

  • User Interaction: You compose an email using Gmail’s Client, and when you hit “Send” Gmail’s client prepares the email.
    • MIME Encoding: Gmail Client encodes the email content in MIME format, including headers (e.g., To, From, Subject), the body and any attachments.

2. Initiating Connection with Gmail’s SMTP Server

  • SMTP Protocol Initiation: Gmail’s client connects to Gmail’s SMTP server via TCP.
    • TLS Encryption: The connection is typically encrypted using TLS to secure the communication.

3. SMTP Conversation Between Gmail Client and Gmail SMTP Server

  • HELO/EHLO Command: Gmail’s client identifies itself to the SMTP server using the EHLO command.
  • MAIL FROM Command: The client sends the MAIL FROM command to specify the sender’s email address.
  • RCPT TO Command: The client sends the RCPT TO command to specify the recipient’s email address (e.g., [email protected]).
  • DATA Command: The client sends the DATA command, followed by the email headers, body and attachments, then terminates the data with a single period (.).

4. Gmail’s SMTP Server Processes the Email

  • Domain Extraction: Gmail’s SMTP server extracts the domain part of the recipient’s email address (yahoo.com) to determine where to send the email.

5. DNS Query to Find Yahoo’s Mail Server

  • MX Record Lookup: Gmail’s SMTP server performs a DNS query to find the Mail Exchanger (MX) records for yahoo.com. This query returns the domain names of Yahoo’s mail servers.
    • A/AAAA Record Lookup: After identifying the MX record, Gmail’s SMTP server queries the DNS to resolve Yahoo’s mail server domain name(s) into an IP address.

6. Establishing Connection Between Gmail and Yahoo Mail Server

  • TCP Connection: Gmail’s SMTP server establishes a TCP connection with Yahoo’s SMTP server.

7. SMTP Conversation Between Gmail and Yahoo Mail Servers

  • HELO/EHLO Command: Gmail’s SMTP server identifies itself to Yahoo’s SMTP server using the EHLO command.
  • MAIL FROM Command: Gmail’s server sends the MAIL FROM command to specify the sender’s email address.
  • RCPT TO Command: The RCPT TO command is sent with the recipient’s email address ([email protected]).
  • DATA Command: The email content is transferred to Yahoo’s server using the DATA command, ending with a period (.).

8. Yahoo Mail Server Accepts and Stores the Email

  • Store: Yahoo’s SMTP server accepts the email and stores it in the mailbox, typically in a storage format like Maildir or Mbox.
    • This mailbox is part of Yahoo’s incoming mail server (IMAP or POP3) server.

9. Retrieval by the Yahoo User

  • IMAP/POP3 Retrieval:
    • IMAP: When the user opens their Yahoo Mail app or client, the client connects to Yahoo’s IMAP server.
      • The IMAP server allows the client to synchronize with the mailbox, fetching the new email without deleting it from the server.
    • POP3: If using POP3, the client downloads the email to the device, potentially deleting it from the server.

10. User Interaction with the Email

  • Email Display: The email client displays the new email in the user’s inbox.

So this is the complex process that happens every time you hit ‘send’!