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).
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.
Trying out yourself
You can use aiosmtpd to emulate your SMTP server. You can install it using pip:
pip install aiosmtpd. Then simply run it with
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:email@example.com\n") send_wait_print(b"RCPT TO:firstname.lastname@example.org\n") send_wait_print(b"DATA\n") send_wait_print(b'From: "Bob Example" <email@example.com>\nTo: Alice Example <firstname.lastname@example.org>\nCc: email@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')
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. I find it difficult to find a definitive guide about all these. Let me know if you found one!
If you are interested to play more with email, you can also learn on how to migrate your email!