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-04-23 14:19:00

An unexpected recognition for analog #photography : UNESCO Cultural Heritage designation suggests a renewed appreciation for the craftsmanship and cultural significance of analog methods.https://www.diyphotography.net/analog-photography-recognized-as-intangible-cultural-heritage-by-unesco/


News

2025-04-17 13:18:00

New restriction on Flickr #photo sharing: Free #Flickr accounts will be restricted from downloading original and large-size images. While users will still be able to upload photos of all sizes, free account holders will only be able to download medium and small photos.https://petapixel.com/2025/04/16/flickr-restricting-download-sizes-for-free-accounts/


News

2025-04-16 10:54:00

Morning commute.#streetphotography#travelphotography


News

2025-04-12 09:17:00

The region in #Europe where you will get more #travel days for your euro (or whatever the local currency happens to be). The difference in the average cost is vast between the cheapest and the most expensive European countries.https://klaava.com/here-are-the-cheapest-countries-to-travel-in-europe/


Looks like a perfect spot for a cat to observe the street but no one was home when I passed the window.

2025-04-10 13:35:48

arihak

Looks like a perfect spot for a cat to observe the street but no one was home when I passed the window.


News

2025-04-07 08:50:00

When you take a #photo in RAW format the file is filled with extra data that allows for much richer post-processing, but the #camera world has never actually settled on one standardized RAW format.https://www.theverge.com/tech/640119/camera-raw-spec-format-explained-adobe-dng-canon-nikon-sony-fujifilm


News

2025-04-01 08:39:00

YouTube was the second-biggest media company in the world last year.And in 2025, #YouTube should eclipse #Disney, and become the biggest #media company in the world.https://www.businessinsider.nl/youtube-is-about-to-eclipse-disney-as-the-biggest-media-company-in-the-world/


News

2025-03-28 08:36:00

World Press Photo Contest 2025 ​have just been announced. This year, according to organizers, 59,320 images were submitted for judging, made by 3,778 photographers. #photographyhttps://www.theatlantic.com/photo/2025/03/winners-2025-world-press-photo-contest/682180/


Look around, and love your phone later

2025-03-22 10:17:39

arihak

Look around, and love your phone later


News

2025-03-21 12:50:00

An ancient football stadium?#streetphotography


News

2025-03-11 13:32:00

The World Photography Organisation has announced the category winners and shortlisted photographers for the Sony World #Photography Awards 2025 Open competition.https://www.dpreview.com/articles/0086546939/sony-world-photography-awards-2025-category-winners


News

2025-03-06 16:56:00

Coffee break.#streetphotography


News

2025-02-27 08:22:00

The Sony World #Photography Organisation unveiled the shortlisted entries and finalists in its annual competition’s 10 professional categories.https://edition.cnn.com/2025/02/25/style/sony-world-photography-awards-professional-2025/index.html


Summer heat.

2025-02-26 17:06:07

arihak

Summer heat.


News

2025-02-24 16:42:00

So, Toyota has built a neighborhood for 100 people to live in:Woven City near Mount Fuji is where #Toyota plans to test everyday living with robotics, artificial intelligence and autonomous zero-emissions transportation. #AI #EVhttps://techxplore.com/news/2025-02-rich-cash-japan-automaker-toyota.html


News

2025-02-23 08:46:00

I have written about shady business practices of Airbnb at Klaava.com for years and now ....#Airbnb has turned into a fright show of consumer train wrecks. Every few hours, someone adds a new personally observed outrage to an ongoing litany of allegations, accusations, and perceived swindles.https://www.frommers.com/tips/hotel-news/heres-why-more-and-more-people-are-done-with-airbnb-its-quite-a-list/?utm_source=flipboard&utm_medium=activitypub


News

2025-02-17 17:44:00

In addition to sewing machines and other things, Singer used to make computers.#streetphotography


Breezy beach house

2025-02-13 16:17:25

Breezy beach house


News

2025-02-13 08:29:00

Many of Hong Kong’s basketball courts are located on rooftops. Bell used satellite images to map out potential locations before using his drone to snap photos. #photography #bookhttps://edition.cnn.com/2025/02/12/style/hong-kong-basketball-photography-austin-bell-hnk-intl/index.html


News

2025-02-12 08:39:00

People have nothing better to do than #travel wherever an #influencer tells them to go:A war between a TikTok influencer and Italian ski resorts has escalated after she urged her two million followers to “invade” one destination because she was blamed for causing chaos at another resort. #Italyhttps://edition.cnn.com/2025/02/11/travel/tiktok-hordes-intercepted-by-police-italy-ski-resort/index.html