Having already shown you how to run your own web server using Apache, we'll now turn our attention to the most important application of networking: email.

Running your own mail server may seem like overkill, but there are a number of good reasons for doing so. And if you consider yourself well-versed in the lore of sysadmin, this is definitely a topic you need to be comfortable with. Read on!

- For a start, you can access your mail from more than one computer (for example, a desktop and a laptop, or one machine at work and one at home) but keep both in sync.

- You can download mail from several email accounts (your ISP, Google Mail and so on) putting them all in one place.

- You can download mail for all family members and sort it into separate mailboxes.

- Mail downloads become much faster, because the slow part of pulling the information from your ISP has already been done in the background.

- Last, but by no means least, you can run spam and virus filtering software to sort out the rubbish from the good stuff before you even fire up your mail program.

There are several aspects to mail serving: receiving mail from outside, delivering it to local mailboxes, serving mail from the local mailboxes to clients, providing web access to those mails and receiving and forwarding outgoing mail from the clients.

There are two ways of getting the mail on to your system; you can run a full-blown mail server such as Postfix and set up the MX records to point to your IP address, or you can pull it from an external mailbox using Fetchmail. We've already covered how to build your own email server with Postfix, so here we're going to show you how the other option, Fetchmail, works.

Get the mail

Fetchmail connects to one or more mailboxes, downloads the mail from them and delivers it to local mailboxes. The first step is to use your package manager to make sure that Fetchmail and Procmail are installed.

Fetchmail reads its configuration from either ~/.fetchmailrc when run as a user or /etc/fetchmailrc if run as a system process from init. You can create a .fetchmailrc file by running fetchmailconf and filling in your details. Move this to /etc/fetchmailrc if you plan on running Fetchmail as an init service. Or you can create a config file by entering

set daemon 300
poll mail.myisp.com with proto POP3
user 'myispuser' there with password 'mypass' is 'myuser' here options keep
mda '/usr/bin/procmail -d %T'

The first line asks Fetchmail to check your mailboxes every 300 seconds (five minutes), the last line tells it how to deliver the mail. By default, Fetchmail tries to connect to a local SMTP server, but we won't be setting one of those up in this tutorial, so we'll use Procmail to deliver the mail for us instead.

The middle two lines are really one line split for ease of reading. These tell Fetchmail to poll the POP3 mailbox for myispuser at mail.myisp.com and deliver the mail to myuser on the local machine. The options keep part instructs Fetchmail to leave the mail on the server, which you should use until you're sure things are working, and then you can remove it. You may have any number of poll lines, pulling mail in from various mail servers or for different users.

If you want to collect mail from a Google Mail account, you'll need to enable POP3 access in the Forwarding and POP/IMAP section of the Gmail settings. Fetchmail is fussy about the order in which options appear in the config file; the global options, such as Set Daemon, must appear first, then the server poll options and finally the delivery settings must follow all the server settings.

Because the configuration file contains passwords, it must be readable only by the user running Fetchmail, or the program aborts with an error. If using it as a service, run

chown root: /etc/fetchmailrc
chmod 600 /etc/fetchmailrc

...to avoid any potential issues.

Via TuxRadar.com