I am working on running a script to mark and delete old emails that are just clogging up an old inbox for practice. When I run the script it starts marking and printing to the console as expected but after a few hundred emails it raises an error. Code is as follows:
# Import necessary modules.
import imaplib
import getpass
import time
# Gets users input.
username = input('Please input your Username: ')
# Gets user's password - For most services this will need to be done through app passwords, which need to be enabled.
password = getpass.getpass()
# Lets the user decide which folder to select emails from.
# For my use case it will be 'Inbox' first
email_folder = (input('Please select the folder you wish to have emails deleted from: ')).upper()
# Allows the user to choose which email provider they use
email_host = input('Please input your email provider: ').strip()
# Makes a call to the email service based on users input.
email_provider_imap = f'imap.{email_host.strip()}'
# A function that will mark and delete emails, Will loop 25 times.
# I did it this way because Imaplib only allows access to 1000 emails at a time per login.
def delete_these(user, creds, email_provider):
x = 0
while x < 26:
# Creates the imap4 class with SSL
imap = imaplib.IMAP4_SSL(email_provider)
# Logs the user in
imap.login(user, creds)
print('logged in!')
# From user input will select the folder containing the Emails to be deleted.
imap.select(email_folder)
# Gets emails from the inbox and stores them as user id's, Identifies emails from before Janurary 1st 2023
_, emails_to_delete = imap.search(None, 'BEFORE "01-JAN-2023"')
# Declares emails_to_delete as a list that can be iterated over by splitting Email id's
emails_to_delete = emails_to_delete[0].split()
# This for loop iterates through and marks the emails for deletion
for mail in emails_to_delete:
imap.store(mail, '+FLAGS', '\\Deleted')
print('Deleting emails marked for deletion: ', mail)
print('Loop complete!')
# Removes the Emails
imap.expunge()
# Closes the email
imap.close()
# Logs the user out to avoid raising an error from trying to use imap.search() multiple times.
imap.logout()
print('Logged out!')
# Sleeps the function for a 10 seconds
time.sleep(10)
# Increases x by 1 so that there is no accidental infinite loop
x += 1
# calls the function for execution
delete_these(username, password, email_provider_imap)
After about 600 emails are marked for deletion this error occurs.
Deleting emails marked for deletion: b'9379'
Traceback (most recent call last):
File "c:\Users\Marcin Malek\Desktop\handle_emails\del_emails.py", line 49, in <module>
delete_these(username, password, email_provider_imap)
File "c:\Users\Marcin Malek\Desktop\handle_emails\del_emails.py", line 39, in delete_these
imap.store(mail, '+FLAGS', '\\Deleted')
File "C:\Users\Marcin Malek\AppData\Local\Programs\Python\Python311\Lib\imaplib.py", line 851, in store
typ, dat = self._simple_command('STORE', message_set, command, flags)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Marcin Malek\AppData\Local\Programs\Python\Python311\Lib\imaplib.py", line 1230, in _simple_command
return self._command_complete(name, self._command(name, *args))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Marcin Malek\AppData\Local\Programs\Python\Python311\Lib\imaplib.py", line 1055, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.IMAP4.error: STORE command error: BAD [b'[CLIENTBUG] STORE Bad sequence in the command']
I tried to make a call to an email service like Google or Yahoo mail via the imaplib module. I passed in credentials into the delete_these() function and started automating the selection and deletion of emails process. I expected it to run through the code and at the end of each loop print out messages. Once the loop finishes running I anticipated the loop to exit and the program to close.