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.

News

2025-01-15 08:59:00

Not to mention travel – when real life experiences in a destination don’t match the expectations and everything that can go wrong, goes wrong. Well, that’s the beauty of #travel. #Europehttps://klaava.com/nitpickers-travel-journal-travel-lessons-learned-in-2024/


News

2025-01-11 13:25:00

A conclusion from the Traffic Scorecard 2024 results is it takes about double the time to drive along the streets of London (the most congested city in #Europe) than the streets of Munich, which is ranked the 21st most congested European city. #traffic #travelhttps://klaava.com/the-cities-in-europe-where-people-are-stuck-in-traffic-most-often/


A shop dedicated to tasty juice

2025-01-09 14:04:10

arihak

A shop dedicated to tasty juice


in the busy old town center.

2025-01-08 16:16:08

in the busy old town center.


News

2025-01-07 14:56:00

A reminder for keeping your data safe when traveling. Why? It is likely that you have to access your most critical data in risky situations on the road.https://cybernews.com/privacy/how-to-protect-your-sensitive-information-while-traveling/


News

2025-01-02 16:33:00

What about the scenery, you who are hurrying to the other side?#streetphotography#streetphoto


News

2025-01-01 16:06:00

According to a survey, third of travelers are victims of online booking scams. Since it is so convenient to make travel reservations online, that’s what we do. The problem is that we haven’t adopted new methods to avoid online scams. #travel #scamhttps://klaava.com/a-traveler-is-more-likely-to-get-scammed-when-booking-a-trip-than-being-robbed-in-a-destination/


Perhaps road builders had too much time?

2024-12-29 15:19:44

Perhaps road builders had too much time?


News

2024-12-27 18:25:00

#Photography is full of so-called “rules” that can feel overwhelming. However, some of these “rules” are downright myths.https://www.diyphotography.net/debunking-seven-common-myths-photographers-still-believe-in/


What should I read next?

2024-12-25 15:18:22

arihak

What should I read next?


News

2024-12-23 11:34:00

Since #EU #DMA has specified #Apple as a #gatekeeper in #mobile devices, EU considers Apple should implement several iOS connectivity features, predominantly used for and by connected devices. For instance, notifications, automatic Wi-Fi connection, AirPlay, AirDrop, or Bluetooth audio switching.https://digital-markets-act.ec.europa.eu/commission-seeks-feedback-measures-apple-should-take-ensure-interoperability-under-digital-markets-2024-12-19_en


News

2024-12-21 14:51:00

Smartphone users in the US are not impressed with the latest artificial intelligence features on their devices with 73 percent of Apple users and 87 percent of Samsung users unsatisfied, according to a new study. #phone #AIhttps://petapixel.com/2024/12/19/majority-of-smartphone-users-are-unimpressed-with-ai-features-study-finds/


There should be enough ice already to open the hotel

2024-12-20 16:40:17

There should be enough ice already to open the hotel


Hiking on a mountain experience: the end of an ancient tunnel

2024-12-09 14:12:31

arihak

Hiking on a mountain experience: the end of an ancient tunnel


on a hot summer day

2024-12-02 18:47:01

on a hot summer day


Cloudy day in a valley

2024-11-25 16:31:59

arihak

Cloudy day in a valley


In the shade of a lone palm tree

2024-11-25 15:46:29

In the shade of a lone palm tree


an abandoned town?

2024-11-07 18:35:22

an abandoned town?


A quiet moment between heavy rain

2024-10-24 17:34:45

A quiet moment between heavy rain


Everyone loves a pretty mountain scenery

2024-10-24 16:54:02

arihak

Everyone loves a pretty mountain scenery