Most users use GUI e-mail clients such as Thunderbird or Evolution, or even prefer Web-based mail services such as Gmail; however, there are still a significant number of people using text-based e-mail clients such as Mutt or Pine. Most likely, more people would use such clients if the learning curve were not so steep. There still isn’t a GUI mail client that could hold a candle to Mutt (my subjective opinion, of course).

Because fewer people are using text-mode clients, one might think there is less use for programs like Procmail and Fetchmail. However, the use of these programs is as valid today as it was five years ago, especially on Linux systems that have a full-blown MTA like Postfix or Exim installed by default that can be used easily and with the proliferation of home servers. Imagine for a moment that you have five e-mail accounts, which isn’t uncommon for many people, and a home file/print server running Linux. With a modest amount of work, that server could also be configured to retrieve e-mail from the five accounts on a regular schedule, filter the mail with Procmail, and deliver it to mailboxes then accessed via IMAP on your personal computer.

With some extra effort, a Web-based mail program like Squirrelmail could be installed on the same server providing you with Web- or IMAP-enabled e-mail, from anywhere, containing all the e-mail from your various accounts.

One part of the solution to such a setup would be Fetchmail, a program that has existed for years to do one simple task: download e-mail. The configuration of Fetchmail is quite simple as well.

Fetchmail uses a configuration file in your home directory, namely ~/.fetchmailrc. Because this file contains passwords, it must be mode 0600 so that only the owner can read and write to the file.

The content of the file is very straightforward:

poll protocol pop3 user "" password "secret" mda "/usr/bin/procmail -d %T" ssl

This is one line of a ~/.fetchmailrc; multiple lines can be configured to have Fetchmail poll multiple mailboxes, including IMAP. This essentially indicates that Fetchmail connects to using POP3 over SSL, with the username and the password secret. Finally, it tells Fetchmail to deliver to Procmail instead of to the local MTA; depending on whether you wish to filter the mail via Procmail first or let your MTA handle it; you can omit the entire mda argument.

A similar setup for IMAP would be:

poll protocol imap user "joe" password "secret" mda "/usr/bin/procmail -d %T" ssl

The only difference here is that the protocol is IMAP, rather than POP3, although again using SSL. The Fetchmail manpage provides a wealth of information on various configuration options, including examples of how to poll mail over SSH.

Fetchmail can also operate in daemon mode to self-schedule polling. For instance:

$ fetchmail -d 300

This tells Fetchmail to daemonize and to poll for mail every 300 seconds. However, Fetchmail also runs well out of cron, with a script similar to the following:

if [ -f ${nomail} -o -f ${lockfile} ]; then
    exit 0
touch ${lockfile}
fetchmail -a -K >/dev/null 2>&1
rm -f ${lockfile}

This simple script could be executed by cron every five minutes or so. It also allows you to simply touch the file ~/nomail to have Fetchmail not attempt to retrieve mail at all, which is useful when you’re debugging something.

With Fetchmail, you can interact with as many mail accounts as you have and can direct the mail wherever you like.

Delivered each Tuesday, TechRepublic’s free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!