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



我正在使用 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==--

这就是雷鸟给我的(也是我想要的):

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--

我刚刚尝试了您的代码,它工作得很好,我将提供有效的解决方案。我认为最好将所需的模块类作为唯一的类导入以在代码中使用。如图所示

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==--

相关内容

  • 没有找到相关文章

最新更新