在mutt中使用python创建多部分/备选邮件
我想使用标记格式创建一条在mutt中使用python创建多部分/备选邮件,python,mime,mutt,Python,Mime,Mutt,我想使用标记格式创建一条text/plain消息,并将其转换为multipart/alternative消息,其中text/html部分是通过标记生成的。 我尝试使用filter命令通过创建消息的python程序对其进行过滤,但消息似乎没有正确发送。下面是代码(这只是测试代码,看看我是否可以生成multipart/alternative消息 import sys from email.mime.text import MIMEText from email.mime.multipart impo
text/plain
消息,并将其转换为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()
导入系统
从email.mime.text导入MIMEText
从email.mime.multipart导入MIMEMultipart
html=”“”
这是HTML
"""
msgbody=sys.stdin.read()
newmsg=MIMEMultipart(“备选方案”)
普通=米文本(msgbody,“普通”)
普通[“内容处置”]=“内联”
html=MIMEText(html,“html”)
html[“内容处置”]=“内联”
newmsg.attach(普通)
newmsg.attach(html)
打印newmsg.as_string()
不幸的是,在mutt中,只有在编写时才会将消息正文发送到filter命令(不包括标题)。一旦我开始工作,我想降价部分不会太难。更新:有人写了一篇关于配置mutt以便与python脚本一起使用的文章。我自己从来没有这样做过。这篇文章介绍了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()
其中:
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==--
内容类型:多部分/备选;
边界=“=========================1440898741276032793==”
MIME版本:1.0
主题:我的主题
发件人:foo@example.org
致:bar@example.net
--===============1440898741276032793==
内容类型:文本/普通;charset=“us ascii”
MIME版本:1.0
内容传输编码:7bit
这是HTML
--===============1440898741276032793==
内容类型:text/html;charset=“us ascii”
MIME版本:1.0
内容传输编码:7bit
这是HTML
--===============1440898741276032793==--
看起来Mutt 1.13能够从外部脚本创建一个多部分/备选方案
。我可以让程序运行,但我认为我的问题是Mutt不允许在编写过程中过滤带有标题的消息。我认为这样做需要对Mutt进行更改。因此这不是python问题。我感到困惑。我uppose问题应该是“如何在mutt中运行此脚本,以便生成多部分/备选消息”?
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==--