You can set up a post queue filter. How are your perl/sed skills?
The first thing you need to do is write a script that will accept a raw message on STDIN
, and modify the message as you'd like (I'll assume that you have this capability already). You then configure postfix to pass all messages to that script as a filter. As the last thing the script does, pass the message back to postfix.
For writing the script, I suggest using perl. It has a very powerful regexp engine. You can also use Net::SMTP
to make resubmission back to postfix easy.
Here's how to configure postfix to do this:
In master.cf
on your smtpd
line, change the last part to include the filter.
Original line:
smtp inet n - n - - smtpd
New line:
smtp inet n - n - - smtpd -o content_filter=myfilter
Add your filter service to master.cf
:
myfilter unix - n n - 3 pipe
flags= user=nobody argv=/usr/local/bin/myfilter.pl localhost 25252 $sender $recipient
Note that the sender and recipient here are from the envelope not the body. You'll need this to be part of the envelope when passing the message back to postfix.
Finally add an unfiltered smtp receiver to master.cf
to accept the newly modified messages:
local:25252 inet n - n - - smtpd
-o content_filter=