How to use mutt with local storage, IMAP and instant pushing of new email

emailmutt

I'm thinking about switching to mutt for email. However, I have a few requirements.

  1. I'd like to be able to store the email offline.
  2. I'd like to have email pushed immediately to my local computer as opposed to periodic polling (e.g. using IMAP IDLE).

For offline storage, I could use imapoffline or isync. I understand that the latter is more stable. However, to have email pushed on demand, the only option I've found for isync is mswatch. Unfortunately, this requires a program to be installed on the remote email server, which is not possible. Is there a solution that will allow me to use mutt, with offline email storage and instant email delivery?

Best Answer

Unfortunately, the two possibilities suggested in the other answer were imperfect. offlineimap was fairly buggy at the best of times. For example, there is no way to automatically run a script after new mail arrives. fetchmail doesn't synchronise bidirectionally.

Instead, the solution that I ended up using was a combination of imapnotify and isync. I configured imapnotify to run a script when new mail is triggered (via IDLE).

This script runs mbsync "${channel}:INBOX" depending on which account has mail. Next it runs notmuch new. Finally, it records the number of unread emails to a file as below. The contents of this file is displayed on a panel of my desktop environment.

mail_count_file="/home/foo/.cache/new_mail_count"
new_count=$(find ~/.mail/*/Inbox/new -type f | wc -l)
if [[ $new_count > 0 ]]; then
  echo $new_count > "$mail_count_file"
else
  if [[ -f "$mail_count_file" ]]; then
    rm "$mail_count_file"
  fi
fi

Update

imapnotify (nodejs-imapnotify) disconnects regularly with no warnings/errors, and often misses new mail. python-imapnotify also works intermittently. However, goimapnotify works very well in my experience. It rarely drops out, and when it does (e.g. because of network disconnects and/or suspend cycles), it quickly restarts itself without fuss.