2005-04-18

To a mailbox and back again: the journey of a misdirected message.

Faithful readers may recall the one time last year when my mail server's MDA decided that every message it was receiving was spam, and thus squirreled it away to a remote section of the disk nary to be seen my mortal men again. I spent the better part of an hour that morning writing a shell script to parse the misdirected mail and redeliver it to its intended recipients.

It did not go as smoothly as I had planned.

Since then, I've been thinking of the ways I could have improved that script. Foremost among these ideas was the thought of hashing each message and creating a file of that hash value. This avoids duplication as shown the following pseudocode:

 $hash = `md5sum < $msg`
 if [ test -x /tmp/mailscript/$hash ] then 
   continue;
 fi
 touch /tmp/mailscript/$hash
 qmail-inject $recipient < $msg

This way, a message with N recipients will not be delivered N times to each recipient.

Today, I discovered serialmail. Now, to be honest, I've known about serialmail for a few years. But damned if the documentation isn't sketchy at best. I mean, I couldn't have told you what the programs in the serialmail package do exactly. But then a recent message to the qmail mailing list put it into perspective. serialmail can take messages from a Maildir and reinject them over SMTP or QMTP.

Me Toby. Me bang rocks 'gether good. Unh! Me strong.

So I threw an OpenBSD 3.7-beta snapshot onto a PC and ran a prerelease version of my qinst qmail installing Perl script on it. In minutes, I had a perfectly configured (for me, at any rate) qmail server up and running.

I added a new user. I sent some mail from his account. (If you're curious: 'for i in `jot 10 1`; do echo $i | /var/qmail/bin/mailsubj '$i' toby; done') It quickly appeared in ~toby/Maildir/new/.

Then I installed serialmail. Here goes nothing.

I moved ~toby/Maildir/ to ~toby/Old/. I remade a new Maildir: '/var/qmail/bin/maildirmake ~toby/Maildir/'. Time to play around.

$ maildirsmtp ./Old/

Done. 'find ./Old/' shows that ./Old/ is empty. 'find ./Maildir/' shows that the messages have all been — what else? — redelivered.

D'oh. I would have killed to know about this last year. Me go back in cave now.

No comments: