The almost forgotten art of procmail with fetchmail
---------------------------------------------------

Last edited: $Date: 2015/11/17 10:58:53 $


A friend has setup a POP3 server  for one of my domains. So this was
a nice trigger to start remembering how I used to use POP for email.

On my diskless  OpenBSD 5.7 machine I  installed fetchmail, procmail
and mutt and started playing with it.

I simply installed  fetchmail, procmail and mutt  with the `pkg_add`
command.

> Fetching and processing mail just like in the old days

Procmail is  a very powerful  tool that kind  do all kind  of email
magic. We will only touch the surface here.


## Using fetchmail to retrieve email

The first  thing I  do is fetch  my email, deleting  it on  the POP3
server.

The most simple way to use fetchmail is to setup a .fetchmailrc file
in your $HOME directory and run fetchmail.

The .fetchmailrc contains something like this:


   set postmaster "matto"
   set logfile /home/matto/.fetchmail.log

   poll pop3.example.com protocol pop3:
       username "<user>" password "<password>";

   mda "/usr/local/bin/procmail -d %T"


The first line is  to make sure all mail comes to  my user, which is
"matto". The second line is to keep a log.

The third line contains the name of the POP3 server. The foutth line
contains the username ans password for the POP3 server.

The  last line  tells  fetchmail  to forward  all  incoming mail  to
procmail. The path depends on the  system you have, on OpenBSD is it
/usr/local/bin/.


## Using procmail to filter your email

Next, setup a .forward file in your $HOME directory:


   |/usr/local/bin/procmail


All this .forward file does is telling that mail should be processed
by procmail. Again, check the path.

Now the magic begins.

Create a .procmailrc file in your $HOME directory:



   SHELL=/bin/sh
   PATH=/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
   MAILDIR=$HOME/Mail
   DEFAULT=$MAILDIR/junk
   LOGFILE=$HOME/.procmail.log
   LOG=""
   VERBOSE=yes

   :0:
   * ^Subject.*SPAM
   $MAILDIR/spam

   :0:
   * ^(To|Cc).*[email protected]
   $MAILDIR/inbox


This .procmailrc file  contains a block with  some settings followed
by two procmail recipes.

This .procmailrc file does several things.

 * It tells procmail which shell and which path-settings to use
 * It tells procmail that my mailboxes line in the $HOME/Mail directory
 * It tell procmail to use the default mailbox "junk" to deliver the
   mail in
 * It tells procmail to use the mailbox "spam" to deliver all mail in
   with in the subjectline the word "SPAM"
 * It tells procmail to use the mailbox "inbox" to deliver all mail
   that is sent to [email protected]

Spammers use all kind of usernames before the @example.com, this way
they all  end up in  the "junk"  mailbox, except the  ones addressed
directly to me (here: [email protected]).

After  running this  for a  few  days, you  add a  spam recipe  with
something like this:

   :0:
   * ^X-Spam-Status: Yes
   /dev/null


This will send all email that  has a line which starts with "X-Spam-
Status:  Yes" to  /dev/null,  which is  a geeky  way  of telling  to
destroy it.

If you are following mailing-lists, it might be a good idea to add a
filter for those lists. Example:


   :0:
   * ^To.*[email protected]
   $MAILDIR/linux


All mail sent to this list will be collected in the mailbox "linux".
Now you  can tell Mutt  to use its powerful  mailinglist-features on
this mailbox.

You can  add as many  as procmail-recipes as  you want. They  can be
very powerful.


## Use Mutt to read and write email

> Mutt is the most powerful email client

Add  some  lines to  your  .muttrc  to  let  Mutt knows  where  your
mailboxes are:


   set spoolfile = "$HOME/Mail/inbox"
   set folder="$HOME/Mail"      # Local mailboxes stored here
   set mbox_type=mbox           # Mailbox type
   mailboxes \
   ~/Mail/=inbox \
   ~/Mail/=junk \
   ~/Mail/=spam


The default keybinding  to change to a different mailbox  in Mutt is
'c',  so  this is  how  you  can go  from  the  inbox to  the  other
mailboxes.


## Resources

Here are some nice reads  that helped me setup my procmail/fetchmail
system.

 * https://www.linode.com/docs/email/clients/using-fetchmail-to-retrieve-email
 * http://easierbuntu.blogspot.nl/2011/09/managing-your-email-with-fetchmail.html
 * http://userpages.umbc.edu/~ian/procmail.html
 * http://www.panix.com/~elflord/unix/procmail.html
 * http://www.erehwon.org/erehwon/procmailex.html


$Id: fetchmail.txt,v 1.4 2015/11/17 10:58:53 matto Exp $