Skip to content

Instantly share code, notes, and snippets.

@wujiang
Created July 15, 2013 22:52
Show Gist options
  • Save wujiang/6004210 to your computer and use it in GitHub Desktop.
Save wujiang/6004210 to your computer and use it in GitHub Desktop.
Archive old Maildir type messages
#!/usr/bin/env python
# Archive old Maildir type messages
# Wu Jiang (wu@morediff.info)
import argparse
from datetime import timedelta
import mailbox
import time
parser = argparse.ArgumentParser(description="Archive Maildir messages.")
parser.add_argument("-d", "--days", default=14, type=int,
help="archive messages older than how many days")
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("path", help="path to maildir")
args = parser.parse_args()
threshold = timedelta(args.days).total_seconds()
maildir = mailbox.Maildir(args.path, factory=None, create=False)
# get the archive folder
try:
archive = maildir.get_folder("archive")
except mailbox.NoSuchMailboxError:
archive = maildir.add_folder("archive")
for key, message in maildir.iteritems():
if time.time() - message.get_date() >= threshold:
archive.add(message)
maildir.remove(key)
if args.verbose:
print("Moved message {} to the archive folder.".format(key))
@wujiang
Copy link
Author

wujiang commented Jul 16, 2013

For mutt, you can run this when you are on index:

T-01/07/13\n;s~/mail/.archive\ny

Explanation:

T-01/07/13: tag all messages received before July 01, 2013
;: prefix to apply following macros to tagged messages
s: save to
~/mail/.archive: wherever your archive folder is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment