HTML mail with mutt using Markdown

Mutt is my favourite email client, as email is primarily about text I don't personally see much need for a GUI client (although if I had to use one it would be Thunderbird with Muttator).

Sending emails in HTML from Mutt isn't really possible, however rjbs wrote a script in 2007 to reformat mails from mutt and add HTML using Markdown. This was a good start, but unfortunately it's external to mutt so there's no way of previewing what the HTML will look like, or skipping it for certain recipients within mutt.

Unfortunately mutt makes it rather hard to filter parts, but I was determined, so I came up with the slightly hacky solution of changing the editor setting to a script, then editing the mail (all from a macro).

So, I have something like the following in my .muttrc:

set editor=email-editor
macro compose B ":set<enter>E:set editor=email-editor<enter>a/tmp/html-markdown-alternative.html<enter>^Du"
#send-hook . 'push B'

As you can see this uses two scripts, one run as the fake editor as mentioned above, and another one as the sendmail program.

The text2mime-markdown script renders the Markdown in the message as HTML to a file, then mutt is told to attach the part to the email via the macro (yes, there's probably a security hole here if you use this on a shared system; don't, or at least change the location to your home directory).

Then when you actually send the message text2mime-sendmail intercepts the call to your normal system sendmail and looks for the magic filename html-markdown-alternative.html, if it sees that, then it magically alters the email to be a multipart/alternative message rather than the multipart/mixed mutt normally generates. Like this whole set-up that script is incredibly hacky as it relies on the MIME format mutt generates, but it seems to work.

Although hopefully obvious what email-editor above means; in my case it is a very simple little shell script, which I wrote because my editor setting in my .muttrc was a little unwieldy:

if grep -q In-Reply-To $1; then
  # Jump to first line of message
  exec vim -c 'norm }j' $1
  # Enter insert mode on the To: line
  exec vim -c '/^To:' -c 'star!' $1

Once all this is set-up actually sending an email in HTML is simply a matter of typing Markdown in your editor, then either pressing B at the compose screen, or uncommenting the send-hook above to do it for every mail (assuming you have autoedit and fast_reply set, probably).

One slightly annoying thing is Markdown treats the '>' character as a blockquote, which isn't really how I like quoted sections to render in emails, I haven't yet fixed this.

This hack currently assumes you use an external sendmail program, I've not tried the recent additions to mutt that do direct SMTP.