Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeEncodeError in email.generator.py #131655

Closed
Elvis33LE opened this issue Mar 24, 2025 · 2 comments
Closed

UnicodeEncodeError in email.generator.py #131655

Elvis33LE opened this issue Mar 24, 2025 · 2 comments
Labels
pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir topic-email type-bug An unexpected behavior, bug, or error

Comments

@Elvis33LE
Copy link

Elvis33LE commented Mar 24, 2025

Bug report

Bug description:

Running offlineimap to sync my german GMX mail account to my homeserver an issue occured:

`ERROR: Copying message 49813 [acc: GMX]

  UnicodeEncodeError: 'ascii' codec can't encode characters in position 34-37: ordinal not in range(128)

Traceback:

  File "/usr/share/offlineimap3/offlineimap/folder/Base.py", line 828, in copymessageto

    new_uid = dstfolder.savemessage(uid, message, flags, rtime)

              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/usr/share/offlineimap3/offlineimap/folder/Maildir.py", line 408, in savemessage

    tmpname = self.save_to_tmp_file(messagename, msg)

              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/usr/share/offlineimap3/offlineimap/folder/Maildir.py", line 359, in save_to_tmp_file

    fd.write(msg.as_bytes(policy=output_policy))

             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/usr/lib/python3.12/email/message.py", line 208, in as_bytes

    g.flatten(self, unixfrom=unixfrom)

  File "/usr/lib/python3.12/email/generator.py", line 117, in flatten

    self._write(msg)

  File "/usr/lib/python3.12/email/generator.py", line 182, in _write

    self._dispatch(msg)

  File "/usr/lib/python3.12/email/generator.py", line 219, in _dispatch

    meth(msg)

  File "/usr/lib/python3.12/email/generator.py", line 286, in _handle_multipart

    g.flatten(part, unixfrom=False, linesep=self._NL)

  File "/usr/lib/python3.12/email/generator.py", line 117, in flatten

    self._write(msg)

  File "/usr/lib/python3.12/email/generator.py", line 182, in _write

    self._dispatch(msg)

  File "/usr/lib/python3.12/email/generator.py", line 219, in _dispatch

    meth(msg)

  File "/usr/lib/python3.12/email/generator.py", line 286, in _handle_multipart

    g.flatten(part, unixfrom=False, linesep=self._NL)

  File "/usr/lib/python3.12/email/generator.py", line 117, in flatten

    self._write(msg)

  File "/usr/lib/python3.12/email/generator.py", line 182, in _write

    self._dispatch(msg)

  File "/usr/lib/python3.12/email/generator.py", line 219, in _dispatch

    meth(msg)

  File "/usr/lib/python3.12/email/generator.py", line 278, in _handle_multipart

    self.write(subparts)

  File "/usr/lib/python3.12/email/generator.py", line 420, in write

    self._fp.write(s.encode('ascii', 'surrogateescape'))

                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^`

I was able to fix this by changing line 420 in generator.py

try:
    self._fp.write(s.encode('ascii', 'surrogateescape'))
except:
    self._fp.write(s.encode('utf-8', 'replace'))

CPython versions tested on:

3.12

Operating systems tested on:

Linux

@Elvis33LE Elvis33LE added the type-bug An unexpected behavior, bug, or error label Mar 24, 2025
@picnixz
Copy link
Member

picnixz commented Mar 24, 2025

It's an issue with offlineimap3 I think. The generator is meant to use ASCII only IIRC.

@picnixz picnixz added stdlib Python modules in the Lib dir topic-email pending The issue will be closed if no feedback is provided labels Mar 24, 2025
@Elvis33LE
Copy link
Author

Okay, thanks for letting me know. Guess this will stay a local fix then :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir topic-email type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants