This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vitas1
Recipients barry, r.david.murray, vitas1
Date 2021-07-21.11:06:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1626865587.67.0.966185604714.issue44694@roundup.psfhosted.org>
In-reply-to
Content
Hello. Here is my code:
#Parse message from file and immediately flatten it
cur_policy = email.policy.SMTPUTF8
with open("/tmp/0.tmp", "rb") as orig_message_file:
    message_bytes = orig_message_file.read()
message_parser = BytesParser(policy=cur_policy)
msg = message_parser.parsebytes(message_bytes)
with open("/tmp/1.tmp", "wb") as new_message_file:
    message_gen = BytesGenerator(new_message_file, policy=cur_policy)
message_gen.flatten(msg)

On some messages script raises the following error:

Traceback (most recent call last):
  File "/misc/parsemail/./1.py", line 34, in <module>
    message_gen.flatten(msg)
  File "/usr/lib/python3.9/email/generator.py", line 116, in flatten
    self._write(msg)
  File "/usr/lib/python3.9/email/generator.py", line 199, in _write
    self._write_headers(msg)
  File "/usr/lib/python3.9/email/generator.py", line 422, in _write_headers
    self._fp.write(self.policy.fold_binary(h, v))
  File "/usr/lib/python3.9/email/policy.py", line 200, in fold_binary
    folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')
  File "/usr/lib/python3.9/email/policy.py", line 214, in _fold
    return self.header_factory(name, ''.join(lines)).fold(policy=self)
  File "/usr/lib/python3.9/email/headerregistry.py", line 257, in fold
    return header.fold(policy=policy)
  File "/usr/lib/python3.9/email/_header_value_parser.py", line 156, in fold
    return _refold_parse_tree(self, policy=policy)
  File "/usr/lib/python3.9/email/_header_value_parser.py", line 2825, in _refold_parse_tree
    last_ew = _fold_as_ew(tstr, lines, maxlen, last_ew,
  File "/usr/lib/python3.9/email/_header_value_parser.py", line 2913, in _fold_as_ew
    encoded_word = _ew.encode(to_encode_word, charset=encode_as)
  File "/usr/lib/python3.9/email/_encoded_words.py", line 222, in encode
    bstring = string.encode('ascii', 'surrogateescape')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

Policies 'default' and 'SMTP' are also affected. 

How to fix:

#For broken messages
message_gen = BytesGenerator(new_message_file, policy=cur_policy, maxheaderlen=0)

Well, but parsing and flattening the same *unmodified* message should be completed without using any additional parameters, isn't it?
Thanks.
History
Date User Action Args
2021-07-21 11:06:27vitas1setrecipients: + vitas1, barry, r.david.murray
2021-07-21 11:06:27vitas1setmessageid: <1626865587.67.0.966185604714.issue44694@roundup.psfhosted.org>
2021-07-21 11:06:27vitas1linkissue44694 messages
2021-07-21 11:06:27vitas1create