Open barskykd opened 1 year ago
I have the same bug but for the policy.default.
Is there a workaround? It is quite a blocker.
For this particular bug there is workaround. Convert Header to string before assignment
msg['Subject'] = str(h)
Oh, simple enough, thank you!
I've found this bug while fighting with problem described here: https://github.com/python/cpython/issues/44510
When I create an email with Subject
as str
, spaces that are located near email body line breaks are lost during decode/encode. For example:
msg['Subject'] = 'Приглашение в закрытую программу Перенос удалится'
gets encoded as:
Subject: =?utf-8?b?0J/RgNC40LPQu9Cw0YjQtdC90LjQtSDQsiDQt9Cw0LrRgNGL0YLRg9GO?=
=?utf-8?b?0L/RgNC+0LPRgNCw0LzQvNGDINCf0LXRgNC10L3QvtGBINGD0LTQsNC70LjRgtGB?=
=?utf-8?q?=D1=8F?=
and later is decoded by email client in this way:
Subject: Приглашение в закрытуюпрограмму Перенос удалится
There are 2 spaces in the beginning of line 2 of MIME representation, but on decode the "significant" space is lost.
If it's impossible now to declare header in this way (what was recommended in the bug mentioned above):
msg['Subject'] = Header('Приглашение в закрытую программу Перенос удалится', 'utf-8')
Is there a workaround for such cases?
I figured out that it's possible to use longer "raw" lines via email policy (max_line_length=998
, for example), but I'm not sure this is the most correct way to overcome this problem.
UPD: No, setting high max_line_length
does not affect the Subject header.
UPD2: Looks like my problem can't be solved with Header
either, I found the exact same bug here: https://github.com/python/cpython/issues/92081
Bug report
Code snippet is taken directly from documentation https://docs.python.org/3/library/email.header.html The only change - policy changed to email.policy.SMTP. Documentation (https://docs.python.org/3/library/email.policy.html#email.policy.SMTP) states that the only difference between email.policy.default and email.policy.SMTP - linesep used.
Output is:
Your environment
Reproduced on windows on