Postfix archive preserving Bcc with X-Envelope-To - cleanup confidential X-Envelope-To with Lmtp
Oh YES ! Thanks you so much.
The sieve langugage will not allow iteration over multiple X-Envelope-To
headers.
So one needs to pass it to an external program.
Here is what I came up with:
require "fileinto";
require "imap4flags";
require ["editheader", "envelope"];
require "vnd.dovecot.filter";
if header :contains "X-Spam-Flag" "YES"
{
fileinto "Junk";
stop;
}
if envelope :is "to" "[email protected]" {
filter "EnvelopeToBcc.sh";
} else {
deleteheader "X-Envelope-To";
}
#!/bin/bash
# Store mail in a variable
# input stream
mail=`cat /dev/stdin`
# Extract headers
envelope=$(echo "$mail" | formail -x X-Envelope-To | tr -d '\015')
tos=$(echo "$mail" | formail -x To | tr -d '\015')
copies=$(echo "$mail" | formail -x Cc | tr -d '\015')
# Loop excluding the To: fields and concatenate in variable
bcc=""
for i in $envelope; do
if [[ ${tos} != *"${i}"* && ${copies} != *"${i}"* ]];then
bcc="${bcc} ${i},"
fi
done
# Eliminate trailing comma
bcc=$(echo $bcc | sed s/,$//)
# Add Bcc header to mail
echo "${mail}" | formail -a "Bcc: $bcc"
If you open the archive with any MUA you can see the bcc rebuilt. NB: script a bit longer but it avoids duplication of the To people in the Bcc list.