📜 ⬆️ ⬇️

ImapFilter - a powerful tool for sorting mail

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, a Thunderbird set up on a working machine handled this task quite well, but I wanted to check mail not only at work, but also at home, and elsewhere — in a word, in any convenient place if there was a desire and opportunity. Thoughts began to appear about some kind of server solution. It would seem - “everything is already stolen before us” (c) - a bunch of fetchmail + procmail + (exim / postfix / sendmail, etc.) + mutt is quite a working one and has a good reputation, but I didn’t need all the features of such powerful mail programs I wanted minimalism, simplicity and functionality.

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

Imapfilter, as the name implies, filters mail by connecting to an IMAP mail server. The possibilities of this program are truly limitless, 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 the lua language (after viewing typical configuration examples and studying the manual, there should be no difficulties with writing your own rules).
')
So, a little instruction on basic configuration.

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

$ sudo apt-get install imapfilter 

And after installation we create a .imapfilter folder in our home directory, and in it - a configuration file called “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 

Anything that begins with a "-" 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 define a 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 will respectively provide information about the total number of emails in your inbox, how many sent, how many unread messages (before filtering make sure that you have already created all the necessary directories for which you will be laying out your mail, or you can use the “create_mailbox” function to create a new directory).
Then we define the variable "results", which we will use in the future to filter mail:

 results = account_name.INBOX:select_all() 

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

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

Where:

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

After filtering, messages can be copied ( copy_messages ), moved ( move_messages ), marked as read / unread / deleted, etc., and deleted ( 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 “Hello, world” subject 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, it is implemented using 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 entry 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 permissions accordingly: 700 on the .imapfilter directory and 600 on the config file:

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

After all the settings have been completed, launch imapfilter in debug mode and write a log with the following keys:

 imapfilter -l logfile.log -d 

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

And finally, add the line to crontab

 * * * * * imapfilter 

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

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

That's all, thank you for your attention.

Source: https://habr.com/ru/post/159173/


All Articles