Reputation: 292
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.";
Upvotes: 1
Views: 657
Reputation: 3643
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";
}
Upvotes: 1