开发者

Re-assemble email messages encoded in 'message/partial' with Python

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: me@example.com
To: me2@example.com
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: me@example.com
Subject:
To: me2@example.com
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_<开发者_StackOverflow中文版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


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)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜