Home > Back-end >  imaplib - Moving mails to a folder fails without an error log
imaplib - Moving mails to a folder fails without an error log

Time:11-26

My python code selects some mails and move them to another folder.

# 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)

At the move_handled_mails method, creating a folder works well, but moving mails doesn't work.

Moreover, when I print all the typs and datas, they are printed as OK and None repectively.

After running the code, no error occurs, but no mail is moved either.

What could be a problem, and how should I debug this problem?


Debug log after adding 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.'

CodePudding user response:

Don't select the folder you're moving to. UIDs are only meaningful in their source folder. You're switching to the destination folder, then trying to move a message out of it, and that message doesn't exist.

Remove this line: typ, data = self.mail.select(archiving_folder)

Or, at least, if you need it to check that the destination folder exists, you must switch back to the source folder: self.mail.select('INBOX') or similar.

  • Related