From 2b151b072f8cfd74c0d2431b46e99521f03cbab1 Mon Sep 17 00:00:00 2001 From: Scott Alfter Date: Mon, 19 Feb 2024 11:36:48 -0800 Subject: [PATCH] initial commit --- .gitignore | 1 + README.md | 19 +++++++++++++++++++ promo_cleanup.py | 15 +++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 promo_cleanup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e07f4a8 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +promo_cleanup +============= + +This script enumerates all subfolders under a folder named "Promotions" on +your IMAP server and deletes everything more than a week old. Use it +against mailers who aren't exactly sending spam but that you don't want +cluttering your main inbox by using something like procmail or sieve to sort +them into Promotions subfolders. This can be set to run as a daily cronjob +to keep things from getting out of control. + +Configure with the following environment variables, which should be +self-explanatory: + +```IMAP_SERVER``` +```IMAP_USER``` +```IMAP_PASSWD``` + +The script assumes you have SSL enabled on port 993 on your IMAP server. If +you don't, you're doing it wrong. diff --git a/promo_cleanup.py b/promo_cleanup.py new file mode 100755 index 0000000..f070a2e --- /dev/null +++ b/promo_cleanup.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +import imaplib +from datetime import datetime, timedelta +from os import environ + +with imaplib.IMAP4_SSL(environ["IMAP_SERVER"]) as M: + M.login(environ["IMAP_USER"], environ["IMAP_PASSWD"]) + for i in M.list(pattern="Promotions.*")[1]: + M.select(i.decode().split(' "." ')[1]) + typ, data=M.search(None, '(BEFORE "'+(datetime.now()-timedelta(days=7)).strftime("%d-%b-%Y")+'")') + for j in data[0].split(): + M.store(j, "+FLAGS", "\\Deleted") + M.expunge() +