What does HackerNews think of imapfilter?

IMAP mail filtering utility

Language: C

My first real exposure to Lua was as the configuration language of imapfilter. Absolutely loved both.

https://github.com/lefcha/imapfilter

You can also do more complicated things on the receiving side of the email, with something like imapfilter (or just a thunderbird incoming mail rule on a desktop PC at your house), to auto sort alerts or notices of nominal condition into certain folders/subfolders.

https://github.com/lefcha/imapfilter

If you just need email processing locally, I have found imapfilter to be quite nice. You can script it in Lua.

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.

[0]: https://github.com/lefcha/imapfilter

Yup - https://github.com/lefcha/imapfilter

"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 can recommend https://github.com/lefcha/imapfilter.

I have it running in a private serving, filtering the emails for me..

This is interesting; what benefit/advantage does it have over IMAP-level email management tools like imapfilter[1]?

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.

[1]: https://github.com/lefcha/imapfilter

If you're looking to consolidate your email the best way is imapfilter https://github.com/lefcha/imapfilter I use this LUA script (this is the Jinja2 template so obviously you'll need to substitute the portions in {{ }} with your real values. It runs on an hourly cronjob on my server and sucks down all the mail from Gmail and another account and then puts it in my main inbox via IMAP (not forwarding) so you don't have to worry about opportunistic transport encryption. It also does not mess with the headers which forwarding emails invariably does.

  -- 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()
Wouldn't using something like https://github.com/lefcha/imapfilter also be an option?

It would be awesome if some day there was a "jmapfilter". I think JMAP would be really efficient for this use case.

Imapfilter [1] would work.

It also uses regexes so it would be easy to convert mike-cardwell's rules.

[1]: https://github.com/lefcha/imapfilter

I use imapfilter[0] to sort imap mails on the remote server and move older ones to a local dovecot instance for long term backup. That way I have current mail available on all my devices and access to the backup via mutt or via file in the MH format on the backup system.

That works, but you have to program a bit lua in order to setup imapfilter correctly.

[0] https://github.com/lefcha/imapfilter

I think that IMAP-serverside filtering is possible: https://github.com/lefcha/imapfilter I haven't really looked into exactly what it's doing though (and I'm no expert on the IMAP spec).