I3/mutt/offlineimap with decent mail notifications

Today I’ll show you my last trick to get decent email notifications on my I3 environment, without using anything else than offlineimap and mutt. Ok, there’s something else, a couple of scripts but nothing scary.

Before going further, I’d like to ensure you’re using i3blocks. If not, then please configure it then come back and read on.

Until today I was using the classical offlineimap configuration which polled my IMAP server every several minutes directed from a crontab. However, that method has several drawbacks:
– lack of instant mail delivery
– offlineimap would occasionally get stuck upon wifi issues.

Investigating these problems, I found this: https://blog.mister-muffin.de/archive/2013/06/1/

That script was a very good starting point for what follows. So here is the setup:

  • the script ~/bin/mail-check.sh is launched upon I3 session start
    • it calls mail-idle.py which would either return a timeout or a string containing the name of the offlineimap account name that has mail, and the folder name
    • it handles the timeouts itself so if mail-idle.py or offlineimap gets stuck for some reason, they get killed
    • when new mail is found, it uses notify-send,  then calls offlineimap to retrieve the mail from the specified folder and it updates i3blocks
    • finally, it calls notmuch to get the new mail indexed

Put this script in ~/bin/mail-check.sh.
chmod +x ~/bin/mail-check.sh

Then save this script as ~/bin/mail-idle.py. Modify lines 25..27 to suit your configuration. Beware that you should use the same values as in your ~/.offlineimaprc file, if not the scripts will not correctly invoke offlineimap.
chmod +x ~/bin/mail-idle.py

Get the i3blocks blocklet command from here and save it to ~/.config/i3blocks/mail_status.sh. Then adjust it on line 2 to get it inspect your local mailboxes. Also, adjust line 6 if the directory containing your mails is not ~/Mail.
chmod +x ~/.config/i3blocks/mail_status.sh

Activate the blocklet into ~/.config/i3blocks/config:

command= ~/.config/i3blocks/mail_status.sh

As you may have noticed, the blocklet will be updated by the other parts of the system using this command:
pkill -SIGRTMIN+12 i3blocks

Hang-on, we’re almost done! 😉

Get the tool activated upon I3 session start by adding this to ~/.config/i3/config:
exec --no-startup-id ~/bin/mail-check.sh &

Stop your current mail retrieval scripts and then restart your I3 session. You should now start having notifications upon new mail arrival. That is, when some of the IMAP folders supervised by mail-idle.py get mail, then you should see a dunst (or whatever you use) notification, and the i3blocket should become red, displaying the offlineimap account name containing new mails, with their count.

Last step is getting mutt update the i3blocket upon mail reading. Just add this macro somewhere in your ~/.muttrc:
macro index <F5> "<sync-mailbox>:set wait_key=no\n!pkill -SIGRTMIN+12 i3blocks^M"

Restart mutt and use F5 to sync your maildir and eventually clear the i3blocklet when no unread mail is left.


Leave a Reply

Your email address will not be published. Required fields are marked *