vladimarius / imapy

Imapy: Imap for Humans
230 stars 22 forks source link

email.header expects str not unicode #11

Closed vovkkk closed 5 years ago

vovkkk commented 8 years ago
  File "D:\dev\Python27\lib\site-packages\imapy\imap.py", line 261, in email
    emails = self.emails(sequence_number, sequence_number)
  File "D:\dev\Python27\lib\site-packages\imapy\imap.py", line 34, in wrapper
    return func(*args, **kwargs)
  File "D:\dev\Python27\lib\site-packages\imapy\imap.py", line 279, in emails
    return self._get_emails_by_sequence(args[0], args[1])
  File "D:\dev\Python27\lib\site-packages\imapy\imap.py", line 345, in _get_emails_by_sequence
    return self._fetch_emails_info(uids)
  File "D:\dev\Python27\lib\site-packages\imapy\imap.py", line 34, in wrapper
    return func(*args, **kwargs)
  File "D:\dev\Python27\lib\site-packages\imapy\imap.py", line 385, in _fetch_emails_info
    email_obj=email_obj, imap_obj=self)
  File "D:\dev\Python27\lib\site-packages\imapy\email_message.py", line 43, in __init__
    self.parse()
  File "D:\dev\Python27\lib\site-packages\imapy\email_message.py", line 152, in parse
    attachment_fname = decode_header(part.get_filename())
  File "D:\dev\Python27\lib\email\header.py", line 73, in decode_header
    header = str(header)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 47: ordinal not in range(128)
ankydanky commented 7 years ago

hey man just seen your patch. i'd propose the following one in order to only encode to utf-8 if it is needed :)

try:
    attachment_fname = decode_header(part.get_filename())
except UnicodeEncodeError:
    attachment_fname = decode_header(part.get_filename().encode("utf-8"))