You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
it would help if you described what you believe the problem is, @mdierksen (without forcing others to reverse engineer your example code to try to understand)
Traceback (most recent call last):
File "/home/dierksen/test4.py", line 16, in <module>
e.as_bytes()
^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/message.py", line 178, in as_bytes
g.flatten(self, unixfrom=unixfrom)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/generator.py", line 116, in flatten
self._write(msg)
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/generator.py", line 199, in _write
self._write_headers(msg)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/generator.py", line 422, in _write_headers
self._fp.write(self.policy.fold_binary(h, v))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/policy.py", line 200, in fold_binary
folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/policy.py", line 214, in _fold
return self.header_factory(name, ''.join(lines)).fold(policy=self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/headerregistry.py", line 253, in fold
return header.fold(policy=policy)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/_header_value_parser.py", line 156, in fold
return _refold_parse_tree(self, policy=policy)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/_header_value_parser.py", line 2825, in _refold_parse_tree
last_ew = _fold_as_ew(tstr, lines, maxlen, last_ew,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/_header_value_parser.py", line 2913, in _fold_as_ew
encoded_word = _ew.encode(to_encode_word, charset=encode_as)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/email/_encoded_words.py", line 222, in encode
bstring = string.encode('ascii', 'surrogateescape')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-10: ordinal not in range(128)
The problem is with the subject header. Extracting the subject as a string works however.
Manually decoding the subject header and replacing it in the email object fixes the problem. Extracting the subject as a string yields the same string as before.
The problem does not occur for encoded headers in a single line, but only for multiline headers that are split up in a certain way.
Activity
wbolster commentedon May 9, 2022
it would help if you described what you believe the problem is, @mdierksen (without forcing others to reverse engineer your example code to try to understand)
mdierksen commentedon May 10, 2022
The first e.as_bytes() causes this exception:
The problem is with the subject header. Extracting the subject as a string works however.
Manually decoding the subject header and replacing it in the email object fixes the problem. Extracting the subject as a string yields the same string as before.
The problem does not occur for encoded headers in a single line, but only for multiline headers that are split up in a certain way.