This question has already been answered on this post.
While it is possible to use
imaplib
directly I would recommend using a more user friendly library likeimap_tools
:with MailBox('imap.mail.com').login('[email protected]', 'pwd', initial_folder='INBOX') as mailbox: # MOVE all messages from current folder to INBOX/folder2 mailbox.move(mailbox.uids(), 'INBOX/folder2')
For the specific case of Google Mail I would recommend using their Python API . For example I wrote a small program to filter emails using Python and the Google API, you can find the code on Github .
upvote them, not me