Unix Mages

Of Mages and Mail, Part Three

We return back to the mail department of /var/spool/mail where the Mail Admin is finishing his lesson of email to the young mage, Acre.

“When a mage is finished writing an email, he hands it off to a daemon called smtpd. smtpd can be though of as the mailman of the Tower. Once smtpd is given an email, he goes on his way to deliver it.

“The first thing smtpd does is check to see where the email is going to. Next, he –”

“Wait, ” Acre interrupted, “How does he know who the email is going to?”

“Er, right. Almost forgot about that part. There’re these things called mail headers.”

“Mail headers.” Acre repeated.

The Admin continued, “Yes. They’re lines of information at the beginning of every email. They tell smtpd who the email is going to, where the email came from, the subject, and other delivery information.”

“What do they look like?”

“Well, like this:”

To: joe@terrarum.net
From: admin@unixmages.com
Subject: Hello

There can be many more headers, but those are the bare minimum smtpd needs to get started on the delivery. On the way, he’ll add some headers himself.” The Admin said.

Acre asked, “Like what?”

“Received Headers. Everytime an smtpd mailman gets an email, he tags it with a received email to say he handled that email at one time. It looks like this:”

Received: from [] (tower1.unixmages.com)         
    by mail.unixmages.com with SMTP id ; Thu, 15 Dec 2005 02:21:27 +0000

“What’s all that mean?” Acre asked.

“It simply says what Tower sent the email and what Tower is going to handle the mail. smtpd also gives it a unique ID to refer to it from now on and tags it with a date.” The Admin replied.

“I got it, ” Acre said, “Now what does smtpd do after it gets a new email to deliver?”

“First, he looks at the To: header and notes the domain name — the part of the email address after the @ sign. In this case, the email is going to terrarum.net. After smtpd knows the domain name, he needs to find out how to get there. He does this by looking up an MX record for that domain. MX stands for Mail Exchange and it lets smtpd know what Tower handles email for a certain domain.”

“Can we look up MX records ourself?” Acre asked.

“Sure can,” The Admin said. “Just cast this spell:”

$ dig -t mx +short terrarum.net
10 terrarum.net.

“This tells us that the Tower terrarum.net handles mail for itself.” The Admin said.

“So Towers can handle mail for other domains besides their own?” Acre asked.

The Admin replied, “Definitely. Sometimes a Tower just doesn’t have the resources to handle it’s own mail, so it lets another Tower do that work.”

“I get it.” Acre said..

“Good! After smtpd knows which Tower to deliver the mail to, it walks over and talks to that Towers smtpd mailman.”

Acre asked, “Does every Tower have a mailman?”

“Technically every Unix Tower has a mailman even if it doesn’t handle email. All modern Towers handle some type of mail — even if it’s internal — but smtpd can only talk to the mailmen of Towers that handle public mail.”

Acre said, “OK, got it. Now what happens when the two mailmen talk to each other?”

“Nothing too exciting. Basically the visiting mailman introduces himself and hands the email over. The destination mailman looks at it and decides if he wants it or not. If he does, he’ll simply place the mail into the mages mailbox. If not, he’ll simply ignore the visiting mailman.”

“That’s kind of rude..” Acre said.

“Well in this day of junk mail, not all mailmen are nice.” The Admin replied.

“So that’s it then?” Acre asked. “The email gets placed in the mailbox and the mage simply reads it using the ways we talked about earlier?”

“Yep, the mail cycle is complete!”

Acre thought for a bit. “You know, this all sounds like a fancy way of working with text files.”

The Admin said, “In a way, it’s exactly that:”

vi, nano, picoMail Program
cat, lessimapd, popd, Mail Program

“Wow, that’s not so hard at all!” Acre said.

“Nope, email is actually pretty simple. Unfortunately it can get difficult with blocking viruses and spam. And there are some advanced email topics, too, but we’ll get to those another day.” The Admin finished.