The Simplicity of E-mail
There is Susam Pai's neat article on how simple IRC is. It's... really neatly simple; you can just log into an IRC server just using a telnet client.
IRC, however, is not unique with its simplicity among earlier internet protocols. In fact, e-mail and SMTP is even simpler!
After all, e-mail is about saying hi and then piping a text file into a TCP port.
Which then gets delivered by appending the same text file onto an Mbox file.
Part 1: connecting
Pick an SMTP server that is not armed to teeth against spam. Gmail will probably not talk to you; you'll have better chances with your self-hosted Postfix instance.
~ $ openssl s_client -starttls smtp -connect your.mail.server.com:587
... if you're using STARTTLS, that is. The command is fairly similar with plain TLS too though. It looks more complicated than plain telnet; it's just the layer underneath though (not many servers do port 25 anymore).
... unless you're on the mail server itself; there is a chance that
~ $ telnet localhost 25
will still be heard. You'll be eventually greeted by
220 your.mail.server.com ESMTP Postfix (Ubuntu)
Part 2: saying hi
... yes, there are actual greetings involved.
Part 3: from and to fields
250 2.1.0 Ok
250 2.1.5 Ok
(the lines starting with numbers are the responses from the server.)
Part 4: the actual message
This is where the "piping the text file" part comes in: you just say
354 End data with <CR><LF>.<CR><LF>
... and this is our actual email text!
250 2.0.0 Ok: queued as 7DCB64A55D
That was it. You sent an e-mail. This is pretty much how your e-mail client does it... or other e-mail servers forward them to your server.
For extra fun value, it's worth looking into the Mbox file format, used by many UNIX systems. It's... impressively simple, too: just a bunch of emails, as text, one after the other, separated by occurences of "From ", following a newline.
They, apparently, couldn't even agree on how to escape these in actual mail text.
So... essentially, to write an email server, you need to:
- listen on a TCP port
- respond to a few super simple commands
- when requested, append-pipe stuff from the TCP port into a text file in a user's home directory.
That's it. E-mail is just a >> operator that works across users and servers. Hard to make things easier than this.
... so you're saying it's not this simple? Well... it indeed... isn't.
First of all, if you want an SMTP server to accept and forward mail for someone that is not itself, you'll need to log in, because otherwise spammers will find it and use it to send their messages.
Also, e-mail has headers (on top of the text file), with all kinds of extra info. Including various signatures to prove that it's coming from the place it's claiming to be coming from. Absent these... well, apparently, mail still gets through, maybe, but it's a lot more likely to be flagged as spam and / or marked as coming from an unknown sender. There is all kinds of complexity in doing this right.
SMTP servers also do tend to end up being really flexible, and, therefore, complex. And then there is IMAP / POP3, which is an entirely different protocol (its purpose being to pull mail, not push it somewhere, like SMTP does, both between client and server and source server - destination server).
In the end though... it's all built on piping text files.