简体   繁体   中英

What configuration is needed to remove attachments from mails in Dovecot Sieve?

How can I remove specific attachments from mails in dovecot sieve mail server? I have tried several configurations but the attachments were not removed. But it did not know keyword "replace" and I don't know what it requires. I am using https://github.com/docker-mailserver/docker-mailserver . Do I need a plugin or extenstion for doing that? Or what is the simplest configuration to do that?

I tried replace:mime:contenttype "text/plain" "This email originally contained an attachment, which has been removed.";

As I mentioned here , you have to use sieve extprograms plugin to filter incoming messages. Vanilla dovecot does not have specific sieve plugin to modify part of multipart MIME message.

First of all, you'll need to edit dovecot's 90-sieve.conf to enable +vnd.dovecot.filter :

...
sieve_global_extensions = +vnd.dovecot.pipe +vnd.dovecot.filter
...
sieve_plugins = sieve_extprograms
...

Specify program location in 90-sieve-extprograms.conf :

  sieve_pipe_bin_dir = /etc/dovecot/sieve-pipe
  sieve_filter_bin_dir = /etc/dovecot/sieve-filter
  sieve_execute_bin_dir = /etc/dovecot/sieve-execute

Create these directories:

mkdir -p /etc/dovecot/sieve-{pipe,filter,execute}

Then, write some filter program to strip attachments like following:

#!/usr/bin/python3
import sys
from email.parser import Parser
from email.policy import default
from email.errors import MessageError

def main():
    parser = Parser(policy=default)
    stdin = sys.stdin

    try:
        msg = parser.parse(stdin)
        for part in msg.walk():
            if part.is_attachment():
                part.clear()
                part.set_content(
                    "This email originally contained an attachment, "
                    "which has been removed."
                )
    except (TypeError, MessageError):
        print(stdin.read())  # fallback
    else:
        print(msg.as_string())

if __name__ == "__main__":
    main()

Save this script as /etc/dovecot/sieve-filter/strip_attachments.py and make it executable:

chmod +x /etc/dovecot/sieve-filter/strip_attachments.py

Your sieve script should look like following:

require "mime";
require "vnd.dovecot.filter";

# Executing external programs is SLOW, should be avoided as much as possible.
if header :mime :anychild :param "filename" :matches "Content-Disposition" "*" {
    filter "strip_attachments.py";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM