Created
July 26, 2020 13:13
-
-
Save coding-lemur/4af09054a4e1c18ba610b15b1c055aee to your computer and use it in GitHub Desktop.
I redirect an copy of my mails to my Gmail account. But the original message always remains in my provider's IMAP mailbox. To save disk space this script helps me to delete old emails automatically. This script was inspired by https://gist.github.com/Rathgore/2597705
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from imapclient import IMAPClient, DELETED | |
| import datetime | |
| SERVER = 'imap.yourdomain.com' | |
| USERNAME = 'mail' | |
| PASSWORD = '1234567890' | |
| FOLDER = 'INBOX' | |
| MAX_DAYS = 90 # emails older than was deleted | |
| today = datetime.date.today() | |
| cutoff_date = today - datetime.timedelta(days=MAX_DAYS) | |
| before_date = cutoff_date.strftime('%d-%b-%Y') | |
| search_args = '(BEFORE "%s")' % before_date | |
| with IMAPClient(SERVER, use_uid=True) as server: | |
| server.login(USERNAME, PASSWORD) | |
| server.select_folder(FOLDER) | |
| messages = server.search(search_args) | |
| # mark messages as deleted | |
| for message in messages: | |
| server.add_flags(message, [DELETED]) | |
| # remove emails marked with the deleted flag | |
| server.expunge() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| imapclient |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment