Delete old mails from outlook

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

This is the line that’s failing (as per the error message):

ty, msg=imap.fetch(messages,'(RFC822)')

This is the reason it’s failing (as per the error message):

imaplib.error: FETCH command error: BAD ['The specified message set is
invalid.'

Note that this line:

status,message=imap.search('None','BEFORE "20-MAR-2021"')

which I rewrote for my own server as:

status, message = imap.search(None, "BEFORE", "20-MAR-2021")

is running this IMAP SEARCH command:

https://tools.ietf.org/html/rfc3501#section-6.4.4

via this Python call:

https://docs.python.org/3/library/imaplib.html#imaplib.IMAP4.search

which returns a:

listing of message sequence numbers corresponding to those messages that
match the searching criteria.
Source: RFC 3501 - INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1

so, from this line:

status,message=imap.search('None','BEFORE "20-MAR-2021"')

you would typically get a status of OK, and a listing of message sequence
numbers like ['1 2'] (for 2 messages matching the criteria).

You split this string to iterate over the message sequence numbers but
something goes wrong as you prepare your image.fetch() call.

Print out for debugging purposes which values you are using for that call, and
we’ll see why the FETCH is not liking them and protesting as so:

imaplib.error: FETCH command error: BAD ['The specified message set is
invalid.'

Thanks Marco for looking into my request. I will check the suggestion provided by you.