I use offlineimap + not-much mail + astroid to handle my email and I wanted a way to make emails self-destruct after a certain amount of time.
Here’s the script I came up with to do that:
nmExpire.py
#!/usr/bin/python
import notmuch
import re, datetime
from dateutil.relativedelta import relativedelta
#from pprint import pprint
today = datetime.datetime.now().date()
db = notmuch.Database(mode=notmuch.Database.MODE.READ_WRITE)
# Find Emails with Ending Tags
query = db.create_query('tag:/^x[0-9]+[mwd]*$/')
for msg in query.search_messages():
tags = msg.get_tags()
for t in tags:
if re.match(r'^x[0-9]+[mwd]*$', t):
if re.match(r'^[0-9]{8}$',t[1:]):
endDate = datetime.datetime.strptime(t[1:], '%Y%m%d').date()
#print(t, endDate)
if endDate < today:
print("msg will now self-destruct")
msg.remove_tag(t)
msg.add_tag('trash')
elif re.match(r'^[0-9]+[mwd]+$', t[1:]): #days
# Change Relative Delays to Dates
#print("Changing delay to date")
delay = int(t[1:-1])
if t[-1] == 'w':
endDate = today + datetime.timedelta(weeks=delay)
elif t[-1] == 'm':
endDate = today + relativedelta(months=+delay)
else:
endDate = today + datetime.timedelta(days=delay)
endTag = "x" + endDate.strftime('%Y%m%d')
msg.remove_tag(t)
msg.add_tag(endTag)
Usage
For any email with an expiration date you would either tag the email with xYYYYMMDD
(if you know the exact expiration date), or xN[d,w,m]
(example: x14d
) to have the email expire in N days, weeks, or months.
Put the nmExpire.py script in a hook script or cron or something so that it gets run periodically through the day. When it finds a tag labeled something like xN[dwm], it changes it to an exact date (e.g. x20180704). When the script finds an email with an expiration date before the current date, it removes the expiration tag and adds the tag trash.
I have all my default searches set up to ignore email with the trash tag, and trashed emails are automatically deleted after a certain number of days.
Example Use
So let’s say some company emails me a coupon and I think it looks pretty good. I might tag it with L because it’s a low priority. Now it shows up in one of my default searches (the one for low-priority items).
But the coupon expires on July 5th, so I tag it as x20180705.
On July 6th, if I still haven’t used the (now expired) coupon, the email is automatically trashed and I no longer see it.
Inspiration
I got the idea for this from Taskwarrior, a command-line task management app. You can set an until date for a task so that it expires after a certain date and the program stops bugging you about it.