5

我想text/plain使用 Markdown 格式创建一条消息,并将其转换为从 Markdown 生成部分的multipart/alternative消息。text/html我尝试使用 filter 命令通过创建消息的 python 程序对其进行过滤,但似乎消息没有正确发送。代码如下(这只是测试代码,看看我是否可以multipart/alternative发送消息。

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()

不幸的是,在 mutt 中,您只能在撰写时将消息正文发送到过滤器命令(不包括标题)。一旦我得到这个工作,我认为降价部分不会太难。

4

2 回答 2

1

更新:有人写了一篇关于配置 mutt 以与 python 脚本一起使用的文章。我自己从来没有做过。hashcash 和 mutt,文章详细介绍了 muttrc 的配置,并给出了代码示例。


旧答案

它解决了你的问题吗?

#!/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()

我们保存并测试程序。

python test-email.py 

这使:

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==--
于 2013-03-30T02:41:25.930 回答
1

看起来 Mutt 1.13 能够multipart/alternative从外部脚本创建一个。http://www.mutt.org/relnotes/1.13/

于 2019-12-01T14:45:16.770 回答