Python script that deletes all emails with specified criteria.
- A specific sender email sent after a certain date
For this script to work, you have to :
- Make sure imap is enabled in your account settings. For google, the provider I'll be using, here's how to enable imap if it isn't.
- Allow less secure apps such as this one to have access to your mail account. To do so, generate a specific password for this app. when asked for the name of the app, copy paste: automatic_mail_deletion
- To test if this works on gmail, write the following on the search bar:
in:{folder you chose to delete from} from:{email of the sender} after:{first date to start deleting from}
example:
in:inbox from:[email protected] after:01/04/2022
- launch the script : python automate_mail_deletion.py
- Refresh your providers page
- If you want to change the folder you're selecting:
imap.select('"folder of your choice"')
- You can list all the folders using
imap.list()
before the select - If you want to change the criteria, change the second parameter of this instruction:
imap.search(None, "FROM '[email protected]' SINCE '02-Jan-2022'")
- You can look up the criteria on the imap library manual
- You can look up the gmail search operators here.
Let's do this !