How 'mark as spam' and Sieve works together?


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

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";

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.

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.