Not long ago I noticed that my Gmail app allowed me to snooze an email and get the “new mail” notification pop up again later. I use IMAP for my main email addresses though and wanted to have that feature for them. My mail server uses dovecot as the IMAP server which doesn’t support snoozing at the moment, I guess it is something that would need to be added to the IMAP protocol to make sure all mail clients could use a feature like that properly.

It looks like the best way to implement it myself would be to create some special IMAP folders which correspond to different reminder times. For example a folder called Snooze with subfolders 0800 and 1300 inside it. These folders could be set as favorites in your email client to make it easier to move stuff to them.

Snooze folders

You could then move a mail into one of these folders using any email client and have a script running on a server which checks the folders every hour or so. If the script finds mail in a folder called 1300 and the time now is 13:00 then it will move that mail back to the inbox and mark it as unread.

If you wanted more control over the day and time then that could also be done by creating even more folders matching the requirements, but things would get messy then. So I am just going to stick with a time so I can choose to be reminded the next morning or in the evening.

So now to make a little script to do that. I guess Python would be easiest, there must be an IMAP library for it.

I decided to try creating a script that can be run periodically from cron, rather than something that would need to run in the background all the time.

# Check IMAP snooze folders for mail to move to inbox
# Folders are expected to be subfolders of 'Snooze' and
# be named the time they should be moved in HHMM format
# This needs to be run at exactly the same time which
# matches the folder name otherwise it will miss that
# folder

from imaplib import IMAP4_SSL
import time

SERVER = 'mail.domain.com'
PORT = 993
USER = 'username'
PASSWORD = 'password'
FOLDER = 'Snooze'

# open connection
server = IMAP4_SSL(host=SERVER, port=PORT)
# login
server.login(user=USER, password=PASSWORD)

# check the Snooze folder exists
if(server.list(pattern=FOLDER)[1] != [None]):
    print("Snooze folder found")
    # get the current time as HHMM
    time_now = time.strftime("%H%M")
    folder_name = FOLDER + "." + time_now

    if(server.list(pattern=folder_name)[1] != [None]):
        print("Found folder matching " + time_now)
        server.select(folder_name)
        response, messages = server.search(None, 'ALL')
        print(messages)
        # go through each message
        for message_id in messages[0].split():
            # mark it as unread
            server.store(message_id, '-FLAGS', '\\Seen')
            # copy it to inbox
            server.copy(message_id, 'INBOX')
            # and remove from the Snooze folder
            server.store(message_id, '+FLAGS', '\\Deleted \\Seen')
        # expunge after doing all the moving
        server.expunge()

server.logout()

To run it from cron you would just need to add a line to your crontab (with crontab -e) looking something like

00 08,13,18 * * * /usr/bin/python3 /home/user/dovecot-snooze.py

In this example the script would be run at 08:00, 13:00, and 18:00 every day. This means it would look for the folders Snooze/0800, Snooze/1300, and Snooze/1800 on your IMAP server.

I only tested it with dovecot as the IMAP server but it should work with any IMAP server.

Previous Post Next Post