Reading emails: mutt + mbsync + mairix + imapfilter
27 September 2020
Hi!
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:
mutt
-- I love mutt[1]. It is fast, simple to use and super customizable. If you've read the first page I hate html email. That's for mutt.mbsync
-- I want to have my email backup in a local (encrypted) storage. I don't want to lose them because an online service stop working and I've not done the opt-out. ;)
mbsync[2] just do this task. It download the emails to a configurable directory.
Also, having the e-mails in a local storage makes mutt ultra-fast.
The only downside is that I need to sync manually the mail every N minutes. That makes the synchronization process a less real-time (but its for email...it is ok to wait 5 minutes to get a notification). If I'm waiting for an email which I know was sent, I can synchronize them manually.msmtp
-- SMTP must be simple (as the name of the protocol states). msmtp[3] is a simple tool wrote with UNIX philosophy. You simply put the mail in it and it will do its SMTP-related things. Straightforward configuration and easily paired with mutt.mairix
-- While I do not have a lot of emails (~5k per account with 4 accounts), I do not want to lose time searching with clumsly IMAP search. Therefore I use mairix[4] with mutt integration to search them.
Before mairix, I've used mu for this task. The problem is that mu uses redis, which is really slow on my OpenBSD configuration on the Raspberry pi (Probably, the encrypted disk is incompatible with the giant database file created by redis).
imapfilter
-- Another thing I want to do is automatically move secondary emails from the INBOX to a specific directory. I don't want to do it only locally but also on server side (maybe I can't access my system to read emails, but I can do it using the awkward web-interface).
To do so I use imapfilter [5], which is a really simple program to write server side filters and take actions. It is configurable in LUA.cron
-- To automatize synchronization I use a simple crontab. A script to synchronize emails gets executed every 5 minutes.
That's for my setup, let's see in detail the configurations:
mutt
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?).
mbsync
.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.
msmtp
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
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
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:
- Read the password file and login to Gmail;
- Get the messages of the last 30 days;
- Filter them by searching
social@network.com
in the "From" header; - Put the matched messages in a specific directory (which probably I will never read ;) ).
cron
As I said previously, the mail synchronization is a manual process, to automatize it I have a simple script [12] that does, in order:- Synchronize emails using mbsync
- Filter them server side using imapfilter
- Index the emails using mailrix
$ 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.
Bye,
D.
[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