Check your email into git using procmail

In this blog post you will see how I commit my email into git one message at a time, as it is delivered, using procmail.

Screenshot of the git log, showing the emails committed with their sender and subject

This is a simple three-step process:

Write a program to commit an email to git.

This is the shell script I use; I named it ~/.bin/commit-an-email . It constructs a commit message with the sender and subject, adds the email to the repo, and commits using that message:

#!/bin/sh

MAILDIR=$HOME/Maildir

commit_message=`grep '^From: ' $LASTFOLDER; grep '^Subject: ' $LASTFOLDER`

cd $MAILDIR && \
git add `echo $LASTFOLDER | sed -e 's|^/home/mike/Maildir/||'` && \
git commit -m "$commit_message"

Set a procmail TRAP.

The TRAP setting points to a shell command which is executed after procmail has finished processing an email; it passes a copy of the email to stdin. Set this to the commit-an-email script in ~/.procmailrc :

TRAP=/usr/home/mike/.bin/commit-an-email

Clean up deletions and moves, and push.

After the email is delivered you may read it, move it to another mailbox, or delete it. It’d be great if these actions were tracked as well. One option is to write a series of mutt macros that redefine s, d, enter, and so on, to do the appropriate git command. Alternatively, you could handle this nightly with this script, meant to be run from cron. I named it ~/.bin/commit-emails:

#!/bin/sh

GIT=/usr/local/bin/git
MAILDIR=$HOME/Maildir

cd $MAILDIR && \
$GIT add . && \
$GIT add -u && \
$GIT commit -m "Nightly maintainance" && \
$GIT push origin master >/dev/null

And this is my crontab :

5 1 * * * /usr/home/mike/.bin/commit-email
Advertisements
%d bloggers like this: