重新组装以“消息/部分”编码的电子邮件消息使用Python

发布于 2024-09-07 17:42:00 字数 2310 浏览 6 评论 0原文

有没有办法在用 Content-Type: message/partial 编码的 Python 电子邮件中重新组装(即 RFC 1521)?

特别是,给定一组 电子邮件,如何将它们合并回原创的?即

emails = [...] # a  list of `email`.

reassembled_email = merge_emails(emails)

merge_emails 有什么作用?有Python项目做过这个吗?

人们可以期待这样一封电子邮件:

From: [email protected]
To: [email protected]
Date: Wed, 30 Jun 2010 14:19:45 -0400
MIME-Version: 1.0
Content-Type: message/partial;
 id="TAN_U_R<0.0000749046c4>";
 number=1;
 total=2

From: [email protected]
Subject:
To: [email protected]
Date: Wed, 30 Jun 2010 14:19:45 -0400
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="DC_BOUND_PRE_<1277921980.0000744>"

This is a multi-part message in MIME format.
--DC_BOUND_PRE_<1277921980.0000c4>
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

This E-mail was sent from Your Printer

Some random text.

--DC_BOUND_PRE_<1277921980.0000744>
Content-Type: application/pdf; name="abcdef.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="abcdef.pdf"

JVBERi0xLjMKJZKgoooKNCAwIG9iago8PC9UeXBlL1hPYmplY3QKL1N1YnR5cGUvSW1hZ2UK
... 

这是我最初的想法:

from email import parser 

def merge_emails(emails):
   # we can presume emails are ordered correctly and all the parts accounted for

   content = ''

   for eml im emails:
      content += emails.get_payload()

   return parser.Parser().parsestr(content)

这行得通吗(有那么简单吗)?您如何重新组合这封电子邮件?

作为在 Python 中执行此操作的替代方法,是否有命令行 Unix/Mac OS X 程序可以执行此操作?

感谢您的阅读以及您可能提供的任何信息。

亲切的问候,

布莱恩

Is there a way to reassemble in Python email messages that are encoded with Content-Type: message/partial (i.e. section '7.3.2. The Message/Partial subtype' of RFC 1521)?

In particular, given a set of emails, how can one merge them back into an original? i.e.

emails = [...] # a  list of `email`.

reassembled_email = merge_emails(emails)

What does merge_emails have to do? Has any Python project done this?

One can expect an email like-so:

From: [email protected]
To: [email protected]
Date: Wed, 30 Jun 2010 14:19:45 -0400
MIME-Version: 1.0
Content-Type: message/partial;
 id="TAN_U_R<0.0000749046c4>";
 number=1;
 total=2

From: [email protected]
Subject:
To: [email protected]
Date: Wed, 30 Jun 2010 14:19:45 -0400
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="DC_BOUND_PRE_<1277921980.0000744>"

This is a multi-part message in MIME format.
--DC_BOUND_PRE_<1277921980.0000c4>
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

This E-mail was sent from Your Printer

Some random text.

--DC_BOUND_PRE_<1277921980.0000744>
Content-Type: application/pdf; name="abcdef.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="abcdef.pdf"

JVBERi0xLjMKJZKgoooKNCAwIG9iago8PC9UeXBlL1hPYmplY3QKL1N1YnR5cGUvSW1hZ2UK
... 

Here's my initial thought:

from email import parser 

def merge_emails(emails):
   # we can presume emails are ordered correctly and all the parts accounted for

   content = ''

   for eml im emails:
      content += emails.get_payload()

   return parser.Parser().parsestr(content)

Will this work (is it that simple)? How can you one reassemble this email?

As an alternative to doing this in Python, is there a command-line Unix/Mac OS X program that will do it?

Thank you for reading and any information you may be able to provide.

Kind regards,

Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-09-14 17:42:00

以下对我有用:(Ubuntu Linux + Thunderbird)

  • 将每条消息保存到一个文件夹(例如:part1.dat、part2.dat...partN.dat) - 顺序很重要
  • 合并文件:catpart1.datpart2。数据部分N.dat > mail.eml
  • 使用 Thunderbird 打开该文件。 Thunderbird 可以理解 .eml 文件并将其作为电子邮件打开(完全组装)

Following works for me: (Ubuntu Linux + Thunderbird)

  • Save each of the messages to a folder (e.g: part1.dat, part2.dat...partN.dat) - Order is important
  • Merge the files: cat part1.dat part2.dat partN.dat > mail.eml
  • Open the file with Thunderbird. Thunderbird can understand .eml files and opens it as an email (fully assembled)
眼中杀气 2024-09-14 17:42:00

虽然不是 Python 解决方案,但程序 uudeview 在重新组装 消息/部分电子邮件。

While not a Python solution, the program uudeview has been very helpful in reassembling message/partial email.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文