【问题标题】:Using python in mutt for creating multipart/alternative mails在 mutt 中使用 python 创建多部分/替代邮件
【发布时间】:2013-01-17 10:14:08
【问题描述】:

我想使用 Markdown 格式创建 text/plain 消息并将其转换为 multipart/alternative 消息,其中 text/html 部分已从 Markdown 生成。 我尝试使用 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 中,您只能在撰写时获得发送到过滤器命令的消息正文(不包括标题)。一旦我得到这个工作,我认为降价部分不会太难。

【问题讨论】:

    标签: python mime mutt


    【解决方案1】:

    更新:有人写了一篇关于配置 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'] = "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==--
    

    【讨论】:

    • 我可以让程序运行,但我认为我的问题是 mutt 不允许在撰写期间过滤带有标题的消息。我认为这样做需要对 mutt 进行更改。
    • 所以这不是 python 问题。我很困惑。
    • 我想问题应该是'我怎样才能在 mutt 中运行这个脚本以便产生multipart/alternative 消息'?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2015-07-21
      • 1970-01-01
      • 2011-04-23
      • 2019-05-06
      • 2013-05-30
      • 2014-01-23
      • 2011-05-20
      • 2014-06-21
      • 1970-01-01
      相关资源
      最近更新 更多