fork download
  1. import email.message
  2. from email import message_from_bytes
  3. from email.policy import default
  4.  
  5. reference_ids = [
  6. '<BN8PR17MB27372595A957D7912CEE184FBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  7. '<CAM9Ku=FZ5RGMvw3VzNrZz+DA78zyq2Am8fz9JNLmjUQ9ZEXpDQ@mail.gmail.com>',
  8. '<BN8PR17MB27371C71A65834531DF028BBBF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  9. # As a test, copied I successively shortened this one
  10. # and sure enough, once it gets short enough, it no longer gets encoded
  11. # '<BNPR17MB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  12. # '<BNR17MB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  13. # '<BN17MB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  14. # '<BN7MB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  15. # '<BNMB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  16. # '<BNB27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  17. # '<BN27377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  18. # '<BN7377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  19. # '<BN377F609B669D0E72638D69BF6F9@BN8PR17MB2737.namprd17.prod.outlook.com>',
  20. '<CAM9Ku=E1wmpj=AMRhsh-Sk1RHqmK_x-J5ey8szVehefYQvn13w@mail.gmail.com>']
  21. in_reply_to = reference_ids[0]
  22.  
  23. class Request:
  24. """fugly hack to let the code below work as in the original question"""
  25. def __init__(self):
  26. self.vars = self
  27. self.email = "sender@example.org"
  28. self.to = "recipient@example.net"
  29. self.cc = "cc.recipient@example.com"
  30. self.bcc = "bcc.recipient@example.int"
  31. self.subject = "I have seen the fnörds"
  32. self.message = """\
  33. LANGUAGE (NATIVE NAME) HELLO
  34. ---------------------- -----
  35. Japanese (日本語) こんにちは / コンニチハ
  36. Chinese (中文,普通话,汉语) 你好
  37. Cantonese (粵語,廣東話) 早晨, 你好
  38. Korean (한글) 안녕하세요 / 안녕하십니까
  39. """
  40.  
  41. request = Request()
  42. es = request
  43.  
  44. msg = email.message.EmailMessage()
  45. if reference_ids is not None: # no parentheses necessary, this is not C
  46. msg.add_header('In-Reply-To', in_reply_to)
  47. msg.add_header('References', (' ').join(reference_ids))
  48. msg['Subject'] = request.vars.subject
  49. msg['From'] = es.email
  50. msg['To'] = request.vars.to
  51. msg['CC'] = request.vars.cc
  52. msg['BCC'] = request.vars.bcc
  53. msg.set_content(request.vars.message)
  54.  
  55. print('** Original')
  56. print(msg.as_string())
  57.  
  58. print("-" * 72)
  59.  
  60. copy = message_from_bytes(msg.as_string().encode('us-ascii'))
  61. print('** Original, from message_from_bytes')
  62. print(copy.as_string())
  63.  
  64. print("-" * 72)
  65.  
  66. custom_policy = default.clone(max_line_length=100)
  67.  
  68. msg = email.message.EmailMessage(policy=custom_policy)
  69. if (reference_ids is not None):
  70. msg.add_header('In-Reply-To', in_reply_to)
  71. msg.add_header('References', (' ').join(reference_ids))
  72. msg['Subject'] = request.vars.subject
  73. msg['From'] = es.email
  74. msg['To'] = request.vars.to
  75. msg['CC'] = request.vars.cc
  76. msg['BCC'] = request.vars.bcc
  77. msg.set_content(request.vars.message)
  78.  
  79. print('** Fixed, with custom policy')
  80. print(msg.as_string())
  81.  
  82. print("-" * 72)
  83.  
  84. copy = message_from_bytes(msg.as_string().encode('us-ascii'), policy=custom_policy)
  85. print('** Fixed, with custom policy; from message_from_bytes')
  86. print(copy.as_string())
  87.  
Success #stdin #stdout 0.16s 18472KB
stdin
Standard input is empty
stdout
** 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=