大网数据广告活动
服务器运维问答

Postfix archive preserving Bcc with X-Envelope-To - cleanup confidential X-Envelope-To with Lmtp

· 52CE 我爱测

Bcc'ing the [email omitted] user, we do not want people to receive that Bcc list, apart from the [email omitted] "user". Goal We want to archive all mails going through postfix, sent and received. The setup uses: postfix, dovecot…

问题原文

原帖链接 · user3450564 · CC BY-SA 4.0

Bcc'ing the [email omitted] user, we do not want people to receive that Bcc list, apart from the [email omitted] "user".

Goal

  1. We want to archive all mails going through postfix, sent and received. The setup uses:
  • postfix,
  • dovecot authentication with Pam (pulling from Ldap) users,
  • and delivery to the mailboxes via Dovecot's LMTP.
  1. We want to include the Bcc recipients in the message archive.

  2. We do not want people to receive that Bcc list, apart from the [email omitted] "user".

Trial

  1. Using always_bcc = [email omitted] Works. But the Bcc recipiets (which as per rights are in the envelope as RCPT TO and not in a header) get lost.

  2. Using: (as per What options are there to archive Postfix email and preserve any BCC information in the header)

main.cf
smtpd_recipient_restrictions =
  check_recipient_access pcre:/etc/postfix/prepend.pcre

while the pcre expression is:

prepend.pcre
/(.+)/          PREPEND X-Envelope-To: $1

which works like a charm: my message stored has the headers To:; Cc; X-Envelope-To. If any day we need to dig into it, we can know who received the message even in Bcc. (NB: the X-Envelope-To includes the To and Cc people, pas well as the external domains recipients).

  1. To eliminate the X-Envelope-To: For sending out:
smtp_header_checks = pcre:/etc/postfix/smtp_header_checks.pcre

With the contents:

smtp_header_checks.pcre
/^X-Envelope-To:/               IGNORE

All ok until there.

Problem

To eliminate the X-Envelope-To for emails not leaving the server. I did:

lmtp_header_checks = pcre:/etc/postfix/lmtp_header_checks.pcre

lmtp_header_checks.pcre
/^X-Envelope-To:/               IGNORE

But this is not selective, even [email omitted] looses the header that was "saved" the Bcc list.

I cannot find a way to the that header check selective since it only evaluates the line itself and cannot use the recipient to make a condition (even using the "if" of the pcre). (http://www.postfix.org/pcre_table.5.html)

Is there any variable that can be used ? A thought was to: include "[email omitted]" in the X-Envelope-To header prepended, in the lmtp_header_checks.pcre, include something like: [email omitted] not equal to $LMTP_DELIVERY_RECIPIENT Delivered-To or X-Delivered-To remain a separate header that the pcre cannot evaluate as a variable.

Otherwise is there a way to have lmtp trigger some milter ? And a simple lmtp_milter filter I can use for that ?

Been chasing for days in all docs possible, Thanks in advance

已采纳解答

原帖链接 · user3450564 · CC BY-SA 4.0

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 omitted]" {
   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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-09-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。