gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:05 24 4
gpt4 key购买 nike

我正在使用标准输入读取邮件

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 = maildir.Maildir('~/mail')

但是,我在 ~/mail 中收到一 strip 有两个附件 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--

最佳答案

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

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 来创建邮箱(如果邮箱不存在)。 创建=True

运行上面的代码并检查邮件 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==--

关于python - 使用Python修改电子邮件(email.Message)并添加附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36133139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com