在mutt中使用python创建多部分/备用邮件

问题描述 投票:5回答:2

[我想使用Markdown格式创建text/plain消息,并将其转换为multipart/alternative消息,其中已从Markdown生成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中,您仅在撰写时才将邮件正文发送到filter命令(不包括标头)。一旦我开始工作,我认为降价部分就不会太难。

python mime mutt
2个回答
1
投票

Update:有人写了一篇文章,介绍如何配置mutt与python脚本一起使用。我从来没有做过。 hashcash and 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'] = "[email protected]"
msg['To'] = "[email protected]"

# 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: [email protected]
To: [email protected]

--===============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==--

0
投票

看起来像Mutt 1.13可以从外部脚本创建multipart/alternativehttp://www.mutt.org/relnotes/1.13/

© www.soinside.com 2019 - 2024. All rights reserved.