How can I move an email from a directory to another using python

Viewed 38

I am facing a problem that I can't solve despite many searches.

It is the following: the goal is to change the directory of an email (for example, an email is in the spam and we want to move it to the inbox).

For the handling of the mailbox, I use the imaplib library (the IMAP4_SSL module in particular). I'm new to using this library and maybe that's why I can't find a solution. I specify that I want to explicitly move the message and not copy it to the desired location and then delete it.

I tried to do this but without success:

ok, uids = self.connexion.search(None, "ALL HEADER From 'test_bot'")
mail_ids = uids[0].decode().split()
resp_code, response = self.connexion.uid("MOVE", mail_ids[0], "[Gmail]/Spam")

The values of resp_code and response are respectively OK, [None]

If you can help me, I would be grateful, any proposal is good to take even if you have to use another library.

Have a nice day.

1 Answers
from imap_tools import MailBox

with MailBox('imap.mail.com').login('test@mail.com', 'pwd', 'INBOX') as mailbox:
    # MOVE all messages from current folder to INBOX/folder2
    mailbox.move(mailbox.uids(), 'INBOX/folder2')

https://github.com/ikvk/imap_tools

I am lib author

Related