在python中使用电子邮件库时修改multipart标头

时间:2012-02-19 23:41:43

标签: python email

我正在尝试使用email.mime来生成各种在发送电子邮件时使用的标头配置。

到目前为止,我一直在使用: -

from email.mime.text import MIMEText

删除,添加和修改标题。我现在开始尝试使用multipart标头并使用: -

from email.mime.multipart import MIMEMultiPart

msg = MIMEMultipart('alternative')
text = 'add this text to the multipart'
textMIME = MIMEText(text, 'plain')
msg.attach(MIMEText)

text2 = 'add this text to the next multipart'
text2MIME = MIMEText(text2, 'plain')
msg.attach(MIME2Text)

它给出了类似于: -

的输出
Return-Path: xxxxx
X-Original-To: xxxxxxxxx
Delivered-To: xxxxxxx
Received: from xxxxxx
        to xxxxxxxxx
        for xxxxxxxxx
Content-Type: multipart/alternative;
 boundary="==========yyyyyyyy=="
MIME-Version: 1.0
Message-Id: xxxxxxx
Date: xxxxxx
From: xxxxx
To: xxxxx

--==========yyyyyyyy==
Content-Type: xxxxxxx
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

add this text to the multipart

--==========yyyyyyyy==
Content-Type: xxxxxxx
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

add this text to the next multipart
--==========yyyyyyyy==

这似乎是合理的直接使用,除了一些我无法解决的事情。

我无法解决的问题是: -

  1. 如何添加文本以使其显示在“To:xxxxx”标题之后和第一个多部分边界之前?当我使用msg.attach时,它首先在写入文本之前添加默认标头。我也尝试了msg.add_header('','我的文字'),但这给了我一个标题格式的文本,即':my text'而不仅仅是纯文本。

  2. 如何专门修改“内容类型”等多部分标题?当我尝试删除像'Content-Type'这样的标题时(无论我把它放在代码中的哪个位置),它会自动删除它找到的第一个标题值,这是第一个'Content-Type:multipart / alternative;  边界= “========== YYYYYYYY ==””。是否可以指定要修改的特定多部分中的哪个标题?

  3. 感谢。

0 个答案:

没有答案