2021-07-11 00:00:00

Communications|Technologyemail|filter|hosting|imapfilter|self-hosted|spam|webmail

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 = 120options.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 foldersmailboxes, folders = accounteu:list_subscribed()-- List unread messagesresults = 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('@iamaspammer.com') +
accounteu.INBOX:contain_from('ionlysendspam@') +
accounteu.INBOX:contain_from('spam@spam.net') +
accounteu.INBOX:contain_from('yesthis@isspam')
-- Move the messages that meet the defined criteria to the Spamfilter folder on the server.
results:move_messages(accounteu.Spamfilter)

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

imapfilter

And you should see something like this:

6 messages moved from a----@----.com@S----.----.NET/INBOX
to a----@----.com@S----.----.NET/Spamfilter.

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.

The historic center of Krakow in Poland features castles, churches, palaces ... and tourists.

2025-06-23 13:31:04

The historic center of Krakow in Poland features castles, churches, palaces ... and tourists.


A man with his donkey.

2025-06-19 15:54:21

arihak

A man with his donkey.


News

2025-06-19 15:11:00

An eye-catching place for a painting.#streetphotographyhttps://wordpress.org/photos/photo/9536554ee4/


News

2025-06-15 15:33:00

There are many ways to classify the best countries for road trips, and that is why we are going to take a look at a few different types of ways to select the best here. #roadtrip #Europehttps://klaava.com/which-countries-in-europe-are-the-best-for-road-trips/


News

2025-06-11 16:24:00

Let's go.#streetphotographyflic.kr/p/2r3EuJNhttps://flic.kr/p/2r3EuJN


News

2025-06-04 18:08:00

A #photographer has been using sunglasses with a camera and loudspeakers for 8 months: "I quite enjoy them, and they’ve quickly become my daily wear as far as sunglasses are concerned."https://fstoppers.com/artificial-intelligence/ray-ban-meta-glasses-photographers-perspective-701899


News

2025-06-02 14:42:00

Today, digital nomads recognize the benefits but also risks of their lifestyle. An extensive study among traveling workers reveals the highs and lows of the nomad life. #remotework #digitalnomadhttps://klaava.com/digital-nomads-feel-powered-by-their-lifestyle-but-recognise-inevitable-downsides-as-well/


Aland archipelago

2025-05-30 16:59:41

Aland archipelago


Highway across a swamp.

2025-05-30 11:14:51

arihak

Highway across a swamp.


News

2025-05-26 16:29:00

Flower market, but she is selling fruit trees.#streetphotographyhttps://unsplash.com/photos/TfQ6uYmI9QQ


News

2025-05-22 14:19:00

In Spain, the Balearic government asked influencers to help ease pressure on overcrowded hotspots by promoting less-visited areas. Instead, influencers have unintentionally drawn large crowds of tourists to remote, ecologically sensitive locations. #photography #selfiehttps://petapixel.com/2025/05/21/spanish-islands-ban-influencers-after-4000-tourists-flock-to-tiny-beach-for-selfies/


News

2025-05-18 18:23:00

Most travelers already knew that south Europe is the warm and sunny zone of #Europe, and this ranking confirms it. Spain and Italy have most cities in the top 30, followed by France and Portugal. #travelhttps://klaava.com/sunniest-cities-in-europe/


Popular sight in Sevilla

2025-05-16 17:51:34

Popular sight in Sevilla


News

2025-05-14 16:03:00

Bordeaux is ready for hot summer days.#streetphotographyhttps://pixelfed.social/p/arihak/827854641319295061


Cooling zone.

2025-05-13 12:38:47

arihak

Cooling zone.


Hochosterwitz castle

2025-05-10 15:23:05

Hochosterwitz castle


News

2025-05-07 14:39:00

A meeting in winter sun.#streetphotography #travelphotographyflic.kr/p/2qey2NHhttps://flic.kr/p/2qey2NH


News

2025-04-30 17:01:00

Shady character.#streetphotography #travelphotography #StreetPhotography


Not in space, but firmly on the ground in Valencia.

2025-04-25 17:47:30

arihak

Not in space, but firmly on the ground in Valencia.


News

2025-04-24 15:17:00

According to a survey, football (a sport where you are supposed to kick the ball instead touching the ball with a hand) is the most popular sports that fans want to follow live on site even if it means traveling overseas. #travelhttps://klaava.com/sports-events-are-trending-among-travelers/