Software

Get more e-mail flexibility with Fetchmail


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 mail.server.com protocol pop3 user "joe@joe.com" 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 mail.server.com using POP3 over SSL, with the username joe@joe.com 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 imap.server.com 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:

#!/bin/sh
lockfile="${HOME}/tmp/.fetchmail.lock"
nomail="${HOME}/nomail"
if [ -f ${nomail} -o -f ${lockfile} ]; then
    exit 0
fi
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!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

3 comments
blissb
blissb

Just make sure you know the policies of any business whose e-mail you want to take off-site. Some companies get kind of twitchy about people taking their company e-mail off to a different server. An acquaintance of mine worked for a consulting firm, and had several regular clients. He did essentially what this article suggests, as a way to aggregate his e-mail, thereby simplifying his life. While his supervisor at the consultancy knew, nobody bothered to ask the client companies what they thought. When one of the clients found out (after more than a year!) not only did he get canned, but the consulting company got sued.

jlwallen
jlwallen

use procmail/fetchmail in conjunction and you can use procmail to play different sounds for different email. use a regular procmail setup and then add lines like this: : 0 (no space between the : and 0) * ^FROM.*EMAIL@ADDRESS.COM.* | cat >> /var/spool/mail/USER ; play SOUND.WAV where EMAIL@ADDRESS.COM is the sender address where USER is the username where SOUND.WAV is the sound way to play this uses the "play" command so you have to have that installed.

vdanen
vdanen

Oh yeah, there are a lot of tricks you can do with procmail. I have mine setup to do auto-responding on certain messages, to run a growl notification (I'm using it on OS X which comes with a nice notification tool called Growl so I get popups a) when I get new mail and b) when I get nagios status alerts about systems being up or down). There are tonnes of cool stuff you can do with procmail.

Editor's Picks