Python IMAPClient/imaplib search unicode issue

﹥>﹥吖頭↗ 提交于 2019-12-12 04:35:49

问题


I'm using the IMAPClient library, but I'm getting UnicodeEncodeError when doing a search. Below is a snippet and the stack trace:

imap_client = imapclient.IMAPClient('imap.gmail.com', use_uid=True, ssl=True)
imap_client.oauth2_login('john@example.com', 'xxx')
subject = u'Test \u0153\u2211\u00b4\u00e5\u00df\u2202'
from_email = u'john@example.com'
to_emails = [u'foo@example.com']
cc_emails = []
approx_date_sent = '05-Aug-2013'

imap_client.select_folder(r'\Sent')
search_criteria = [
   u'FROM %s' % from_email,
   u'SUBJECT %s'.encode('utf-8') % subject,
   u'TO %s' % ';'.join(to_emails) or '',
   u'CC %s' % ';'.join(cc_emails) or '',
   u'SENTON %s' % approx_date_sent
]
msg_ids = imap_client.search(search_criteria, charset='utf-8')

'ascii' codec can't encode characters in position 77-82: ordinal not in range(128)
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~app/dev.369284735686497536/imapapi.py", line 269, in post
    to_emails=to_emails, cc_emails=cc_emails, approx_date_sent=approx_date_sent
  File "/base/data/home/apps/s~app/dev.369284735686497536/utils/imap.py", line 123, in search_message
    msg_ids = imap_client.search(search_criteria, charset='utf-8')
  File "/base/data/home/apps/s~app/dev.369284735686497536/imapclient/imapclient.py", line 569, in search
    typ, data = self._imap.search(charset, *criteria)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 625, in search
    typ, dat = self._simple_command(name, 'CHARSET', charset, *criteria)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 857, in _command
    self.send('%s%s' % (data, CRLF))
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/imaplib.py", line 1178, in send
    sent = self.sslobj.write(data)
  File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/ssl.py", line 232, in write
    return self._sslobj.write(data)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 77-82: ordinal not in range(128)

It seems the issue happens in ssl.py? And ssl=True is needed for oauth2_login.


回答1:


Try this out

Python IMAP search using a subject encoded with iso-8859-1

It covers utf-8 as well as iso-8859-1



来源:https://stackoverflow.com/questions/18054337/python-imapclient-imaplib-search-unicode-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!