Finding and pretty-printing many emails

Home Blog Resume Canary IF Photos Projects Software Thanks Writing

November 8, 2021

Back in 2020, we were one of many customers sold cars with undisclosed defects by a local car dealer.

We’re off to VCAT soon, and so I recently needed to print out a whole bunch of emails. But printing emails from mail clients is slow, and it’s easy to miss individual messages. So I sought to find a solution that used command-line tools to handle the task in bulk.

Unix to the rescue :)


(I’m leaving aside the philosophical question of just how Unixy some of these tools are. I’d argue that despite the odd miss (like --exec options in mail searchers) they have the Unix nature by virtue of being somewhat single-focused, and pleasantly composable.)

Anyway, my approach was to break down the problem into the following steps:

  1. Download a local copy of the mail.
  2. Search the mail for the individual messages of interest.
  3. Pretty-print the messages of interest.
  4. Send the pretty-printed documents to the printer.

To download the email, I used mbsync. My email provider is Fastmail; I already use mbsync with mu4e for my own email so this was a natural step. The configuration I used is as follows:

IMAPAccount foo
Port 993
AuthMechs Login

IMAPStore foo-remote
Account foo

MaildirStore foo-local
SubFolders Legacy
Path ~/email/foo/
Inbox ~/email/foo/INBOX/

Channel foo
Master :foo-remote:
Slave :foo-local:
Patterns * INBOX
Create Slave
Sync All
Expunge Both
SyncState *

Having synchronised the mail with:

$ mbsync -c ~/email/foo foo

… the next step was to find the emails I was interested in.

This is where the email indexing tool mu came in. I’ve already initialised an email index in ~/email/ with mu init --maildir=~/email as part of my regular email setup, so all I needed to do was to find the files was to use mu along with GNU Parallel:

$ mkdir ~/tmp/mails
$ mu find --skip-dups to:REDACTED or subject:REDACTED --fields=l | parallel -N 1 cp {1} ~/tmp/mails

…. where --fields=l just returns the full path to the email in question.

The next step was to install an email pretty-printer. I chose to use Nick Russler’s email-to-pdf-converter tool, which I installed along with wkhtmltopdf.

This produces pleasant-looking output out of the box, e.g.:

From: Duncan Bayne <>
Subject: Test 3
Date: Wed, 09 Jun 2021 16:07:32 +1000

Testing, testing, one two three.

Duncan Bayne
+61 420 817 082 |

I usually check my mail every 24 - 48 hours.  If there's something
urgent going on, please send me an SMS or call me.

… becomes:

Rendered email

Pretty-printing the emails in PDF format was then just a matter of:

$ cd ~/tmp/mails
$ find . -type f -exec java -jar ~/bin/emailconverter-2.5.3-all.jar {} \;

Finally, then, all that remained was the fourth and final step: sending the PDFs to the printer:

$ find . -type f -name '*.pdf' -exec lpr {} \;

The end result is a stack of email hard copies, and a slightly more insistent low toner warning on my printer :)

Thanks to Wikimedia Commons for the Unix licence plate photo, and to Jonathan Leffler for the suggestion to use GNU Parallel.