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