pigeohole-sieve is a great extension for dovecot mailserver. One thing I’m missing is the ability to forward a processed message as attchment from (imap) sieve filters.
According to the developers «forward-as-attachment» is in pipeline for future release but for the moment – with the redirect command – a message can only be forwarded inline.
But fortunatly sieve knows external programs where a message can be piped to. Let’s assume that we want to forward messages that are moved via imap from folder «Spam» to «INBOX» else to a specific address «report@domain.tld«.
First we define the imapsieve in /etc/dovecot/conf.d/90-plugin.conf
1 2 3 4 5 6 7 8 9 10 | sieve_plugins = sieve_imapsieve sieve_extprograms sieve_extensions = +vnd.dovecot.filter +vnd.dovecot.pipe +vnd.dovecot.execute +vnd.dovecot.environment sieve_filter_bin_dir = /etc/dovecot/sieve-filters sieve_pipe_bin_dir = /etc/dovecot/sieve-filters sieve_execute_bin_dir = /etc/dovecot/sieve-filters sieve_global_dir = /home/vmail/dovecot imapsieve_mailbox1_name = INBOX imapsieve_mailbox1_from = Spam imapsieve_mailbox1_causes = COPY APPEND imapsieve_mailbox1_before = file:/home/vmail/dovecot/report.sieve |
Then we create report.sieve file
1 2 3 4 5 6 7 | require ["vnd.dovecot.pipe", "copy", "imapsieve", "environment", "variables"]; if environment :matches "imap.user" "*" { set "username" "${1}"; } pipe "report.sh" "${username}"; |
Quite standard so far. The magic is done in report.sh which dovecot will look for in sieve_pipe_bin_dir. It’s just a bash script which acts as a wrapper for the forward. In my case I use swaks
1 2 3 4 5 6 7 8 9 10 11 12 | #!/bin/bash file=$(mktemp) subject='' while IFS= read -r line ; do [ "x$subject" = 'x' ] && [[ "x$(echo $line|egrep ^Subject:.*$)" != 'x' ]] && subject="$(echo $line|awk -F'Subject: ' '{print $2}')" printf "$line" >>$file done </dev/stdin sa-learn --ham $file swaks --server SERVER_TO_SEND:24 --from "$1" --to "report@domain.tld" --lhlo "MY_HELO_STRING" --header "Subject: [PREPEND_STRING]: $subject" --attach-name report.eml --attach $file --protocol LMTP -4 --body "Forward message from $1" >/dev/null 2>&1 rm $file |
The example above uses LMTP to deliver the forwarded message. Just remove –protocol LMTP and change/remove the port specification to use default SMTP
Leave a Reply