from email import policy from email.parser import BytesParser original_msg = b'Date: Fri, 09 Apr 2021 00:00:00 -0000\nFrom: First Last \nTo: "John Doe (Company)" \nSubject: Multi-Part Message\nMessage-ID: \nReply-To: First Last \nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary===--12345--12345--12345--==\n\n\n--==--12345--12345--12345--==\nContent-type: text/plain; name="attachment-1"\n\nContents of attachment #1 with just plain ascii text\n--==--12345--12345--12345--==\nContent-type: text/plain; name="attachment-2"\n\nContents of attachment #2 which contains utf-8 encoded text such as \xc3\xa9\n--==--12345--12345--12345--==--\n' my_8bit_policy = policy.default.clone(cte_type='8bit', utf8=True, refold_source='none') my_7bit_policy = policy.default.clone(cte_type='7bit', utf8=False, refold_source='none') policies = [my_8bit_policy, my_7bit_policy] my_parser = BytesParser(policy=my_8bit_policy) def print_sep(label=None): if label is not None: print(' {} '.format(label).center(70, '-')) else: print('{}'.format('-'*70)) def run_test(msg, policies): for p in policies: print("Convert message using as_bytes() with policy: ", p) try: _ = msg.as_bytes(policy=p) print("Success") except Exception as err: print("Failed - Exception:", err) msg = my_parser.parsebytes(original_msg) print_sep("Original Message") print(original_msg.decode('utf-8')) print_sep("Original 'as_string()'") print(msg.as_string()) print_sep("Additional observations") print("List of defects in original message: ", msg.defects) print("Any 'preamble': ", msg.preamble) print_sep("Test #1: Running as_bytes() test on original message") print("Boundary: ", msg.get_boundary()) run_test(msg, policies) print_sep("Test #2: Correcting boundary and re-running test") msg.set_boundary('==--12345--12345--12345--==') print("Boundary: ", msg.get_boundary()) run_test(msg, policies) print_sep("Test #3: Re-parsing corrected boundary and re-running test") new_msg = original_msg.replace(b'boundary===--12345--12345--12345--==', b'boundary="==--12345--12345--12345--=="') msg = my_parser.parsebytes(new_msg) print("Boundary: ", msg.get_boundary()) run_test(msg, policies) print_sep("Test #4: What would happen if original was not multipart?") simple_msg = new_msg.replace(b'Content-Type: multipart/mixed; boundary="==--12345--12345--12345--=="', b'Content-Type: text/plain;') msg = my_parser.parsebytes(simple_msg) run_test(msg, policies) print_sep("Test #5: Fully correcting original messsage") corrected_msg = new_msg.replace(b'Content-type: text/plain; name="attachment-2"', b'Content-type: text/plain; charset=utf-8; name="attachment-2"\nContent-Transfer-Encoding: 8bit') msg = my_parser.parsebytes(corrected_msg) run_test(msg, policies)