import email.message from email import message_from_bytes from email.policy import default reference_ids = [ '<BN8PR17MB27372595A957D7912CEE184FBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', '<CAM9Ku=FZ5RGMvw3VzNrZz+DA78zyq2Am8fz9JNLmjUQ9ZEXpDQ@mail.gmail.com>', '<BN8PR17MB27371C71A65834531DF028BBBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', # As a test, copied I successively shortened this one # and sure enough, once it gets short enough, it no longer gets encoded # '<BNPR17MB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', # '<BNR17MB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', # '<BN17MB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', # '<BN7MB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', # '<BNMB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', # '<BNB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', # '<BN27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', # '<BN7377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', # '<BN377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>', '<CAM9Ku=E1wmpj=AMRhsh-Sk1RHqmK_x-J5ey8szVehefYQvn13w@mail.gmail.com>'] in_reply_to = reference_ids[0] class Request: """fugly hack to let the code below work as in the original question""" def __init__(self): self.vars = self self.email = "sender@example.org" self.to = "recipient@example.net" self.cc = "cc.recipient@example.com" self.bcc = "bcc.recipient@example.int" self.subject = "I have seen the fnörds" self.message = """\ LANGUAGE (NATIVE NAME) HELLO ---------------------- ----- Japanese (日本語) こんにちは / コンニチハ Chinese (中文,普通话,汉语) 你好 Cantonese (粵語,廣東話) 早晨, 你好 Korean (한글) 안녕하세요 / 안녕하십니까 """ request = Request() es = request msg = email.message.EmailMessage() if reference_ids is not None: # no parentheses necessary, this is not C msg.add_header('In-Reply-To', in_reply_to) msg.add_header('References', (' ').join(reference_ids)) msg['Subject'] = request.vars.subject msg['From'] = es.email msg['To'] = request.vars.to msg['CC'] = request.vars.cc msg['BCC'] = request.vars.bcc msg.set_content(request.vars.message) print('** Original') print(msg.as_string()) print("-" * 72) copy = message_from_bytes(msg.as_string().encode('us-ascii')) print('** Original, from message_from_bytes') print(copy.as_string()) print("-" * 72) custom_policy = default.clone(max_line_length=100) msg = email.message.EmailMessage(policy=custom_policy) if (reference_ids is not None): msg.add_header('In-Reply-To', in_reply_to) msg.add_header('References', (' ').join(reference_ids)) msg['Subject'] = request.vars.subject msg['From'] = es.email msg['To'] = request.vars.to msg['CC'] = request.vars.cc msg['BCC'] = request.vars.bcc msg.set_content(request.vars.message) print('** Fixed, with custom policy') print(msg.as_string()) print("-" * 72) copy = message_from_bytes(msg.as_string().encode('us-ascii'), policy=custom_policy) print('** Fixed, with custom policy; from message_from_bytes') print(copy.as_string())
Standard input is empty
** Original In-Reply-To: =?utf-8?q?=3CBN8PR17MB27372595A957D7912CEE184FBF6F9=40BN8PR17MB?= =?utf-8?q?2737=2Enamprd17=2Eprod=2Eoutlook=2Ecom=3E?= References: =?utf-8?q?=3CBN8PR17MB27372595A957D7912CEE184FBF6F9=40BN8PR17M?= =?utf-8?q?B2737=2Enamprd17=2Eprod=2Eoutlook=2Ecom=3E?= =?utf-8?q?=3CCAM9Ku=3DFZ5RGMvw3VzNrZz+DA78zyq2Am8fz9JNLmjUQ9ZEXpDQ=40ma?= =?utf-8?q?il=2Egmail=2Ecom=3E_=3CBN8PR17MB27371C71A65834531DF028BBBF6F9=40B?= =?utf-8?q?N8PR17MB2737=2Enamprd17=2Eprod=2Eoutlook=2Ecom=3E?= <CAM9Ku=E1wmpj=AMRhsh-Sk1RHqmK_x-J5ey8szVehefYQvn13w@mail.gmail.com> Subject: I have seen the =?utf-8?q?fn=C3=B6rds?= From: sender@example.org To: recipient@example.net CC: cc.recipient@example.com BCC: bcc.recipient@example.int Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: base64 MIME-Version: 1.0 TEFOR1VBR0UgKE5BVElWRSBOQU1FKQlIRUxMTwotLS0tLS0tLS0tLS0tLS0tLS0tLS0tCS0tLS0t CkphcGFuZXNlICjml6XmnKzoqp4pCeOBk+OCk+OBq+OBoeOBryAvIO+9uu++ne++hu++ge++igpD aGluZXNlICjkuK3mlocs5pmu6YCa6K+dLOaxieivrSkJ5L2g5aW9CkNhbnRvbmVzZSAo57K16Kqe LOW7o+adseipsSkJ5pep5pmoLCDkvaDlpb0KS29yZWFuICjtlZzquIApCeyViOuFle2VmOyEuOya lCAvIOyViOuFle2VmOyLreuLiOq5jAo= ------------------------------------------------------------------------ ** Original, from message_from_bytes In-Reply-To: =?utf-8?q?=3CBN8PR17MB27372595A957D7912CEE184FBF6F9=40BN8PR17MB?= =?utf-8?q?2737=2Enamprd17=2Eprod=2Eoutlook=2Ecom=3E?= References: =?utf-8?q?=3CBN8PR17MB27372595A957D7912CEE184FBF6F9=40BN8PR17M?= =?utf-8?q?B2737=2Enamprd17=2Eprod=2Eoutlook=2Ecom=3E?= =?utf-8?q?=3CCAM9Ku=3DFZ5RGMvw3VzNrZz+DA78zyq2Am8fz9JNLmjUQ9ZEXpDQ=40ma?= =?utf-8?q?il=2Egmail=2Ecom=3E_=3CBN8PR17MB27371C71A65834531DF028BBBF6F9=40B?= =?utf-8?q?N8PR17MB2737=2Enamprd17=2Eprod=2Eoutlook=2Ecom=3E?= <CAM9Ku=E1wmpj=AMRhsh-Sk1RHqmK_x-J5ey8szVehefYQvn13w@mail.gmail.com> Subject: I have seen the =?utf-8?q?fn=C3=B6rds?= From: sender@example.org To: recipient@example.net CC: cc.recipient@example.com BCC: bcc.recipient@example.int Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: base64 MIME-Version: 1.0 TEFOR1VBR0UgKE5BVElWRSBOQU1FKQlIRUxMTwotLS0tLS0tLS0tLS0tLS0tLS0tLS0tCS0tLS0t CkphcGFuZXNlICjml6XmnKzoqp4pCeOBk+OCk+OBq+OBoeOBryAvIO+9uu++ne++hu++ge++igpD aGluZXNlICjkuK3mlocs5pmu6YCa6K+dLOaxieivrSkJ5L2g5aW9CkNhbnRvbmVzZSAo57K16Kqe LOW7o+adseipsSkJ5pep5pmoLCDkvaDlpb0KS29yZWFuICjtlZzquIApCeyViOuFle2VmOyEuOya lCAvIOyViOuFle2VmOyLreuLiOq5jAo= ------------------------------------------------------------------------ ** Fixed, with custom policy In-Reply-To: <BN8PR17MB27372595A957D7912CEE184FBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com> References: <BN8PR17MB27372595A957D7912CEE184FBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com> <CAM9Ku=FZ5RGMvw3VzNrZz+DA78zyq2Am8fz9JNLmjUQ9ZEXpDQ@mail.gmail.com> <BN8PR17MB27371C71A65834531DF028BBBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com> <CAM9Ku=E1wmpj=AMRhsh-Sk1RHqmK_x-J5ey8szVehefYQvn13w@mail.gmail.com> Subject: I have seen the =?utf-8?q?fn=C3=B6rds?= From: sender@example.org To: recipient@example.net CC: cc.recipient@example.com BCC: bcc.recipient@example.int Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: base64 MIME-Version: 1.0 TEFOR1VBR0UgKE5BVElWRSBOQU1FKQlIRUxMTwotLS0tLS0tLS0tLS0tLS0tLS0tLS0tCS0tLS0t CkphcGFuZXNlICjml6XmnKzoqp4pCeOBk+OCk+OBq+OBoeOBryAvIO+9uu++ne++hu++ge++igpD aGluZXNlICjkuK3mlocs5pmu6YCa6K+dLOaxieivrSkJ5L2g5aW9CkNhbnRvbmVzZSAo57K16Kqe LOW7o+adseipsSkJ5pep5pmoLCDkvaDlpb0KS29yZWFuICjtlZzquIApCeyViOuFle2VmOyEuOya lCAvIOyViOuFle2VmOyLreuLiOq5jAo= ------------------------------------------------------------------------ ** Fixed, with custom policy; from message_from_bytes In-Reply-To: <BN8PR17MB27372595A957D7912CEE184FBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com> References: <BN8PR17MB27372595A957D7912CEE184FBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com> <CAM9Ku=FZ5RGMvw3VzNrZz+DA78zyq2Am8fz9JNLmjUQ9ZEXpDQ@mail.gmail.com> <BN8PR17MB27371C71A65834531DF028BBBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com> <CAM9Ku=E1wmpj=AMRhsh-Sk1RHqmK_x-J5ey8szVehefYQvn13w@mail.gmail.com> Subject: I have seen the =?utf-8?q?fn=C3=B6rds?= From: sender@example.org To: recipient@example.net CC: cc.recipient@example.com BCC: bcc.recipient@example.int Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: base64 MIME-Version: 1.0 TEFOR1VBR0UgKE5BVElWRSBOQU1FKQlIRUxMTwotLS0tLS0tLS0tLS0tLS0tLS0tLS0tCS0tLS0t CkphcGFuZXNlICjml6XmnKzoqp4pCeOBk+OCk+OBq+OBoeOBryAvIO+9uu++ne++hu++ge++igpD aGluZXNlICjkuK3mlocs5pmu6YCa6K+dLOaxieivrSkJ5L2g5aW9CkNhbnRvbmVzZSAo57K16Kqe LOW7o+adseipsSkJ5pep5pmoLCDkvaDlpb0KS29yZWFuICjtlZzquIApCeyViOuFle2VmOyEuOya lCAvIOyViOuFle2VmOyLreuLiOq5jAo=