How can I use mutt with local storage, IMAP and instant pushing of new email?

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.


The only way to 'push' mail immediately to your client is for the server to do it as soon as the mail is delivered. This will inevitably require software to be installed and run on the mail server, so every potential solution is going to have the same problem as mswatch.

Frequent polling is probably your best option.

In my experience, running offlineimap or similar from cron every few minutes works well enough.

Some imap-fetching programs support the imap IDLE command. IDLE is not exactly 'push' (since the mail is still pulled from the server by the client) but generally results in almost instant updates to your local mailbox.

offlineimap supports it but the manual says "IDLE support is incomplete and experimental. Bugs may be encountered."

fetchmail supports IDLE since version 5.0

Tags:

Email

Mutt