问题
I'm trying to forward e-mails matching a certain pattern to a set of addresses, and BCC the same e-mail to some other e-mail addresses. From looking at my procmail log, it appears that all my rules are matching, yet the BCC recipient does not receive the message.
The relevant lines of my .procmailrc look like this:
:0fhw
* ^From.*@example.com
* ! ^X-Loop: test
| formail -A "Bcc: $BCCS"
:0fhw
* ^From.*@example.com
* ! ^X-Loop: test
| formail -A "X-Loop: test"
:0
* ^From.*@example.com
* ! $DEST ?? ^$
! $DEST
At the point this part of the procmailrc is reached, the BCCS
variable contains the address(es) to BCC, and the DEST
variable contains the address(es) to forward to.
In the log I see something like this:
procmail: Match on "^From.*@example.com"
procmail: Match on ! "^X-Loop: test"
procmail: Executing "formail,-A,Bcc: bcctest@somewhere.com"
procmail: Match on "^From.*@example.com"
procmail: Match on ! "^X-Loop: test"
procmail: Executing "formail,-A,X-Loop: test"
procmail: Match on "^From.*@example.com"
procmail: Match on ! "^$"
procmail: Executing "/usr/sbin/sendmail,-oi,user1@somewhere.com,user2@somewhere.com"
procmail: Assigning "LASTFOLDER=/usr/sbin/sendmail -oi user1@somewhere.com,user2@somewhere.com"
procmail: Notified comsat: "michael@:/usr/sbin/sendmail -oi user1@somewhere.com,user2@somewhere.com"
It appears that a Bcc: header is being added and the e-mail forwarded as I expect. My assumption from what I have gathered from my research is that to BCC in the forward I need to add a "Bcc:" header, and sendmail will copy the message to any addresses it specifies and strip the Bcc: header off in the actually sent e-mail. However I am not 100% sure, as all the questions I have found regarding BCC deal with people wanting to trigger on BCC on the incoming message, which can't really be done if the sending server is configured properly.
Am I doing something wrong?
回答1:
This all resolves to a very common FAQ: the headers don't ultimately decide where a message actually gets delivered. Your action
! $DEST
tells Sendmail to send the message to $DEST
and nowhere else.
(You can tell Sendmail to actually examine the recipient headers with sendmail -t
.)
With that understanding, you can actually remove the recipe to add an explicit Bcc:
header, and simply change the last line to
! $DEST $BCCS
(Calling formail
twice was superfluous anyway. It's sometimes useful and necessary, but you can have two -A
options in the same formail
invocation. But adding the Bcc is not useful; Sendmail will strip it off again.)
With Sendmail -t
(which inside Procmail can be used in an action ! -t
) the headers are examined for To:
, Cc:
, Bcc:
etc, and the parsed out addresses are copied to the outgoing message's envelope recipients. Without -t
, the command-line arguments are copied as envelope recipients, and the headers are not consulted. Once the message is inside the envelpe, only the envelope information decides where it goes.
回答2:
formail -A "Bcc: $BCCS"
adds a "Bcc:" header. ! $DEST
forwards the message to "$DEST", however, ! ...
will ignore the "Bcc:" header. ! ...
effectively works like Bcc (except there is no header added and removed). Instead of your 3rd forwarding rule you can use 2 rules, where the 1st works on a copy of the message (note the c flag in the 1st rule):
:0 c
* ^From.*@example.com
* ! $BCC ?? ^$
! $BCC
:0
* ^From.*@example.com
* ! $DEST ?? ^$
! $DEST
An alternative is sendmail -t
. It reads recipients from the mail headers, so it would also see your "Bcc:" header and process the message accordingly. I'd advise against using sendmail -t
though in general unless you have a very controlled environment where you are sure there are no "To:", "CC:" and "BCC:" headers in the messages that you don't want.
Procmail by default uses sendmail
, not sendmail -t
. So, you'd have to pipe the message to sendmail -t
like in
:0
* ^From.*@example.com
* ! $DEST ?? ^$
| sendmail -i -t $DEST
来源:https://stackoverflow.com/questions/50634411/how-to-make-procmail-forward-and-bcc-too