Manage lots of mailboxes with getmail

Updated 2017-03-06: Now properly handles oldmail files to avoid multiple downloads of old mail when calling different sets.

Faced with a growing number of mailboxes to fetch messages from, I devised a little script to help me easily manage lots of accounts with getmail. It was inspired by this post from Charles Cazabon, the developer of getmail.

The advantage of this solution is that you need to create, name and manage getmail’s rcfiles for different mailboxes in one place only, without modfying other scripts, crontabs or whatever.

First off, this is the folder structure I created. It may remind you of the old /etc/rc.d/ folder structures that were common for system start scripts before we had upstart and all this modern whoop-de-do:

.getmailsets
├── set-all
│   ├── rc.blog
│   ├── rc.freemail
│   ├── rc.provider
│   ├── rc.uni
│   └── rc.work
├── set-often
│   ├── rc.provider -> ../set-all/rc.provider
│   ├── rc.uni -> ../set-all/rc.uni
│   └── rc.work -> ../set-all/rc.work
├── set-rare
│   ├── rc.freemail -> ../set-all/rc.freemail
│   └── rc.blog -> ../set-all/rc.blog
└── set-important
    ├── rc.uni -> ../set-all/rc.uni
    └── rc.work -> ../set-all/rc.work

.getmail/ -> .getmailsets/set-all/

All the getmail-typical rcfiles live in the .getmailsets/set-all/ folder, which is symlinked to .getmail/. This ensures that things will work like any normal getmail setup whenever you don’t use the sets.

Specific sets of rcfiles are defined by folders with a corresponding name and contain links to the actual rcfiles in the „all“ set.

This is the script that puts this structure to use. I call it checkmail:

#!/bin/bash

RCARGS=""
RCPATH=/home/myself/.getmailsets
RCSET=$1  # first argument names the set,
shift     # then gets deleted

for F in $RCPATH/set-$RCSET/rc.*; do
  RCARGS="$RCARGS --rcfile $(basename $F)"  # prepares --rcfile args
done

exec getmail $@ --getmaildir $RCPATH/set-all/ $RCARGS
                # make sure getmail always uses same oldmail-*
           # $@ holds all the (remaining) arguments to the script.

You use checkmail by calling it with the set name as the first argument, and any arguments you want to pass on to getmail, like in these examples:

$ checkmail all -q
$ checkmail important -v
$ checkmail rare -q -d

And these calls, of course, are what you want to place in your scripts, shortcuts and whatnot. You can then manage the set contents or rename the rcfiles in your .getmailsets without changing any of the scripts. How about, for example, a button on your desktop that runs:

$ ssh mailserver "checkmail important -v"

The script is also ideal, of course, for your crontabs. Here’s an example of my configuration:

*/5  0-3,9-23  * * * checkmail often -q
  0      8-23  * * * checkmail rare -q

Since $HOME/.getmail/ is symlinked to .getmailsets/set-all/, you can also call getmail for any single mailbox like your ordinarily would:

$ getmail -r rc.work
$ getmail -vr rc.uni
Advertisements
1 Kommentar
  1. It won’t be a bad idea to use pushd $RCPATH &> /dev/null instead of cd $RCPATH, and popd &> /dev/null at the end of the script, to don’t change the user’s pwd.

Kommentar schreiben/Post a comment

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s