Saturday, January 14, 2012

My Gmail setup with mutt, offlineimap, mailcheck, wmbiff, some shell scripts and notify-send


I often say that I prefer interacting with most people over email than face to face, especially when it involves discussions about physics because I have more time to think. So I consider reading and writing emails as efficiently as possible an essential task, and one that I also must have fun with.

For many years I used Thunderbird to manage my emails. I would keep it open and maximized all the time in its own Window Maker workspace in order to receive pop up notifications as new emails arrived. But its creeping featurism  conflicted with my minimalist views of computer software. So I decided to try mutt on December 2007.



I like it and I never looked back. However, for a long time I would still keep thunderbird open just to get the new email notifications even though I would read them using mutt. I had to do something about it.

I used a Window Maker dockapp called wmckgmail for some time, but it was restricted to gmail and I wanted notifications for my other accounts too. So I needed to find other tools and tweak them to my needs.

In the end the solution I found is in lines with the Unix philosophy:
Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.

My email setup
  1. Sync emails from various servers with my local Mail folder with offlineimap
  2. Display the number of new emails in each mailbox in wmbiff
  3. Pop up notifications using notify-send, mailcheck and shell scripts
The above tasks are managed by wmbiff, a Window Maker dockapp which apart from displaying the number of new emails on each configured mailbox, can be used to periodically invoke a 'fetchcmd' command to fetch emails (task 1).

Furthermore, wmbiff periodically checks (every checkinterval seconds) whether the watched mailboxes received new emails and calls the 'globalnotify' command in case they did. That takes care of task 3 above.

As you can see in my $HOME/.wmbiffrc file below, I configured the 'fetchcmd' command to be the shell script update-emails.sh, which contains the line:

offlineimap -o -a DAMTP,gmail

and the 'globalnotify' command to be the script notify-emails-wmbiff.sh.

The notify-emails-wmbiff script is the following:

#!/bin/bash

new_mail=0
maildir=" "
sum_mail=0

mailcheck | grep new 2>&1 >/dev/null
if [ $? -eq 0 ];
then
maildir=`mailcheck | grep new | awk '{ print $NF }' \
| sed 's/\// /g' | awk '{ print $NF }' \
| awk '{ printf "%s ", $0 }' | sed 's/ /, /g' \
| sed 's/, $//'`

new_mail=`mailcheck | grep new | awk '{ print $3 }'`

for N in `echo $new_mail`
do
sum_mail=`expr $sum_mail + $N`
done

if [ $sum_mail -eq 1 ] ;
then
notify-send -c email.arrived -u normal -i mail_new \
"You have $sum_mail new mail:" "$maildir"
else
notify-send -c email.arrived -u normal -i mail_new \
"You have $sum_mail new mails:" "$maildir"
fi
fi

The script above uses mailcheck to count the new messages in each mailbox. It's a very simple program whose output appears in the snapshot below:



Parsing that output explains all the awk's and sed's in the script (I modified this script from somewhere in the internet).

After counting how many new emails I have, the script uses the program 'notify-send' to create a pop-up notification window on the top right:






Notice how the "Gmail" line in the wmbiff dockapp in the snapshot above has a red entry displaying "02". It means there are 2 new emails in my gmail inbox. Clicking that line opens up the mutt client inside the Gmail inbox -- to check exactly which command is run, see the wmbiffrc configuration file.

So that's how I traded thunderbird by mutt. It's much nicer this way!

PS:  My ~/.wmbiffrc contains (among other things):

# Seconds between checking mailboxes
interval=30

# Interval between mail auto-fetching; use 0 to disable
# right-clicking the dockapp makes it execute fetchcmd.0 right away
# use -1 for auto-fetching on new mail arrival
fetchinterval.0=600

# Command for fetching mail.
fetchcmd.0=/home/mafra/bin/update-emails.sh

label.1=Gmail
path.1=maildir:/home/mafra/Mail/Gmail/INBOX
action.1=xterm -geometry 140x48+50+0 -bg black -fg white +sb +sm -fn 10x20 -sl 4000 -cr yellow -name Gmail -e mutt -f /home/mafra/Mail/Gmail/INBOX

# Command which will be executed for new mail in any watched mailbox
globalnotify=/home/mafra/bin/notify-emails-wmbiff.sh

My ~/.offlineimaprc contains:

[general]
accounts = DAMTP, gmail
fsync = false
ui = Curses.Blinkenlights, TTY.TTYUI,
Noninteractive.Basic, Noninteractive.Quiet
maxsyncaccounts = 3
socktimeout = 60

[Account gmail]
localrepository = Local-gmail
remoterepository = Remote-gmail
autorefresh = 4
quick = 10

[Repository Local-gmail]
type = Maildir
localfolders = ~/Mail/Gmail

[Repository Remote-gmail]
type = Gmail
remoteuser = myemail
remotepass = mypassword
maxconnections = 3
realdelete = yes
folderfilter = lambda foldername: foldername not in ['[Gmail]/Spam','[Gmail]/Trash','[Gmail]/All Mail']

3 comments:

kix said...

In ~/.wmbiffrc

the values should be ".0" always (not .1)

kix said...

Ah! Nice entry. Thanks a lot, all is fine now.

I don't know why I don't have the notifications :-(. notify-send "Hello" don't do nothing.

Mafra said...

notify-send indeed does not work. One workaround is to install notify-osd which uninstalls notification-daemon in the process. After that my setup generates notifications correctly.