I have been trying to write a quick script to move emails from one folder to another in my email. However, I continue getting the error message:
imaplib.error: UID command error: BAD [b'Could not parse command']
from imap_tools import MailBox, Q
from dotenv import load_dotenv
import os
load_dotenv('.env')
def get_emails(folder, move_to, search):
# get list of email msg_uid from INBOX folder
with MailBox(imap).login(email, password, initial_folder=folder) as mailbox:
print('Login Success')
if mailbox.folder.exists(folder) and mailbox.folder.exists(move_to):
print('Both folders exist')
msg_uid = [msg.uid for msg in mailbox.fetch(Q(text=search))]
print(f'Message count: {len(msg_uid)}')
for msg in mailbox.fetch():
print(f'UID: {msg.uid}', f'Subject: {msg.subject}')
res = mailbox.move(msg.uid, move_to)
else:
if not mailbox.folder.exists(folder):
print('From folder missing')
if not mailbox.folder.exists(move_to):
print('To folder missing')
if __name__ == '__main__':
email = os.getenv('my_gmail_email')
password = os.getenv('my_gmail_password')
imap = os.getenv('my_gmail_imap_server')
folder = 'INBOX/Aqua Living/QuickForm (NOW)'
move_to = 'INBOX/Aqua Living'
search_text = 'NOW'
get_emails(folder, move_to, search_text)