Autofiltering tnef attachments with procmail

So I’ve tried a couple times to follow the standard directions to get Debian/Ubuntu setup to automatically convert those annoying winmail.dat files bad outlook mail systems always send to normal compliant attachments. There are some decent directions up on the web about doing this with ytnef-filter but unfortunately none of them seem to work. Probably because the implementation of ytnef that Ubuntu (and maybe Debian) have been shipping for awhile is either busted or incompatible. Anyway I finally got fed up with the whole thing and modified the code myself. So here is what you have to do to get things working.

First get procmail up and running and install tnef, perl and the appropriate perl modules. If you are running Ubuntu (and probably Debian) after you have procmail up and running simply execute.

apt-get install tnef ytnef libmailtools-perl

We won’t actually use ytnef but I modified the code from ytnef-filter so installing ytnef will ensure you have all the necessary perl modules. Now download my modified filter I’ve titled tnef-filter and place it somewhere in your path (I use stow to put a symlink to tnef-filter into /usr/local/bin but for the 2s install just save it in /usr/local/bin/).

tnef-filter

Try typing tnef-filter (control-D to exit) and if the file isn’t found go back and mark it as executable

chmod a+x /usr/local/bin/tnef-filter

Finally we can go ahead and modify your .procmailrc file in your home directory by adding the following code

##############################################################

TNEF Attachments

:0
* ^X-Ms-Tnef-Correlator
{
:0 fw B
* (^Content-Type:[ ]*application/ms-tnef|winmail.dat)
|/usr/local/bin/tnef-filter

}

Of course if you’ve used different directories make the appropriate changes. Note that laziness lead me to hardcode /usr/bin/tnef into the tnef-filter perl script so if you have your tnef utility installed elsewhere you’ll have to edit my filter

Just to be on the safe side you might want to save some backup copies of your incoming mail in case something goes wrong. You can do this by adding something like the following ABOVE all your other procmailrc rules.

:0 c: #Save copy in trash in case this fucks up
.Trash/

Assuming your mail directories are set up the same way mine are (standard from dovecot install but should be the same for any maildir setup) this will send a backup copy of all the messages you recieve to the trash before any other rules have a chance to mangle the messages. Sure the backup will disappear whenever you empty your trash but this way you don’t have to worry about the duplicates mucking up your Bayesian spam filtering or remembering to delete them.

Ideally this filter would be run by postfix so all users would benefit from this filtering. I’m sure it’s possible but since I only have two real email accounts on my machine not worth my time now.

Note that I’ve done nothing of significance here. While writing up this description I’ve realized my modifications are almost the same as those used here in the instructions for Sarge. The only further difference is I added some code to strip out the winmail.dat file so I don’t have to see it at all.