DeskTux

Linux on Desktops

User Tools

Site Tools


Sidebar

apps:imapfilter

Imapfilter

One of the greatest benefits of IMAP is the fact that all mails, folders etc. are residing on the server. So it doesn't matter whether you connect with Thunderbird from your local computer, Evolution on your notebook or on vacation using webmail – you will always have the same mails and folders.

As soon as your mail setup and volume grows you might want to use filters for e.g. sorting your mail into different folders. And here it becomes obvious what the problem might be: you define Message Filters in Thunderbird and those work really fine. But on the other systems you must either also define the same rules or keep on sorting manually. The more systems you use the less doable this becomes. But it can already be a big hassle when only using two systems.

If you don't have access to a mailserver using Sieve, this is where imapfilter comes into play. You install imapfilter, define your mail filtering (sorting) rules in a textfile (actually, it's a Lua script) and you can transport your rules to every system where they are needed. If you have access to a shell server you can even run imapfilter daemonized on that system, so only one place to define rules and imapfilter handles the rest!

The real deal however would be to use Sieve. imapfilter sorts mail after it has arrived in your inbox, Sieve will directly deliver the mail into the correct folder (mailbox).

The configuration file

Understanding and writing the configuration file is actually most work. It is quite easy to understand though, check my example below and man imapfilter_config for more information.

More complex setups are easily possible, this is about sorting mails into folders only. Of course you must fill in your account settings accordingly!

imapfilter_config.lua
---------------------
-- General Options --
---------------------
 
-- Otherwise the timeout is infinite
options.timeout = 120
 
-- Newly created mailboxes are automatically subscribed
options.subscribe = true
 
 
----------------------
-- Account Settings --
----------------------
 
myaccount = IMAP {
	server = 'imap.myserver.org',
	username = 'myuser',
	password = 'mypassword',
	ssl = 'ssl2'
}
 
 
-----------
-- Rules --
-----------
 
function SortMail()
 
	-- Forum
	-- Move messages from "no_reply@myforum.org" to the maildir
	-- folder "MyForum".
	messages = myaccount.INBOX:match_from('no_reply@myforum.org')
	myaccount.INBOX:move_messages(myaccount['MyForum'], messages)
 
	-- Mailings
	-- Move messages that contain "mailings.org" in the sender and
	-- "Mailing" in the subject to the maildir folder "Mailings".
	-- "*" is the "AND" operator
	messages = myaccount.INBOX:contain_from('mailings.org') *
		myaccount.INBOX:contain_subject('Mailing')
	myaccount.INBOX:move_messages(myaccount['Mailings'], messages)
 
	-- Administration
	-- Move messages that contain "root" or "administrator" in the
	-- sender to the maildir subfolder "Users/Root".
	-- "+" is the "OR" operator
	messages = myaccount.INBOX:contain_from('root') +
		myaccount.INBOX:contain_from('administrator')
	myaccount.INBOX:move_messages(myaccount['Users.Root'], messages)
 
end
 
-- Daemonize imapfilter and run the rules in function "SortMail" every
-- 60 seconds.
become_daemon(60, SortMail)

Cron

It is nice that imapfilter will run daemonized, but if the host reboots imapfilter won't start automatically. So just put this into your crontab, it will check every minute if imapfilter is running and start it if necessary.

* * * * *	[ `pidof imapfilter` ] || imapfilter
This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
apps/imapfilter.txt · Last modified: 2016-02-16 14:32 (external edit)