Sunday, April 20, 2014

Creating a local email archive with: offlineimap and procmail

I synchronize my imap folders to maildir on my local laptop often so I can both have access to my email without a network and utilize my preferred search and email clients.  In order to facilitate how I use email I keep a local archive which created and filtered by procmail.

Here is an approximation of my crontab (cron doesn't start a shell, so I put most of the commands in a script):

% crontab -l
HOME=/home/myhome
MAIL=$HOME/maildir
PROCMAILD=$HOME/.procmail.d
0-59/5 9-18 * * * $HOME/bin/syncemail 


Here is the syncemail script:

#!/bin/sh

offlineimap 2>&1 | logger -t offlineimap

for i in `find $MAIL/Disney -type f -newer $PROCMAILD/log `; do
  cat "$i" | procmail
done
[


and here are the relevant portions of my .procmailrc:

PMDIR=$HOME/.procmail.d
VERBOSE=off
MAILDIR=$HOME/maildir
DEFAULT=$MAILDIR/mbox
LOGFILE=$PMDIR/log
LOGABSTRACT=all
ARCHIVEBY=`date +%Y-%m`
ARCHIVE=$MAILDIR/archives/$ARCHIVEBY
MKARCHIVE=`test -d ${ARCHIVE} || mkdir -p ${ARCHIVE}`

# Prevent duplicates
:0Wh: $PMDIR/msgid.lock
| /usr/bin/formail -D 100000 $PMDIR/msgid.cache

:0c
${ARCHIVE}/


§