Pretty mail with Emacs

I don’t normally care too much about HTML email, as reading it in Emacs (using Notmuch to index and browse it) does not really work too well.

Note: \@til had this idea that the solution to this could be - at least on platforms that use GKT as toolkit - to provide for a WebView widget in Emacs proper, controllable via Elisp. This might become involved quickly though, and it is uncertain if something like this would ever make it into upstream. (Just speculating here of course, but with RMS’ somewhat justified skepticism of web tech it seems unlikely such a patch would get accepted). Maybe some other solution based on piping the mime part to an external process (such as vimb or uzbl) could be used?. That is still to be found out…

Then I stumbled over this article by Edward O’Connor and decided to try out writing Email in Markdown, converting it to plain-text and HTML parts using pandoc just before sending it all off.

Setup

I wrote a little shell script to convert stdin to the correct formats usable in message-mode:

#!/bin/bash

TEXT=`cat`
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
PLAIN=$(echo "$TEXT" | pandoc -f markdown -t plain)
HTML=$(echo "$TEXT" | pandoc -f markdown -t html)
CSS=$(cat $DIR/mime.css)


cat <<EOF
<#multipart type=alternative>
$PLAIN
<#part type=text/html>
<html>
  <head>
    <title></title>
    <style>
      $CSS
    </style>
  </head>
  <body>
    $HTML
  </body>
</html>
<#/multipart>
EOF

Then I added a modified version of the Elisp on Edward’s blog to my init.el:

(defun convert-message ()
  (interactive)
  (save-excursion
    (message-goto-body)
    (shell-command-on-region (point) (point-max) "~/.emacs.d/expand-mime.sh" nil t)))

This function basically navigates the to the body of the message, then pipes everything between the current point of the cursor until the end of the buffer to shell-command-on-region. This in turn invokes the little script above on its input, yanking back the result into the message buffer.

Finally, when I had everything tested and working, I added the convert-message function to the message-send-hook like so:

(custom-set-variables
 ;;  ..your stuff 
 '(message-send-hook '(notmuch-message-mark-replied convert-message)))

Now, every time I hit C-c C-c in message-mode I get both plain text and HTML parts of my message. Nifty, innit? :)

Ideas

I forgot to mention the little CSS file which gets interpolated into the HTML <head> section. Of course it could also be included in the shell script directly, but I tend to like things broken up like that, just because its possible to use the regular editing facilities when working on it (e.g. highlighting in css-mode).

Also, wouldn’t it be nice to have syntax highlighting for code blocks inline? It might be possible! Same goes for Literate Programming, nice inline images etc.

Update

Apparently there is an branch with code to allow for embedding XEmbed-aware applications into Emacs that is still activly maintained (looking at the bug tracker at least).