How 'mark as spam' and Sieve works together?

Hello,

My email setup based on Dovecot and rspamd. I configured managesieve service in Dovecot and Sieve integration in Mail app.

Is this possible to run user Sieve filter when it marks some message as spam/not spam?
How to catch this event?

Sieve script for all users account will be the same. Is there way to autoprovision it to the all users email accounts? Existing and new ones.

As it is in all popular public email services. When user mark message as spam, I want to trigger Sieve filter which moves message to Spam folder and with rspamc sends spam signature to rspamd server.
Can it be done by this way with Mail app Sieve integration?

If Sieve can do it then you can configure this through our app. User account provisioning is a supported feature.

English not my native language maybe I not clearly write question.

Usually Sieve filter look like this.
If header contains something do thisā€¦

require "fileinto";
if header :contains "X-Spam" "Yes" {
 fileinto "Junk";
}

When user mark message as Spam in Mail app, on message icon appears exclamation mark and word ā€˜Junkā€™. But mail headers does not change.

Screenshot 2021-07-28 10.26.39

How to catch flags or event in SIeve script when user mark message as spam?
Could you please provide a simple example of such Sieve filter?
What should be in IF condition in the Sieve script to handle a message marked as spam?

require "fileinto";
if ??? {
 fileinto "Junk";
}

# Move spam to spam folder
if anyof (header :contains "X-Spam-Flag" "YES", header :contains ["X-Spam", "X-Spam-Status"] "Yes") {
  fileinto :create "Junk";
  # Stop here so that we do not reply on spams
  stop;
}

Seems I found how it works.

  1. When user mark message as spam Mail app sets IMAP flag ā€˜$junkā€™ to it

  2. This can be checked (for Dovecot IMAP server) by

  # found user 'olya' mailbox guid
  $ sudo doveadm search -u olya mailbox inbox  
  3949a11b5aad026192010000c3bd5c8a 14

  # View message IMAP flags
  $ sudo doveadm fetch -u olya "flags uid" mailbox-guid 3949a11b5aad026192010000c3bd5c8a
  flags: \Seen $junk
  uid: 14

  1. When user mark message as not spam. Mail app changed this flag to ā€˜$notjunkā€™

  2. To use this in Sieve filter script you need:

  • configured managesieve service on your IMAP server to upload user defined Sieve filter scripts
  • configured imapsieve service on your IMAP server to run Sieve filter script on IMAP events
  1. For user defined Sieve script you then can catch when message will be marked as spam with something like this
  require ["imap4flags", "fileinto", "environment", "imapsieve"];

  if anyof (hasflag "$junk", 
            environment :is "imap.changedflags" "$junk") {
    fileinto "INBOX.Spam";
    stop;
  }

To developers: This was interesting to investigate how it works. But, please, add a few words how Sieve integration works to Mail app admin documentation.

1 Like

What info would you expect?

  1. General information and typical use of Mail App and Sieve integration.
    For what cases it designed for and for what not.
    What it can do and what not.

  2. Information that Mail App menu item ā€œmark as spam/not spamā€ manipulates messages IMAP flags.

  3. Maybe, simple example of Sieve filter script, for use described in item #1, by reading message IMAP flags.

Did anyone else get this working? Weā€™re trying to implement it on our servers, but the message never gets moved.
We have enabled imapsieve following iRedMail docs at Auto learn spam/ham with Dovecot imap_sieve plugin (iredmail.org)
It is working, if users move their messages manually to junk folder the reporting script is executed.

But we havenā€™t been able to make the user-defined script work. Although the message flags are changed (verified using doveadm fetch) the message is never moved. It looks like the ā€œimap.changedflagsā€ environment variable is always empty.

Sieve needs a complete UI to make and delete rules with, in fact. No professional email program worth so much as a warm cup of spit fails to have filter rule capabilities.

I made my own manually but the average mail user would be completely unable to navigate this kind of interface, and this feature alone makes using this mail app mostly a non-starter in a corporate setting.

@KimmoJ have a look at https://nextcloud.com/contribute/code-of-conduct/ please.

A sieve UI is something we want to have. The vacation autoresponder did the base work for a Sieve UI. The work can be picked up by an engineer any time.

Subscribe to Filtering of Incoming messages Ā· Issue #44 Ā· nextcloud/mail Ā· GitHub to learn more.