How Email Actually (Apparently) Works

Home

January 24, 2020

2 protocols

I found out that actually the email infrastructure is consisted to 2 level of abstraction, the SMTP communication protocol and the email formatting. The analogy is close to SMTP protocol being the HTTP protocol and email formatting being the HTML formatting (in case of web).

Difference between SMTP protocol and email format

The protocol used by the SMTP server to communicate with each other is defined in RFC 821, which is superceeded by RFC 5321. And for the formatting of the email, RFC 822, superceeeded by RFC 5322.

SMTP conversation

Trying out yourself

Demo of SMTP using telnet

You can use aiosmtpd to emulate your SMTP server. You can install it using pip: pip install aiosmtpd. Then simply run it with aiosmtpd -n.

Then communicate with the server using this simple python script

from telnetlib import Telnet
import time

def send_wait_print(message):
    tn.write(message)
    time.sleep(1)
    print(tn.read_eager())

tn = Telnet("localhost", 8025)
time.sleep(4)

send_wait_print(b'HELO example.com\n')
send_wait_print(b"MAIL FROM:kevinwinatamichael@gmail.com\n")
send_wait_print(b"RCPT TO:you@example.com\n")
send_wait_print(b"DATA\n")
send_wait_print(b'From: "Bob Example" <bob@example.com>\nTo: Alice Example <alice@example.com>\nCc: theboss@example.com\nDate: Tue, 15 Jan 2008 16:02:43 -0500\nSubject: Test message\n\nHello Alice.\nThis is a test message with 5 header fields and 1 line in the message body.\n')
send_wait_print(b'\r\n.\r\n')
send_wait_print(b'QUIT')

Furthermore

There are much more interesting topics that I cannot cover in this post. We haven’t discussed the email formatting in detail, how do we include media files, how do we format reply thread etc.. In the SMTP protocol, we haven’t discuss about authentication, IMAP, POP and email forwarding/aliasing.

If you are interested to play more with email, I also found this interesting article on how to migrate your email that is worth trying.