imaplib - 将邮件移动到文件夹失败,没有错误日志

imaplib - Moving mails to a folder fails without an error log

我的 python 代码选择了一些邮件并将它们移动到另一个文件夹。

# A.py

    def __init__(self):
        account = conf.account
        password = conf.password
        server = conf.imap_server
        port = conf.imap_port
        self.mail = imaplib.IMAP4_SSL(host=server, port=port)
        try:
            self.mail.login(account, password)
        except imaplib.IMAP4.error as e:
            self.logger.error("Cannot login to STMP server: %s" % str(e))


    def move_handled_mails(self, mail_list, suffix=0):
        archiving_folder = ''.join([str(current_year), '-', str(current_month), '-', str(suffix)])
        self.mail.debug = 4
        typ, data = self.mail.select(archiving_folder)
        if typ != 'OK':
            if "NONEXISTENT" in data[0].decode():
                self.mail.create(archiving_folder)
                typ, data = self.mail.select(archiving_folder)
            if "Too many mail in folder" in data[0].decode():
                return self.move_handled_mails(mail_list, suffix+1)

        if typ != 'OK':
            exit()

        for num in mail_list:
            print(num)
            typ, data = self.mail.uid('COPY', num, archiving_folder)
            if typ == 'OK':
                typ, data = self.mail.uid('STORE', num, '+FLAGS', '(\Deleted)')
            if typ != 'OK':
                exit()
        print("Executing expunge")
        typ, data = self.mail.expunge()
        if typ != 'OK':
            exit()


    def run(self):
        res, nums = self.mail.select()  # default=INBOX
        if res != 'OK':
            self.logger.error(nums)
            exit()

        typ, msgnums = self.mail.uid('search', None, 'ALL')
        mail_list = msgnums[0].split()
        self.move_handled_mails(mail_list)

move_handled_mails方法中,创建文件夹效果很好,但移动邮件不起作用。

此外,当我打印所有typdata时,它们分别打印为OKNone

运行代码后,没有错误发生,但也没有移动邮件。

可能是什么问题,我应该如何调试这个问题?


添加self.mail.debug=4后的调试日志:

Mail list: [b'7144291']
  03:12.87 > b'GBDI2 SELECT 2021-11-0'
  03:13.04 < b'* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)'
  03:13.04 < b'* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.'
  03:13.04 < b'* 13534 EXISTS'
  03:13.04 < b'* 0 RECENT'
  03:13.04 < b'* OK [UNSEEN 1] First unseen.'
  03:13.04 < b'* OK [UIDVALIDITY 119] UIDs valid'
  03:13.04 < b'* OK [UIDNEXT 7129395] Predicted next UID'
  03:13.04 < b'* OK [NOMODSEQ] No permanent modsequences'
  03:13.04 < b'GBDI2 OK [READ-WRITE] Select completed.'
b'7144291'
  03:13.04 > b'GBDI3 UID COPY 7144291 2021-11-0'
  03:13.16 < b'GBDI3 OK No messages copied.'
  03:13.16 > b'GBDI4 UID STORE 7144291 +FLAGS (\Deleted)'
  03:13.30 < b'GBDI4 OK Store completed.'
Executing expunge
  03:13.30 > b'GBDI5 EXPUNGE'
  03:13.30 < b'GBDI5 OK Expunge completed.'

不要 select 您要移动到的文件夹。 UIDs 仅在其源文件夹中有意义。您正在切换到目标文件夹,然后尝试从中移出一条消息,但该消息不存在。

删除这一行: typ, data = self.mail.select(archiving_folder)

或者,至少,如果您需要它来检查目标文件夹是否存在,则必须切换回源文件夹:self.mail.select('INBOX') 或类似文件夹。