并非所有MIME文本都显示在邮件服务器上

时间:2014-05-05 03:38:51

标签: python email mime

我正在使用Python email.mime lib来编写电子邮件,我创建了两个MIMEText对象,然后将它们作为文本(而不是附件)附加到Message上,结果我得到了如下的MIME文档,就像你一样可以看到有两个文本对象,一个是plain类型,另一个是html类型,我的问题是我只能看到一些邮件客户端中的后一个文本对象(这里是html),而我可以看到两个文本某些其他邮件客户端中的对象(例如,live.com),是什么导致了这个?

Content-Type: multipart/mixed; boundary="===============0542266593=="
MIME-Version: 1.0
FROM: john.smith@NYU.com
TO: john.smith@live.com, john.smith@gmail.com
SUBJECT: =?utf-8?q?A_Greeting_From_Postman?=

--===============0542266593==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

SGkhCkhvdyBhcmUgeW91PwpIZXJlIGlzIHRoZSBsaW5rIHlvdSB3YW50ZWQ6Cmh0dHA6Ly93d3cu
cHl0aG9uLm9yZw==

--===============0542266593==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

ICAgICAgICA8aHRtbD4KICAgICAgICAgIDxoZWFkPjwvaGVhZD4KICAgICAgICAgIDxib2R5Pgog
ICAgICAgICAgICA8cD5IaSE8YnI+CiAgICAgICAgICAgICAgIEhvdyBhcmUgeW91Pzxicj4KICAg
ICAgICAgICAgICAgSGVyZSBpcyB0aGUgPGEgaHJlZj0iaHR0cDovL3d3dy5weXRob24ub3JnIj5s
aW5rPC9hPiB5b3Ugd2FudGVkLgogICAgICAgICAgICA8L3A+CiAgICAgICAgICA8L2JvZHk+CiAg
ICAgICAgPC9odG1sPgogICAgICAgIA==

--===============0542266593==--

1 个答案:

答案 0 :(得分:0)

您已将'multipart / mixed'指定为mime类型。如果您只想显示一个项目,请指定“multipart / alternative”,如下所示:

email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.encoders import encode_base64

# Note: 'alternative' means only display one of the items. 
msg = MIMEMultipart('alternative')
msg['Subject'] = "Hello"
msg['From'] = 'me@example.com'
msg['To'] = 'you@example.com'

msg.attach(MIMEText('Hello!', 'plain'))
msg.attach(MIMEText('<b>Hello!</b>', 'html'))

# Not required, but you had it in your example, so I kept it.
for i in msg.get_payload():
    encode_base64(i)

print msg.as_string()