Printing an entire (Thunderbird or other mbox) folder of email correspondence

Ever wanted to print out an entire folder archive of email correspondence? Of course, a lot of it will be redundant (do you have long correspondences, quoting the previous message), but it might still be useful. Note that there may be trickier ways of doing this (uploading all to Gmail, which does a nice job of taking out overlapping portions of the thread?).

Anyway, here’s my take. This works fine for Linux. It will have at a minimum one page per email message – so this could be a very large file.

#!/bin/bash
# $Id$
# $URL$

if [[ -z $1 ]]
then
cat < 

  will print an entire 'folder' or mbox file, from
  Thunderbird, Mutt, etc., into a single PDF, with 
  one page per email message.

  Attachments are ignored.

EOF
exit 2
fi

MBOX=$1
show=$2

#------------------------------------------------------------
# requirements
fail=no
FORMAIL=$(which formail)
[[ -z $FORMAIL ]] && fail=yes

MUTTPRINT=$(which muttprint)
[[ -z $MUTTPRINT ]] && fail=yes

PDFTK=$(which pdftk)
[[ -z $PDFTK ]] && fail=yes

PS2PDF=$(which ps2pdf)
[[ -z $PS2PDF ]] && fail=yes

if [[ $fail = yes ]] 
then
  echo "A requirement is missing: "
  echo "FORMAIL=$FORMAIL"
  echo "MUTTPRINT=$MUTTPRINT"
  echo "PDFTK=$PDFTK"
  echo "PS2PDF=$PS2PDF"
  exit 2
fi
#-------------------------------------------------------------
# do the job
TMPDIR=/tmp/convert-mbox
[[ -d $TMPDIR ]] || mkdir $TMPDIR
OUTFILE_pdf=$(basename $MBOX).pdf

export FILENO=00000
cat $MBOX |\
  $FORMAIL -d \
    -s /bin/bash -c "$MUTTPRINT -p - |  $PS2PDF - $TMPDIR/f\$FILENO.pdf"
pdftk $TMPDIR/f*.pdf output $OUTFILE_pdf
[[ -f $OUTFILE_pdf ]] && echo "Created $OUTFILE_pdf from $MBOX"
[[ -z $show ]] || acroread $OUTFILE_pdf

\rm -rf $TMPDIR

Leave a Reply