使用Python修改电子邮件(email.Message)并添加附件

时间:2016-03-21 14:04:10

标签: python email attachment mime multipart

我正在使用

从stdin读取邮件
message = mailbox.email.message_from_file(sys.stdin)

并想要添加文本文件附件。我尝试了以下方法:

new_msg = email.mime.multipart.MIMEMultipart('related')

old_msg = email.mime.message.MIMEMessage(message)
new_msg.attach(old_msg)

att_msg = email.mime.text.MIMEText("Textfile attachment")
att_msg.add_header('Content-Disposition', 'attachment', filename= 'my_attachment.txt')
new_msg.attach(att_msg)

maildir.add(new_msg)

其中maildir = mailbox.Maildir('~/mail')

但是,我在~/mail收到一封邮件,其中包含两个附件ForwardedMessage.emlmy_attachment.txt

我的目标是拥有原始邮件(具有相同的标题),但附加了文本文件。

编辑让我举个例子。原始信息:

To: foo@bar.com
From: User <user@mydomain.net>
Message-ID: <56F2AAD2.7030408@mydomain.net>
Date: Wed, 23 Mar 2016 15:40:18 +0100
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit

Testmessage

用我的代码:

Content-Type: multipart/related; boundary="===============7892775444970429949=="
MIME-Version: 1.0

--===============7892775444970429949==
Content-Type: message/rfc822
MIME-Version: 1.0

To: foo@bar.com
From: User <user@mydomain.net>
Message-ID: <56F2AAD2.7030408@mydomain.net>
Date: Wed, 23 Mar 2016 15:40:18 +0100
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit

Testmessage

--===============7892775444970429949==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="atach.txt"

Textfile attachment
--===============7892775444970429949==--

这就是Thunderbird给我的(以及我想要的):

To: foo@bar.com
From: User <user@mydomain.net>
Message-ID: <56F2AAD2.7030408@mydomain.net>
Date: Wed, 23 Mar 2016 15:40:18 +0100
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------010607020403070301060303"

This is a multi-part message in MIME format.
--------------010607020403070301060303
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit

Testmessage

--------------010607020403070301060303
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="atach.txt"

Textfile attachment
--------------010607020403070301060303--

1 个答案:

答案 0 :(得分:1)

我刚试过你的代码,它完美无缺,我将提供有效的解决方案。我认为最好将所需的模块类导入为代码中使用的唯一类。如图所示

import sys
import mailbox
import email
from email.mime.multipart import MIMEMultipart
from email.mime.message import MIMEMessage
from email.mime.text import MIMEText

message = mailbox.email.message_from_file(sys.stdin)
maildir = mailbox.Maildir('./mail',create=True)
new_msg = MIMEMultipart('related')

old_msg = MIMEMessage(message)
new_msg.attach(old_msg)

att_msg = MIMEText("Textfile attachment")
att_msg.add_header('Content-Disposition', 'attachment',filename='atach.txt')
new_msg.attach(att_msg)

maildir.add(new_msg)

如果邮箱不存在,我还传递了一个额外的关键字arg来创建邮箱。 创建=真即可。

运行以上并检查邮件Dir给了我以下内容我希望这就是你想要的。

Content-Type: multipart/related; boundary="===============2731426334901210480=="
MIME-Version: 1.0

--===============2731426334901210480==
Content-Type: message/rfc822
MIME-Version: 1.0


Hello trial 2

--===============2731426334901210480==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="atach.txt"

Textfile attachment
--===============2731426334901210480==--