Personally I went a bit overboard with it and implemented a simple TCP server in node which queries GitLab APIs based on the email metadata and moves Mails into Folders based on assignees, Labels etc. So letting do imapfilter the heavy IMAP stuff, calling node via tcp and using the response to sort based on it.
"IMAPFilter is a mail filtering utility. It connects to remote mail servers using the Internet Message Access Protocol (IMAP), sends searching queries to the server and processes mailboxes based on the results. It can be used to delete, copy, move, flag, etc. messages residing in mailboxes at the same or different mail servers. The 4rev1 and 4 versions of the IMAP protocol are supported."
I have it running in a private serving, filtering the emails for me..
I don't ask that to be confrontational; I've successfully managed several email inboxes on several different providers for years with plain old IMAP rules, so I'd like to know if there's anything I'm missing.
-- crontab -e
-- 0 * * * * /usr/local/bin/imapfilter
function main ()
-- See 'man imapfilter_config' for an explanation of
-- imapfilter options and functions.
-- Some config examples:
-- https://github.com/lefcha/imapfilter/blob/master/samples/config.lua
-- https://gist.github.com/dylanwh/408810
-- http://www.npcglib.org/~stathis/blog/2012/07/09/linux-task-sorting-mail-with-imapfilter/
-- General Options
options.timeout = 120
options.subscribe = true
-- Accounts
{%- for v in ACCOUNTS | from_json %}
local {{ v.serverName }} = IMAP {
server = '{{ v.server }}',
username = '{{ v.username }}',
password = '{{ v.password }}',
ssl = '{{ v.ssl }}'
}
{%- endfor %}
-- GMAIL
-- Gmail behaves differently to normal imap. With a normal imap acccount
-- you can simply use move_messages(). Google stores both inbox
-- and sent email in the "[Gmail]/All Mail" folder. An email sent to yourself
-- from the same email address exists as a single email and is visible in
-- the Inbox and Sent box. If you delete the inbox mail then the sent mail
-- is also deleted. Thus it is only safe to delete mail once both the Inbox
-- and Sent box is copied.
-- Only go ahead and copy inbox/sent/spam from gmail if I have received
-- some Inbox email.
if (gmail['Inbox']:check_status() > 0) then
inbox_mail = gmail["Inbox"]:select_all()
inbox_copy_success = inbox_mail:copy_messages({{ PRIMARY }}['Gmail Inbox'])
sent_mail = gmail['[Gmail]/Sent Mail']:select_all()
sent_copy_success = sent_mail:copy_messages({{ PRIMARY }}['Gmail Sent'])
spam_mail = gmail["[Gmail]/Spam"]:select_all()
spam_mail:move_messages({{ PRIMARY }}['Gmail Spam'])
-- Only clear the All Mail folder if we were successful in moving
-- our Inbox and Sent mail. Note: move_messages is supposed to return
-- booleans for copying the messages and then marking them as deleted.
-- For some reason the mark for deletion return value is 'nil'.
-- This may need to be [Gmail]/Bin or [Gmail]/Trash check your account!
if (inbox_copy_success and sent_copy_success) then
print('Safe to delete both inbox and sent')
move_inbox = inbox_mail:move_messages(gmail['[Gmail]/Trash'])
move_sent = sent_mail:move_messages(gmail['[Gmail]/Trash'])
if (move_inbox and move_sent) then
print('Emptying bin');
bin_mail = gmail['[Gmail]/Trash']:select_all()
bin_mail:delete_messages()
end
end
end
{%- if SECONDARY != '' %}
-- {{ SECONDARY }}
-- Only go ahead and copy inbox/sent/spam from {{ SECONDARY }} if I have received
-- some Inbox email.
if ({{ SECONDARY }}['Inbox']:check_status() > 0) then
inbox_results = {{ SECONDARY }}['Inbox']:select_all()
inbox_results:move_messages({{ PRIMARY }}['{{ SECONDARY }} Inbox'])
sent_results = {{ SECONDARY }}['Sent']:select_all()
sent_results:move_messages({{ PRIMARY }}['{{ SECONDARY }} Sent'])
spam_results = {{ SECONDARY }}['Junk']:select_all()
spam_results:move_messages({{ PRIMARY }}['{{ SECONDARY }} Spam'])
end
{%- endif %}
end
main()
It would be awesome if some day there was a "jmapfilter". I think JMAP would be really efficient for this use case.
It also uses regexes so it would be easy to convert mike-cardwell's rules.
That works, but you have to program a bit lua in order to setup imapfilter correctly.