ImapFilter - a powerful mail sorting tool

At work, I have to work daily with a large number of letters, and in order not to lose sight of important information, they need to be sorted. At first, the configured Thunderbird completely coped with this task on a working machine, but I wanted to check mail not only at work, but also at home, and somewhere else - in a word, in any convenient place if there is a desire and opportunity. Thoughts about a certain server solution began to appear. It would seem - “everything has already been stolen to us” (c) - a bunch of fetchmail + procmail + (exim / postfix / sendmail, etc.) + mutt is quite working and has proven itself well, but I did not need all the features of such powerful email programs, I wanted minimalism, simplicity and functionality.

As many of you probably know, mutt is initially able to work with the mail server using the imap protocol, and it checks wonderful mail on its own (i.e. we no longer need fetchmail), it also knows how to send mail through third-party smtp- server (i.e. MTA is also not needed), it remains only to solve the problem of filtering mail. And here he comes to the rescue - imapfilter.

Imapfilter, as the name implies, filters mail by connecting to the mail server using IMAP. The possibilities of this program are truly endless, with its help you can do almost everything (create / delete / change folders, filter / copy / move / delete letters, set various flags, sort and search letters, etc.).

The configuration file of the program is a set of rules written in lua language (after looking at typical examples of the config and studying the manual there should be no difficulties with writing your own rules).

So, a little basic setup instructions.

Suppose we have a debian-like system. We put the imapfilter package:

$ sudo apt-get install imapfilter

And after installation, create the .imapfilter folder in our home directory, and in it the configuration file with the name "config.lua":

$ mkdir ~/.imapfilter
$ touch ~/.imapfilter/config.lua

Now let's start editing the config. Let the standard Nano be the editor:

$ nano ~/.imapfilter/config.lua

Everything that starts with "-" in the configuration file is considered comments and, accordingly, is not processed.

-- Таймаут (в секундах), в течение которого нужно ждать ответ от сервера --
timeout = 120
-- Сообщаем, что мы хотим вручную указать имена почтовых ящиков (читай, imap-каталогов) --
options.namespace = false
-- Настройка учетной записи --
account_name = IMAP {
                      server = 'mailserver.ru',
                      username = 'your_login',
                      password = 'your_pass',
   }

Here, if necessary, you can add the fields 'ssl' and 'port', filling them with the necessary values.
Next, we determine the list of folders, and check the current status of the directory with incoming mail:

mailboxes, folders = account_name:list_all()
account_name:check_status()

The “list_all ()” option will return a list of all available directories and subdirectories in your mailbox, and the “check_status ()” option , respectively, will provide information on how many messages are in your mailbox, how many are sent, how many are unread (before filtering is necessary make sure that you have already created all the necessary directories for which you will lay out mail, or you can use the create_mailbox function to create a new directory).
Then we define the “results” variable, which we will use in future to filter mail:

results = account_name.INBOX:select_all()

Now you can go directly to the rules. Roughly speaking, a typical rule would look something like this:

res = results:contain_subject('hello')
res:move_messages(account_name.folder)

Where:
  • res - an arbitrary variable name that will be responsible for letters matching the condition;
  • results: contain_subject ('hello') - the filtering process itself is carried out here;
  • res: move_messages (account_name.folder) - action that will be performed on letters.

You can filter messages by sender ( contain_from ), by recipient ( contain_to ), by subject ( contain_subject ), by “Copy” field ( contain_cc ), by size ( is_larger and is_smaller ), by date ( is_older , for example, if the letter is older than that a certain number of days) and on a bunch of other parameters (see the link at the bottom of the page).

After filtering the letters, you can copy ( copy_messages ), move ( move_messages ), mark as read / unread / delete, etc., and delete ( delete_messages )

Well, as an example, consider the case when letters from user@domain.ru will be copied to the user folder in the Inbox directory, letters with the subject "Hello, world" will be moved to the spam folder, and all letters in the work folder that are older than 10 days will be deleted.

res = results:contain_from('user@domain.ru')
res:copy_messages(account_name.user)
--
res = results:contain_subject('Hello, world')
res:move_messages(account_name.spam)
--
old = account_name.work:is_older(10)
account_name.work:delete_messages(old)

In the filtering process, you can use several conditions at the same time, this is implemented using the logical operators: "+" (logical "OR"), "*" (logical "AND") and "-" (logical "NOT"):

res = results:contain_subject('Hello, world')+
      results:contain_from('user@domain.ru')
res:move_messages(account_name.spam)

I think a further analogy is clear. Imapfilter can also work with subdirectories in your mailbox (for example, Inbox / work / 1 or Inbox / work / 2). In this case, when performing an action on a letter, the record of the path to this subdirectory will be slightly different:

res:move_messages(account_name['work/1'], results)

In principle, this knowledge is already enough to sort mail for most tasks. After completing the settings, you need to set the access rights accordingly: 700 to the .imapfilter directory and 600 to the config file:

$ chmod 700 ~/.imapfilter
$ chmod 600 ~/.imapfilter/config.lua

After completing all the settings, run imapfilter in debug mode and write a log with the following keys:

imapfilter -l logfile.log -d

A log file with the corresponding name and a file with the name “Debug. *****” will appear in the program’s home folder, which will contain all the information on the program’s actions - there you can see which rule and how it worked.

And finally, add a line to crontab

* * * * * imapfilter

so that our filter runs every minute and beautifully arranges mail in the right folders.

Here you can find all the options and parameters that are available in the configuration file.

That's all, thanks for your attention.

Also popular now: