0

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?

Michael
  • 9,060
  • 14
  • 61
  • 123

2 Answers2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Depending on what you want to accomplish, maybe refactor the last condition to examine `BCCS` as well - do you want to forward if `DEST` *or* `BCCS` is nonempty perhaps? I guess maybe not. – tripleee Jun 01 '18 at 03:36
  • We want to forward it if `DEST` is non-empty. The assumption is that if `DEST` is empty, then `BCCS` will be too, however the opposite is not true. – Michael Jun 01 '18 at 04:46
  • With `! $DEST $BCCS` won't it simply forward to the BCC list but not blindly? (i.e. won't everyone see who was in the BCC list?) – Michael Jun 01 '18 at 04:46
  • No, I'll say this one more time: this bypasses the message headers entirely. The message is passed on as an opaque data blob without modification to the recipients on Sendmail's command line. – tripleee Jun 01 '18 at 04:49
  • (You can even send something which isn't a valid email message, though it will then most probably be severely mangled by the *receiving* MTA.) – tripleee Jun 01 '18 at 04:51
  • Right, I think I understand that if I don't use `-t`, any headers I provide are ignored - so I'm wondering how `! $DEST $BCCS` would even work, as I'm not seeing how it is distinguishing between "To:" and "Bcc:" recipients in that list. In other words, it looks like I *have* to use `-t` as I can't see how appending 1$BCSS` would even work. – Michael Jun 01 '18 at 14:31
  • It doesn't distinguish between them. There is no need to. These recipients will receive the message, regardless of whether their addresses are in the headers at all. Basically, by *definition,* they *all* get `Bcc`:ed. – tripleee Jun 01 '18 at 15:47
  • (If somebody's address is both in `To:` or `Cc:` and `Bcc:` they will still only receive one copy of the message, so this really is exactly like everybody is in `Bcc:`) – tripleee Jun 01 '18 at 15:49
  • But this is tiresome; try it and see. You can create unlimited free public mailboxes for testing at Mailinator. – tripleee Jun 01 '18 at 15:51
1

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
xebeche
  • 905
  • 7
  • 20
  • 2
    [`sendmail` proper](https://linux.die.net/man/8/sendmail.sendmail) apparently behaves like you describe, though it's not clearly documented. The Postfix [`sendmail`](http://www.postfix.org/sendmail.1.html) behavior for `-t` changed in 2.1, apparently for reasons of Sendmail compatibility; older versions did not permit explicit recipients to be passed in as well when `-t` was used. I would be hesitant to rely on any specific behavior in this corner case. – tripleee Jun 01 '18 at 18:20
  • @tripleee Thanks. I did write I discourage its use. – xebeche Jun 01 '18 at 18:52
  • well. i feel like a dummy. of course everything is always BCC, it appears to be going to the original recipient. how could i have missed that important detail? that is the missing piece and why I was confused... :D – Michael Jun 01 '18 at 19:21
  • I guess this comment belongs to the discussion under my answer? If the problem is solved now, please proceed to acoept an answer (write one of your own if the existing ones are not to your liking). – tripleee Jun 02 '18 at 04:43