####python version - Python 2.7.18
I am beginner and trying my hands on write some code in Python, so please forgive me for my ignorance. I am trying to write a code that will delete old mails from outlook. Below is the error i get :
###############
Traceback (most recent call last):
File “test_mail.py”, line 20, in
ty, msg=imap.fetch(messages,’(RFC822)’)
File “/usr/lib/python2.7/imaplib.py”, line 459, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File “/usr/lib/python2.7/imaplib.py”, line 1091, in _simple_command
return self._command_complete(name, self._command(name, *args))
File “/usr/lib/python2.7/imaplib.py”, line 921, in _command_complete
raise self.error(’%s command error: %s %s’ % (name, typ, data))
imaplib.error: FETCH command error: BAD [‘The specified message set is invalid.’ ]
###############
print(‘Connecting to server {host}:{port} for user “{username}”’.format(host=host, port=port, username=username))
imap=imaplib.IMAP4(host, port)
print(imap.welcome)
imap.login(username,password)
imap.select(“MYFOLDER”)
#print(imap.list())
status,message=imap.search(‘None’,‘BEFORE “20-MAR-2021”’)
message=message[0].split(b’ ‘)
for messages in message:
ty, msg=imap.fetch(messages,’(RFC822)’)
for response in msg:
if isinstance(response, tuple):
msg = email.message_from_bytes(response[1])
# decode the email subject
subject = decode_header(msg[“Subject”])[0][0]
if isinstance(subject, bytes):
# if it’s a bytes type, decode to str
subject = subject.decode()
print(“Deleting”, subject)
mark the mail as deleted
imap.store(mail, “+FLAGS”, “\Deleted”)
imap.expunge()
imap.close()
imap.logout()
imap.logout()