Reading emails: mutt + mbsync + mairix + imapfilter

27 September 2020

During the last years (when I've started working), I've started to receive e-mails from different sources: students, co-workers, social-networks, security bulletins, spam, online required registrations (sigh...).

I hate reading promotional emails, because they are full of crappy html and requires time to be filtered.

Therefore I reviewed my e-mail Raspberry OpenBSD setup (but can be ported to every POSIX operating system, such as GNU/Linux) which, at the moment is composed of:

That's for my setup, let's see in detail the configurations:


At the link[6], you can find my .muttrc. As you can see I can select the inbox and the email to use using Function keys (F1 for the first account, etc, etc). Every account file in .mutt has the following format[7]. In these file I've specified the maildir configured in .mbsyncrc.

To create mails I use vim, started automatically in insert mode and with a colored column at 70 chars.

Another thing configured in the file is the auto-dump of html emails. In extreme cases you can't see the URL in them and you need to download the email and open it in the browser (double check it to see if it contains js or auto-loading images!!!), or just cat it to a terminal and copy and paste the URL.

I don't do it too much, just 1/2 times per year, for services which don't have a correct pure-text mail handling (somebody said conferences?).


.mbsyncrc is composed of fragments like the ones you can see in [8], one per account. As you can see I've configured gpg and placed (encrypted) passwords in an encrypted directory. Using gpg you can also specify the passphrase in a file passed on the command line to automatize the workflow.

Using mbsync -a you will synchronize local directories with remote IMAP server.

When I was synchronizing gmail for the first time I've got banned with a strange error "Lockdown in sector 4". That's because Gmail does not support such an use-case ( downloading 10k emails in a day ;) ) therefore do it when you're basically free from emails or synchronize just a part of mails per day.


To send emails, the configuration is really similar to the one of mbsync. In my .msmtprc[9] you can see that I've configured the options to manage the encrypted passwords and basic informations such as the remote SMTPS server, tls support and the logfile.

When using multiple accounts you only need to replicate the part which configures Gmail.


Mairix is a tool which is almost drop in, you only need to instruct it where you have your maildirs and which directories it must search [10].

Choose a name for the resultant maildir and set it in .muttrc. The following lines from .muttrc configure mairix search:

macro generic ,f "mairix " "search with mairix"
macro generic ,,f "=.Search" "load the search results mailbox"

Pressing ,f you will search for emails (e.g. pressing ,f thesis you can search for the emails containing "thesis"), when the search is done you can display it using ,,f.

When you want to index new emails (such as every time you receive an email), you need to use: mairix -v -F to index the configured mailboxes.


Imapfilter is one of the most powerful tools in this list. It is configurable by using lua scripts. You can find mine here[11].

The configuration script (~/.imapfilter/config.lua) gets executed every time you issue imapfilter. In details the filter posted does:
  1. Read the password file and login to Gmail;
  2. Get the messages of the last 30 days;
  3. Filter them by searching social@network.com in the "From" header;
  4. Put the matched messages in a specific directory (which probably I will never read ;) ).


As I said previously, the mail synchronization is a manual process, to automatize it I have a simple script [12] that does, in order:
  1. Synchronize emails using mbsync
  2. Filter them server side using imapfilter
  3. Index the emails using mailrix
The system executes this script every 5 minutes during the day using cron:

$ crontab -l
*/5 * * * * /home/user/mbsync.sh

That's it! If you have some suggestions or problems with a similar setup do not hesitate to contact me...via email ;)

You can find my address (a little obfuscated) in the home page.


[1] mutt Gitlab repository
[2] isync/mbsync Github repository
[3] msmtp official site
[4] mairix Github repository
[5] imapfilter Github repository
[6] my .muttrc
[7] my per-account muttrc
[8] my .mbsyncrc
[9] my .msmtprc
[10] my .mairixrc
[11] my imapfilter.lua
[12] Script to synchronize emails