2

I would like to use procmail to forward a message to another email address. Both the headers and body of the incoming message should be in the body of the outgoing message (inline forwarding).

Example incoming message:

From: outside@example.com
To: me@example.com
Subject: Test
Date: Mon, 03 Nov 2014 05:00:04 GMT

This is a test

The forwarded message should be like this:

From: me@example.com
To: thirdparty@example.com
Subject: Fwd: Test
Date: Mon, 03 Nov 2014 05:01:00 GMT

From: outside@example.com
To: me@example.com
Subject: Test
Date: Mon, 03 Nov 2014 05:00:04 GMT

This is a test

Can this be done using procmail, maybe in conjunction with something like formail?

tripleee
  • 175,061
  • 34
  • 275
  • 318
tater
  • 171
  • 8

1 Answers1

2

Easy enough.

:0
* Some conditions, perhaps?  Omit this line to forward unconditionally
* ^Subject:[    ]*\/.*
| (echo From: me@example.com; echo To: thirdparty@example.com; \
   echo "Subject: Fwd: $MATCH"; echo; cat -) | $SENDMAIL -t

If you don't care about forwarding the original Subject header verbatim, this can be simplified additionally.

The -t flag to sendmail says to use whatever To: and Cc: headers are in the message to determine the recipient. I omitted generating a Date: because (most imitations of) Sendmail will do that for you.

The stuff in the square brackets should be one space and one tab, as usual.

If you want to keep a copy, either add Bcc: yourself (and take care to not have the incoming copy trigger a mail loop!) or change :0 to :0c which makes Procmail continue down the rest if the recipe file.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • The subject isn't important, so I can indeed simplify that bit. This works great, thank you. – tater Nov 03 '14 at 15:17
  • how do you add "Bcc:" yourself? – Michael May 31 '18 at 22:47
  • @Michael See now https://stackoverflow.com/questions/50634411/how-to-make-procmail-forward-and-bcc-too though in here, simply `echo "Bcc: one@example.net, two@example.org"` in between the other `echo`s would work fine. – tripleee Jun 01 '18 at 05:01