5

Markdown フォーマットを使用してメッセージを作成し、その部分が Markdown から生成されたメッセージにtext/plain変換したいと考えています。filter コマンドを使用して、メッセージを作成する python プログラムでこれをフィルタリングしようとしましたが、メッセージが適切に送信されないようです。コードは以下のとおりです (これは、メッセージを作成できるかどうかを確認するための単なるテスト コードです。multipart/alternativetext/htmlmultipart/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 コマンドに送信されるメッセージ本文のみを取得します (ヘッダーは含まれません)。これが機能するようになれば、マークダウンの部分はそれほど難しくないと思います。

4

2 に答える 2

1

更新: Python スクリプトで使用するための mutt の構成に関する記事を誰かが書きました。私自身はやったことはありません。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 に答える