Unix Mages

Of Mages and Mail, Part One

It was Acre’s first day of training in the Towers mail department over at /var/spool/mail. This is where all the mail is stored and waits to be retrieved by the POP and IMAP daemons.

“How do these mailboxes work?” Acre asked the admin on duty.

“Well,” the admin replied, “it’s really not that complicated. Our Tower users a mail storage format known as maildir.”

maildir?”

“Mail Directory. It allows us to keep the mail very organized and secure.” the admin replied.

“‘Secure’ as in no one can read it?” Acre asked.

“No, people can still read it if the wrong permissions are set. But by secure, I mean it’s harder to corrupt and lose mail like it is with the other storage format.”

“What other format is that?”

mbox.” the Admin said.

Acre thought for a bit.

“So there are two formats: maildir and mbox. How do either work?” he asked.

“Lets start with mbox. In most towers, a Mage’s mailbox is named the login name of the mage.”

“So mine would be acre.” Acre said.

“Correct. And that’s located at /var/spool/mail/acre. Now that mailbox is just a plain text file with all your email in it. It kind of looks like this:”

From root@localhost  Sat Nov 12 12:22:21 2005
Return-Path: root@localhost
From: root@localhost
Received: by localhost@localdomain
Subject: Email Message 1
This is email number one
From joe@unixmages.com  Sat Nov 12 12:33:21 2005
Return-Path: joe@unixmages.com
From: joe@unixmages.com
Received: by localhost@localdomain

Subject: Email Message 2

This is email number two

“So as you can see, all your email is just placed in one big file.” the Admin said.

“That looks like it can get sloppy.”

“Oh it can. Mailboxes can get really big and if one email is corrupt, it ruins all the other email.”

“So is that why we use maildir instead?” Acre asked.

“Yes, maildir solves a lot of the problems mbox has.”

“And how does maildir work?”

“Well, instead of your mailbox, /var/spool/mail/acre, being a text file, it’s a directory. And inside that directory are 3 subdirectories:”

# ls /var/spool/mail/acre
cur new tmp

“3 directories.” the Admin continued, “new is where all the mail you haven’t read yet goes. After you read it, the message gets placed in to cur. When the mail is being delivered, it uses tmp as a temporary area so the delivery doesn’t interfere with other mail. If, for some reason, a message is stuck in tmp for a really long time (like a few days), something is definitely wrong with that message.

“Finally, instead of all mail being stored in one big file, each message gets it’s own text file. So if one message is corrupt, it doesn’t bother any of the other messages.” the Admin finished.

# ls /var/spool/mail/acre/new
1130981120.V801I40ba9M194107.unixmages.com
# cat /var/spool/mail/acre/new/1130981120.V801I40ba9M194107.unixmages.com

From root@localhost  Sat Nov 12 12:22:21 2005
Return-Path: root@localhost
From: root@localhost
Received: by localhost@localdomain
Subject: Email Message 1

This is email number one

“Why is the name of the email so weird?” Acre asked.

“It makes sense to the mail system. It includes the date the message was sent, the name of the domain the message belongs to, and some other internal information. It all helps the organization of the mail system.”

“I get it. So if this is how email is stored, how is it picked up?” asked Acre.

“We’ll take a break and I’ll explain that part later.” the Admin said.

References:

mbox

maildir