Fetchmail and Procmail – the basics

19 Jan

Fetchmail and Procmail – the basics
In response to a fair few questions on these two programs (and a desire from Richard to have me do a talk on them) I’ve put together these introductory notes on the subject.

http://ieee.uow.edu.au/~mjp16/misc/fetchprocmail.html

Fetchmail and Procmail – the basics
In response to a fair few questions on these two programs (and a desire from Richard to have me do a talk on them) I’ve put together these introductory notes on the subject.

Fetchmail
Takes mail from a central store (typically your ISPs mail machine) to the local system.
Config file $HOME/.fetchmailrc. Permissions must be 0600, to protect password details.
Use the following line for each server you’re taking mail from in your .fetchmailrc file:
poll protocol :
user ““, with password ““, is “” here;

The options should be filled in appropriately, as follows:
server – your ISP’s mail machine, such as pop.isp.com.
proto – The protocol you use to pick up your mail. Usually either pop3 or imap, though the program does support many others. If you need them, you’ll know it.
username – your username on the server. The double quotes help protect against any untoward characters (such as spaces and periods) which might otherwise make things unpleasant.
pass – the password of your account on the server mail machine. This is why we set such restrictive permissions on .fetchmailrc.
local username – Your username on the local machine (where fetchmail is running). This is useful if, for instance, your ISP assigns you a username fre9kjm, when you’re known as fred on your local box.
To have fetchmail run periodically whilever the link to the Internet is active, place the following line in ip-up (or equivalent):
su -c “fetchmail -d 600”

This tells fetchmail to startup and poll for mail every 600 seconds (10 minutes). Change this number for more rapid or occasional polling.
To stop fetchmail again when the link goes down, place this in ip-down (or equivalent):
su -c “fetchmail –quit”

Can add –syslog to the startup command line, or set syslog to the beginning of .fetchmailrc (before any poll lines) to make all log messages go to syslog.
Add –logfile to command line or set logfile to .fetchmailrc to log to a specified file .
Procmail
Filters mail based on user-specified rules.
Config file: $HOME/.procmailrc
Should be the default local delivery agent on most Linux systems – ask the sysadmin if you’re not sure. If it isn’t, place the line
“|/procmail”

in your $HOME/.forward file, replacing with, strangely enough, the path to the procmail binary.
Typical config:
PATH= MAILDIR= DEFAULT=
LOGFILE=

Should place in PATH only directories which you will need, to maximise security. If in doubt, have an empty path and specify full path on all binaries.
maildir – likely to be $HOME/Mail or similar.
default mailbox – where mail which doesn’t fit any filtering recipe will be put.
logfile – hmm, well…. (grin)
Recipes
:0 [flags]

Most useful flag – c – specifies that mail which matches this recipe should still be matched against the rest of the recipes, and not considered delivered.
If there are multiple conditions, all must match for the action to be executed.
If there are no conditions, then the recipe is always true.
A condition is signalled by a ‘*’ – everything after that (except leading and trailing whitespace) is considered to be the regular expression to match against.
Regular expressions are a cryptic and powerful language for describing strings. Common components used are:
^ – start of line.
$ – end of line.
. – any single character, except newline
() – a group of characters (used, typically, with the options below)
* – 0 or more of the previous character or group.
+ – 1 or more of the previous character or group.
? – zero or one instances of the previous character or group.
\ – escape the next character.
Any other character – itself.
Manual pages for egrep(1) and sed(1) are also instructive for extended regular expressions.
Escapes are used to make an ordinarily special character match literally instead. For instance, if you wanted to match against the ‘$’ character, then if you just placed it by itself, procmail would interpret that as ‘match if end of line’. Instead, we use ‘\$’ to match against a single ‘$’.
There are some special procmail-specific tokens which you can use:
^TO – Should match any line which specifies a message recipient containing the word which follows.
^FROM_DAEMON – Should match on any message which was sent from an automated mail program (mailing list software, spam bot, whatever).
Plenty examples are available in the procmailex(5) man page.
References
The Fetchmail home page
The Fetchmail FAQ
The procmail home page
Man pages:
fetchmail(1)
procmail(1)
procmailrc(5)
procmailex(5)