[RFC] writing HTML email with notmuch

Jayanth R Varma jrvarma at gmail.com
Wed Feb 27 23:23:21 PST 2019


Antoine Beaupr? <anarcat at orangeseeds.org> writes:

> The idea is that you write your email normally, but in 
> markdown. When
> you're done writing that email, you launch the above function 
> (carefully
> bound to "M-x anarcat/notmuch-html-convert" here) which takes 
> that email
> and adds an equivalent HTML part to it. You can then even tweak 
> that
> part to screw around with the raw HTML if you feel depressed or
> nostalgic.
>
> What do people think? Am I insane? Could this work? Does this 
> belong in
> notmuch? Or maybe in the tips section? Should I seek therapy? Do 
> you
> hate markdown? Expand on the relationship between your parents 
> and text
> editors.


I do something similar routinely. I have bound the C-c C-c key to 
a function that presents me with four choices:

1. Send as plain text

2. Send as HTML

3. Preview HTML in emacs (with w3m)

4. Preview HTML with external browser

For a few weeks, I suggest religiously previewing HTML in external 
browser before sending any HTML mail because there are a lot of 
things that can go wrong (more on this below). After most of the 
wrinkles are ironed out, you can switch to previewing in 
emacs-w3m. When you are really sure that everything is working, 
you can send most simple mails as HTML without preview, but I 
would still suggest previewing complex mails (for example with 
tables, embedded code and so on). 

What can go wrong:

1. Lazy blockquote in markdown means that you must leave a blank 
line between a quoted message and your response, or else your 
response will also be part of the blockquote. It is so easy to 
slip up on this in email. In fact, I am considering adding some 
code to insert this blank line automatically.

2. Signatures: Since you know your own signature, it is best to 
search for an exact match for it rather than rely on some 
heuristics.

3. Attachments. In plain text mail, attachments can be 
anywhere. Many years ago, I once inserted an attachment by mistake 
mid-sentence, but the email was sent and received perfectly (apart 
from an extra line break). So you have to be careful while 
converting to HTML. My method is to use a regex search to find all 
attachments and move them to the end of the email. So the 
converted message begins with a multi-part/alternative with plain 
text and html alternatives at the top. This is followed by the 
signature and the attachments. Since these are now all plain text 
parts (the email default), there is no need for any <pre> tags for 
the signature.

Conversion to HTML

1. I use pandoc for the conversion because it gives complete 
ability to customize the exact flavour of markdown that you want 
(for example, table formats).

2. Although everything can surely be done in elisp, I prefer to 
delegate most of the pre-processing and previewing to a external 
(python) script which orchestrates everything: creating temp 
files, invoking pandoc, running external browsers and cleaning up 
at the end.

3. I ensure that previewing the HTML does not change the message 
buffer at all (everything happens in other buffers or in temp 
files). So you can keep tweaking the markdown till you get 
everything right, and there is no need to tweak the raw HTML.

PS: I am also a plain text guy. But in email, plain text does not 
mean plain text (as in unicode), it actually means 7-bit ASCII. So 
if you want a euro or yen symbol, Greek letters or mathematical 
symbols, you need HTML just as a wrapper to provide UTF encoding. 

PPS: If you are writing markdown and sending both plain text and 
HTML alternatives, you are not really a sinner. Only the recipient 
who chooses to view the HTML part is sinning (and even that sin 
can be condoned if the recipient uses notmuch/emacs to render the 
HTML).


More information about the notmuch mailing list