如何使用python在imaplib中按时间对电子邮件进行排序

时间:2019-01-09 03:00:54

标签: python imaplib

我想按到达时间对电子邮件进行排序。

代码如下:

data = get_credential('info.txt')
imap_conn = create_connection(data, 'outlook.office365.com')
imap_conn.select('INBOX', readonly=False)
result, messages = imap_conn.sort('ARRIVAL', 'UTF-8', 'FROM peter@gmail.com SINCE "'+(datetime.date.today()-datetime.timedelta(4)).strftime('%d-%b-%Y')+'" 

它返回以下错误:

File "C:\Program Files\Python37\lib\imaplib.py", line 794, in sort
    typ, dat = self._simple_command(name, sort_criteria, charset, *search_criteria)
  File "C:\Program Files\Python37\lib\imaplib.py", line 1196, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Program Files\Python37\lib\imaplib.py", line 1027, in _command_complete
    raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.IMAP4.error: SORT command error: BAD [b'Command Error. 12']

如何解决问题?

输入字符集UTF-8是否正确?如何从messages获取它?

1 个答案:

答案 0 :(得分:2)

Office365不支持SORT扩展名。

* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS MOVE ID UNSELECT CLIENTACCESSRULES CLIENTNETWORKPRESENCELOCATION BACKENDAUTHENTICATE CHILDREN IDLE NAMESPACE LITERAL+

在使用任何扩展名之前,您应检查CAPABILITY