Mutt: open HTML via mailcap

Some agencies insist on sending HTML email without including a decent plain text version under multipart/alternative. To users of mutt or other email clients that do not include a browser, this presents a small problem. One can automatically pipe the HTML through a text-based browser such as elinks, but for some HTML emails the result is hard to navigate and this approach does not suffice.

The obvious solution is to include a rule in your .mailcap file to read text/html files in a full-fledged browser, such as

text/html; firefox %s

However, there is a catch: there is a race condition where the tmpfile /tmp/mutt.html may or may not be deleted before firefox manages to open it. This can be circumvented by using a wrapper: I have a script under ~/bin/firefox-norace with the following contents.

#!/usr/bin/fish

set file $argv[1]
set tmpfile (mktemp --suffix=.html)
cp $file $tmpfile
firefox $tmpfile

Note: I like to use the fish shell for its clean syntax. For POSIX-compliant interpreters, the script would be as follows.

#!/bin/sh

file="$1"
tmpfile="$(mktemp --suffix=.html)"
cp "$file" "$tmpfile"
firefox "$tmpfile"

The relevant rule in .mailcap then reads

text/html; ~/bin/firefox-norace %s

This allows me to easily read HTML mail from mutt by pressing v (view-attachments), navigating to the HTML file and pressing Enter.