26

Sometimes I need to send a fragment of code on google-group inline. Text does not help here; I can type it in markdown, convert it to html (using pandoc etc.), attach to mutt as text/html and send it.

There is one good solution available here but it uses external sendmail program to send email. I am using mutt which has capabilities to send emails over IMAP by itself.

Dilawar
  • 971
  • 1
    Why not just pipe output from a command-line Markdown formatter to sendmail? – Naftuli Kay Jan 09 '14 at 02:26
  • Hmm.. shared computer! Don't want to store password for external sendmail. – Dilawar Jan 09 '14 at 02:35
  • Is there any examples we can see of what your current results look like on google-groups? – slm Jan 09 '14 at 03:44
  • Also you want to type things up in markdown, but have them rendered prior to attaching them to your emails, right? – slm Jan 09 '14 at 03:53
  • This sounds like what you're asking for, but might need to be modified: https://dgl.cx/2009/03/html-mail-with-mutt-using-markdown. Also markdownmail.py sounded like something you could use. – slm Jan 09 '14 at 03:55
  • @slm, Thank, I is indeed what I am using currently (I have added this link to question also) but the problem is that it uses external sendmail which is ok on my person computer but not on lab computer which is shared. I have tweaked it a bit but I am waiting for a better solution. – Dilawar Jan 09 '14 at 04:54
  • Are you sure that mutt sends emails over IMAP? –  Jan 09 '14 at 15:55
  • @bersch I am not sure about terminology. It uses smtp_user and smtp_password variables. I think that smtp is part of IMAP. Is it not? – Dilawar Jan 10 '14 at 05:28
  • @Dilawar The Internet Message Access Protocol (IMAP) is a protocol for e-mail storage and retrieval. E-mail transfer is handled by the Simple Mail Transfer Protocol (SMTP). They are distinct protocols. – Thomas Nyman Jan 10 '14 at 05:44

4 Answers4

39

After you compose a message, but before sending you have lots of options available to you. Press ? to view them.

Some that may help here:

  • F to filter the attachment through an external processor
    • Use pandoc -s -f markdown -t html to convert to HTML
  • ^T to edit the attachment MIME type
    • Change from text/plain to text/html.

Now a macro that will do everything in one step. Add this to your .muttrc:

macro compose \e5 "F pandoc -s -f markdown -t html \ny^T^Utext/html; charset=utf-8\n"
set wait_key=no

To use this macro, after you have finished composing your message but before you send, press Esc then 5 to convert your markdown formatted message into HTML.

You can naturally customize this macro as you see fit. Mutt has lots of key bindings already built in, so whatever key sequence you choose to bind to, make sure it doesn't overwrite something else (or it's something you can live without).


The option set wait_key=no suppresses Mutt's Press any key to continue... prompt when external commands are run. If wait_key is yes (which is the default) you'll have to press Esc, then 5, then any other key to continue.

bahamat
  • 39,666
  • 4
  • 75
  • 104
  • 1
    This is a really elegant solution! +1 – sinisterstuf Sep 23 '15 at 09:51
  • 7
    this is nice, but it's got core flaw. it eliminates the plaintext part of the email which makes it suck to read in clients like ... mutt ;) HTML emails should have a plaintext and html component. the raw markdown should be the plaintext, the converted should be the HTML. – masukomi Jun 17 '16 at 15:27
  • 1
    Agree with @masukomi, email clients in general send both html and text versions of the email. It would be nice to have a macro that adds the html version and leaves the original as text/plain. – oblitum Aug 25 '17 at 17:30
  • Nevermind, it seems the issue is more complicated than expected: https://github.com/neomutt/neomutt/issues/587 – oblitum Aug 26 '17 at 10:13
  • 2
    In the end I've build my own setup for this http://nosubstance.me/post/mutt-secret-sauce/ – oblitum Sep 20 '17 at 17:37
  • @pepper_chico I've tried running the example of MIMEmbellish linked on your page but it fails with (23) Failed writing body. Do you still use it? – Rastapopoulos Nov 16 '18 at 11:56
  • 1
    @Rastapopoulos yes: https://github.com/oblitum/dotfiles/issues/3#issuecomment-437954693 – oblitum Nov 16 '18 at 12:21
  • @pepper_chico Thanks! Looks amazing! I'll try it out! – Rastapopoulos Nov 17 '18 at 13:02
  • 1
    I email to work (mutt and webmail) had to change: charset=us-ascii To charset=utf-8 – Tony Barganski Feb 03 '20 at 05:02
  • @TonyBarganski Thanks for the tip. Since this answer is more than 6 years old, it's due for an update! – bahamat Feb 03 '20 at 18:31
  • auto_view text/html in .muttrc will use w3m to render incoming html emails in plain text as formatted by the html. That's quite passable for me. There's other ways to interact, including using a browser-like experience. https://www.sendmail.org/~ca/email/mutt/manual-5.html – Josh Vander Hook May 04 '21 at 21:05
5

You can send e-mails also as a multipart/alternative containing both text/plain and text/html.

Requirements: pandoc

Basically it creates from markdown message plaintext and html5. Creates attachments from those parts, marks them as inline attachments, set correct mime type and combine them into mutlipart message.

Any other attachments are supposed to be added after running this macro in compose menu. Optionally signing/encrypting message should be done as the final step

macro compose ,m \
"<enter-command>set pipe_decode<enter>\
<pipe-message>pandoc -f gfm -t plain -o /tmp/msg.txt<enter>\
<pipe-message>pandoc -s -f gfm -t html5 -o /tmp/msg.html<enter>\
<enter-command>unset pipe_decode<enter>a^U/tmp/msg.txt\n^Da^U/tmp/msg.html\n^D^T^Utext/html; charset=utf-8\n=DTT&d^U\n" \
"Convert markdown gfm to HTML and plain" 
Jakub Jindra
  • 1,462
1

Sendmail is often not flexible enogh for sending mails.

I use msmtp together with mutt on particular accounts for flexible SMTP.

To use it with mutt change:

# ~/.muttrc  
set sendmail="/usr/bin/msmtp -a default"   

and

# ~/.msmtprc  
defaults
tls off
logfile ~/.msmtp.log  
account default   
host your.smtp.host  
port 25  
from your-user-name@your-host.com  
auth off  
user username  
password password  
1

I can send email in any format using neomutt. I just use Emacs (org-mode), instead of vim. Although, I am a vim user as well. But, I mostly use Emacs with evil-mode.

In my .muttrc I have set up the editor to be emacs instead of vim. When writing a new email, neomutt fires emacs up. Then I call "org-mode", write the message, and export to whatever format I want.

I can export to PDF format. Then I save it and attach the PDF file in my /tmp. After that I can send to whomever.

If I want the html format, I export that in the same way and I automatically can see the output, before sending the email.

Apart from that, there are many other export formats in org-mode. Just, choose what you want. For sending code to other people, just add the source code to whatever language you want. Everything is explained in the org-wiki.

Kevdog777
  • 3,224
Achylles
  • 11
  • 2