简体   繁体   English

从 Dovecot Sieve 中的邮件中删除附件需要什么配置?

[英]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?如何从 dovecot sieve 邮件服务器中的邮件中删除特定附件? 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 .我正在使用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.";我试过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.正如我在这里提到的,您必须使用 sieve extprograms插件来过滤传入的消息。 Vanilla dovecot does not have specific sieve plugin to modify part of multipart MIME message. Vanilla dovecot 没有特定的筛选插件来修改多部分 MIME 消息的一部分。

First of all, you'll need to edit dovecot's 90-sieve.conf to enable +vnd.dovecot.filter :首先,您需要编辑 dovecot 的90-sieve.conf以启用+vnd.dovecot.filter

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

Specify program location in 90-sieve-extprograms.conf :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:将此脚本保存为/etc/dovecot/sieve-filter/strip_attachments.py并使其可执行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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