How the Email System Works

In a few days when I get a better handle on it myself I’ll post several independant articles on fetchmail, procmail, sendmail, spamassassin, etc. However, the problem I had when I was just getting into all of this was where to start. Indeed, the email system is a convoluted mess to say the least. However, if you understand the high level functions of each component things start to make a little more sense. Let me share what I know so far.

Note since original article: I found a link that explains this far better than my little story. Check out ZDNet UK’s story For a real good read on the basic components. Then read mine if theirs makes too much sense. 🙂

So you sit at your desktop and compose and email. For the sake of clarity lets pretend it is to somebody in a distant organization with no affiliation to yourself. In that circumstance your email program will help you draft a pretty little email. It will then ship it off to port 25 on some fairly close server to do the actual work. Usually this is with your office or ISP.

This not so distant machine gets the email on port 25 and says,”hey, who was it that asked for all the traffic to port 25. Oh yes, I remember it was Sendmail“. (The first piece of the puzzle.) Sendmail knows how to transfer mail from a local machine on port 25 to a distant machine on port 25. It turns out that sendmail is a real patient program. If the remote machine is down it will make all the right decisions to give the email the best chance of arriving at its intended destination. In our case everything is fine. The not so distant sendmail machine grabs the email and passes it along to the distant sendmail machine.

So the distant sendmail machine gets an email on port 25. Linux on that machine says, “as I recall sendmail had some interest in port 25. I think I’ll make it handle this data.” Sendmail wakes up and says,” oh yes, not only do I speak sendmailese (also known as SMTP), but this message appears to be for one of the users that I so benevolently serve.” So sendmail says, well, I’ve done my part, I’ll run some subordinate to actually store this email

Sendmail looks in its hideous configuration file and decides that procmail is a pretty good program for filing emails. So it forks off a procmail process and says, “hey, here is an email for you to file.$quot; Upon hearing which, procmail cheerfully springs into action.

Procmail says, what does my configuration want done with this message? Should I just file it in the inbox? Well, in our case our SysAdmin set procmail up to ask SpamAssassin how likely it is that this message is spam.

Procmail forks off a SpamAssassin program and passes it a raw message. SpamAssassin consults databases, does some math, checks some blacklists, maybe does some Bayesian filters, and after all that work adds a simple header (Like a From: header only with a different name) to the email. This new header is how likely SpamAssassin says it is that this message is spam.

SpamAssassin finishes and passes its data back to Procmail. Procmail says, “I see you’ve classified this as on of the few peices of non-spam we see around here. As per my configuration, I’m going to file this in the Inbox.”

Now, your distant friend is sitting at his desktop wondering, “I wonder if I’ve received any new spam?”. So he loads up his email client. If your friend is cool they are checking with IMAP which allows for cool stuff like folders. For imap to work your friend’s mail server needs a program called (drum roll please) imapd! Bet you didn’t see that one coming. Your distant friends email program connects to port 143, talks some IMAP, and ultimately gets the email you sent.

Was that so bad? … Ok it was. Don’t blame me, I didn’t build it. 🙂 But you’ve probably got a sense for how it all fits together. The best/worst part of this is that at any stage you could swap out one of the programs I mentioned and use a different program that is functionally equivalent. Its cool to have all those choices, but investigating half a dozen choices just to send email can be a pain. Thats my .02 on the matter. More later as I learn it. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *