The setup described here enables you to store all your email (and email for other people) on a single machine, which might be a home server, a remotely hosted server, or even a desktop, and then access it from anywhere.
IMAP: A Quick Explanation
The mail protocol most people are most familiar with is POP, which has long been the industry standard for serving and retrieving e-mail. A client, which is the sort of desktop mail program with which everyone’s familiar, connects to the POP server and says, “Do you have any messages for me?” If the answer is yes, the client gets a list of the messages, downloads them, and optionally either deletes them from the server or leaves them in place. That’s pretty much the entire capability of POP.
IMAP is an alternative to POP that offers many advantages. Notably, it keeps centralized copies of messages on the server, where they can be accessed from anywhere, rather than fragmented and hidden away in various non-synchronized, non-centralized desktop mailboxes. The mail client interacts with the centralized messages, so your mailboxes look the same at any computer you access them from. The read/unread/replied status of each message is tracked on the server too.
Since IMAP requires long-term storage of messages on the server, e-mail providers have long preferred POP and its quick, space-saving turnaround, which passes the expense of long-term storage on to the user. In fact, almost no popular consumer e-mail provider offers IMAP. Running your own server, though, you can take advantage of IMAP’s benefits. The majority of desktop e-mail clients — Outlook, Eudora, Apple Mail, Thunderbird, et al. — are already ready for IMAP. If you prefer a web-based interface, you can set that up too.
There’s a wide variety of IMAP server software. Options include UW-IMAP, created by the University of Washington, which invented IMAP itself; Cyrus, from rival university Carnegie Mellon; Courier, and a number of others,
Their relative advantages have been the subject of hotly argued battles, centering around storage formats and nuances of protocol and design. They all work well, but Dovecot stands out — it’s fast, flexible, easy to configure, and compatible with the various popular ways of doing things.
Installing it is as easy as:
apt-get update ; apt-get install dovecot
To configure Dovecot, edit its configuration file.
There’s a line that reads
which is where we specify whether we want to use IMAP, POP, or both. Change that line to
protocols = imaps
(imaps is a secured version of IMAP) and save the file.
The next step is to create an SSL certificate, which Dovecot will use for its secure connections. At the command line, type
openssl req -new -x509 -nodes -out /etc/ssl/certs/dovecot.pem -keyout /etc/ssl/private/dovecot.pem -days 5000
SSL, the software that handles the encryption side of affairs, will walk you through the process of generating the key. We’ve set it up to be valid for 5,000 days, so you’ll need to re-run this command in 14 years or so. SSL prompts you to enter some information about yourself, which can pretty much be made up, except for “Common Name”, which should be the full name of your server. The e-mail address should be valid too.
Now, to make sure Dovecot is installed properly, type
at the command line to launch it. It should give you no feedback at all, which is how Linux tells you you’re doing everything right. Next, try a test connection to Dovecot, just to make sure it’s ready and waiting:
openssl s_client -connect localhost:993
(This opens a connection to the IMAP server software via SSL. 993 is the numbered port devoted to secure IMAP traffic.) You should see a long series of certificate-verifying jargon as SSL does its thing. Finally, Dovecot will say:
* OK Dovecot ready.
to ask Dovecot what it’s capable of. This is how IMAP clients say hello. Dovecot will respond with something like:
* CAPABILITY IMAP4rev1 SASL-IR SORT THREAD=REFERENCES MULTIAPPEND UNSELECT LITERAL+ IDLE CHILDREN NAMESPACE LOGIN-REFERRALS STARTTLS AUTH=PLAIN 1 OK Capability completed.
That’s good! Your new IMAP server is very capable. End the connection by typing:
Now is a good time to open up port 993 on your router and/or firewall.
You can start Dovecot by running
At this point you can configure your mail client of choice to connect to Dovecot, or just use a public service like mail2web.com to check your mail. Either way, you’ll need to configure it to check IMAP mail via SSL, using port 993 (the default SSL port). Your IMAP server name is the domain name of the hosting server, and your username and password are the regular ones you use to log in to the server. Every user configured on the server gets his or her own Dovecot account.
When first logging in, you may get an error message about an unrecognized or “self-signed” SSL certificate, but just tell the mail client to accept it.
Now that you’ve got the infrastructure of your mail server ready, it’s time to set it up to get some actual mail. It can be configured to receive mail directly, so messages sent to your address just arrive at the server with no intermediary. But, to start with at least, it’s safer and simpler to just download messages from an existing e-mail account elsewhere, using POP. That lets you off the hook if, say, your DSL goes down for a few hours: you and your users can find their new e-mail still on their e-mail provider’s server, without any concern about bounces or deferments. The software we’re going to use to fetch mail to the server is called Fetchmail. Install it:
apt-get install fetchmail
To configure Fetchmail, edit its configuration file:
First, input a few lines at the top to set up the basics:
set daemon 60 set syslog set postmaster "your username"
Then give it the details of the POP server where you get your e-mail. An IMAP server is fine too, if you happen to use one of those instead. You’ll need to know the server name, username, and password. Type these up into a “stanza” of configuration, along with the username you use on your server (not root):
poll your.server.name with proto POP3 (or IMAP, or AUTO if you don't know/care) and options uidl user "your POP username" there with password "your password" is username here fetchlimit 10 keep mda "/usr/bin/procmail -d %T"
To clarify where to stick in your own info, take a look at this real-world example:
poll pop.gmail.com with proto POP3 and options uidl user "miniservedude" there with password "8JKtilB2" is paul here fetchlimit 10 keep mda "/usr/bin/procmail -d %T"
You may want to set this up just for a test account for now, not your main personal e-mail address, until you get it working smoothly. Eventually, you can create a stanza like this for each person using your server (and for each e-mail account each person has), to download all their e-mail for them and place it into their accounts on the server.
Using that handy “user X there is Y here” syntax, you can place mail from multiple accounts of different types into a single account on your server.
The keyword “keep” tells Fetchmail to leave messages on the POP server after it downloads them. If you want to delete messages when they’re downloaded, change “keep” to “fetchall.”
The full documentation for Fetchmail can be read at the command line by typing the manual command
Fire up Fetchmail, similarly to how you started Dovecot earlier, by typing
It will commence running in the background with no further interference needed from you — remember, it is a *daemon*. To keep an eye on what it’s doing, you can run
tail -F /var/log/mail.log
which will display a real-time view of the messages Fetchmail mutters as it runs (those messages are stored in a log file called B, which we’re monitoring with the command tail). Since you configured Fetchmail to check mail once a minute (above, with that “set daemon 60″ line), it will look something like this:
Feb 21 14:04:07 localhost fetchmail: sleeping at Wed 18 Jul 2007 02:04:07 PM EST Feb 21 14:05:07 localhost fetchmail: awakened at Wed 18 Jul 2007 02:05:07 PM EST Feb 21 14:05:10 localhost fetchmail: sleeping at Wed 18 Jul 2007 02:05:10 PM EST Feb 21 14:06:10 localhost fetchmail: awakened at Wed 18 Jul 2007 02:06:10 PM EST Feb 21 14:06:14 localhost fetchmail: sleeping at Wed 18 Jul 2007 02:06:14 PM EST Feb 21 14:07:14 localhost fetchmail: awakened at Wed 18 Jul 2007 02:07:14 PM EST Feb 21 14:07:18 localhost fetchmail: sleeping at Wed 18 Jul 2007 02:07:18 PM EST
and so on. Now, from another machine, send a test e-mail message to the POP account you configured, and wait for it to come in:
Feb 21 14:09:22 localhost fetchmail: awakened at Wed 18 Jul 2007 02:09:22 PM EST Feb 21 14:09:26 localhost fetchmail: 1 message for account at pop.gmail.com (1629 octets). Feb 21 14:09:26 localhost fetchmail: reading message firstname.lastname@example.org:1 of 1 (1629 octets) Feb 21 14:09:26 localhost fetchmail: not flushed Feb 21 14:09:26 localhost fetchmail: sleeping at Wed 18 Jul 2007 02:09:26 PM EST
Hit Ctrl-C to get out of the tail output.
Your server is now fully equipped for IMAP mail. You can install an SMTP server, like Exim, Postfix, or Qmail as well, to handle the sending of e-mail, or you can just send e-mail through your ISP’s mail server.
- For instructions on setting up an IMAP mail server using UW IMAP and the web-based Squirrelmail interface, see Webmonkey’s tutorial Set Up IMAP on Your Mail Server