Posted in Communications, Technology

How to filter messages hosted by any email server

Some time ago, I caught our email hosting company reading my inbox. I access email in a web browser, and the browser status line displayed that a script was continuously polling for new messages and relaying data to another organization. That was it. I instantly subscribed to a European hosting company that has to obey GDPR, and I quit all business with the U.S. based hosting company.

I had a backup of all messages, so I could and still can search for any old message – they are archived on a local hard drive. Switching email providers, however, introduced another problem: managing spam. The U.S. email provider had a slick web user interface and excellent spam filtering functionality. The spam filter deployed by the European email hosting company simply doesn’t work.

So, I had to find a solution that acts like an email client, can interact with the email server via IMAP or POP protocol, and can move, delete, and copy messages on the server according to my instructions. Sure, there are email client applications that can filter messages, but that’s not for me. Web mail is the universal method that works on any device on any operating system. In essence, I need a filtering program that runs independently from the email access method.

The solution I found is imapfilter. It was easy to configure for simple filtering, and it worked from the first try.

As the name implies, imapfilter works with any email server that allows access with IMAP protocol. All widely deployed email server software packages support IMAP, and unless your email host hasn’t blocked IMAP for some reason, imapfilter can access your mailbox.

Another thing to note with imapfilter is that it only runs on Linux. It is open source software, available for download here, or you can install it directly from a package manager system of your Linux distribution. Email filtering apps for Windows are introduced in this article.

How to set up imapfilter for spam filtering?

Install the program. For instance, on Debian and other distributions with the apt package management system:

apt-get install imapfilter

After installation, you should have a directory named .imapfilter under your home directory. Using your favorite editor, create a file called config.lua in that directory. Below is my configuration, you can find the full original sample from Github.

-- Options --
options.timeout = 120
options.subscribe = true

-- Accounts --
-- Connects to "imap.mail.server" as user "user" with "secret" as the password.
accounteu = IMAP {
server = 'imap.mail.server',
username = 'user',
password = 'secret',
ssl = 'ssl23',
-- Get a list of the subscribed mailboxes and folders
mailboxes, folders = accounteu:list_subscribed()
-- List unread messages
results = accounteu.INBOX:is_new()

The parameters for accessing the inbox have been specified, but there is no action yet. Now, let’s tell the program what kind of messages we want to filter:

-- Select unseen messages, and filter them based on the content in the "From" field.
results = accounteu.INBOX:is_unseen() *
accounteu.INBOX:contain_from('') +
accounteu.INBOX:contain_from('ionlysendspam@') +
accounteu.INBOX:contain_from('') +
-- Move the messages that meet the defined criteria to the Spamfilter folder on the server.

Now, you can save the configuration file, and run the program:


And you should see something like this:

6 messages moved from

Imapfilter can create a new folder if it detects that the destination folder doesn’t exist. I only used the message sender as the filter field, but the subject and other fields can be used as well.

There are plenty of commands, such as copy, delete, and others to execute for the filtered messages. View the full sample configuration file for details. You can also specify an external dynamic filtering program to do the filtering for imapfilter, so you don’t have to hardcode the filters in the configuration file. See the extend.lua sample configuration for more information.

Leave a Reply

Your email address will not be published. Required fields are marked *