5

I'd like to create a text/plain message using Markdown formatting and transform that into a multipart/alternative message where the text/html part has been generated from the Markdown. I've tried using the filter command to filter this through a python program that creates the message, but it seems that the message doesn't get sent through properly. The code is below (this is just test code to see if I can make multipart/alternative messages at all.

import sys
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

html = """<html>
          <body>
          This is <i>HTML</i>
          </body>
          </html>
"""

msgbody = sys.stdin.read()

newmsg = MIMEMultipart("alternative")

plain = MIMEText(msgbody, "plain")
plain["Content-Disposition"] = "inline"

html = MIMEText(html, "html")
html["Content-Disposition"] = "inline"

newmsg.attach(plain)
newmsg.attach(html)

print newmsg.as_string()

Unfortunately, in mutt, you only get the message body sent to the filter command when you compose (the headers are not included). Once I get this working, I think the markdown part won't be too hard.

karlcow
  • 6,977
  • 4
  • 38
  • 72
Chris W.
  • 302
  • 1
  • 3
  • 10

2 Answers2

2

Looks like Mutt 1.13 has the ability to create a multipart/alternative from an external script. http://www.mutt.org/relnotes/1.13/

Chris W.
  • 302
  • 1
  • 3
  • 10
1

Update: Someone wrote an article on configuring mutt for using with a python script. I have myself never done it. hashcash and mutt, the article goes through the configuration of muttrc and give code example.


Old answer

Does it solve your issue?

#!/usr/bin/env python

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


# create the message
msg = MIMEMultipart('alternative')
msg['Subject'] = "My subject"
msg['From'] = "foo@example.org"
msg['To'] = "bar@example.net"

# Text of the message
html = """<html>
          <body>
          This is <i>HTML</i>
          </body>
          </html>
"""
text="This is HTML"

# Create the two parts
plain = MIMEText(text, 'plain')
html = MIMEText(html, 'html')

# Let's add them
msg.attach(plain)
msg.attach(html)

print msg.as_string()

We save and test the program.

python test-email.py 

Which gives:

Content-Type: multipart/alternative;
 boundary="===============1440898741276032793=="
MIME-Version: 1.0
Subject: My subject
From: foo@example.org
To: bar@example.net

--===============1440898741276032793==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

This is HTML
--===============1440898741276032793==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

<html>
          <body>
          This is <i>HTML</i>
          </body>
          </html>

--===============1440898741276032793==--
karlcow
  • 6,977
  • 4
  • 38
  • 72
  • I can get the program to run, but I think my problem is that mutt doesn't allow filtering of a message with headers during compose. I think doing this will require a change to mutt. – Chris W. Apr 04 '13 at 19:31
  • so it is not a python issue. I'm confused. – karlcow Apr 06 '13 at 14:54
  • I suppose the question should be 'how can I run this script in mutt such that a `multipart/alternative` message will be produced'? – Chris W. Apr 08 '13 at 21:04