-1

Debian Sid, latest postfix from Sid.

I need to invoke bash script after user reveive mail. So, what I did:

  • create file /etc/postfix/transport, for example: mail@domain.com myscript

  • run command to create database: postmap transport

  • add to main.cf: transport_maps = hash:/etc/postfix/transport

  • add to master.cf: myscript unix - n n - - pipe user=michal flags=FR argv=/home/michal/test.sh

  • reload postfix

What's the problem? If I configure it this way, after mail is received, script "test.sh" will be executed, but incoming mail will not be delivered to mailbox and it will be deleted immediatelly after receiving.

So - how to avoid this? I need the script to be executed, but incoming mail should be also delivered to my mailbox.

tripleee
  • 175,061
  • 34
  • 275
  • 318
user1209216
  • 7,404
  • 12
  • 60
  • 123

1 Answers1

1

Use Procmail.

:0c
| $HOME/test.sh

The script receives the full message on standard input, but if you don't feel like parsing the message yourself, there are standard techniques for extracting header values into Procmail variables. You can pipe to formail:

SUBJECT=`formail -zcxSubject:`

or you can grab into MATCH, which avoids spawning an external process, but is a bit trickier for more-complex tasks;

:0
* ^Subject:[  ]*\/.+
{ SUBJECT=$MATCH }

(the whitespace inside [ ] should be a space and a tab); either way, you can now pass in $SUBJECT as a parameter on the test.sh command line. Obviously, other header values can be extracted into variables in a similar way.

PS. You cannot inline the formail call like this because it will consume the standard input from the pipe.

:0c
| $HOME/test.sh "`formail -zcxSubject:`"   # erroneous!

Instead, you need to split it up, like this:

:0
* ^Subject:[  ]*\/.+
{ SUBJECT=$MATCH }
:0c
| $HOME/test.sh "$SUBJECT"
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • It works! I also need to pass to this sript parameters: mail sender, date received and mail subject. How to modify this file to get this? – user1209216 Jul 28 '12 at 16:01
  • I reverted your edit but answered your question in an update to the answer. Please don't change the question significantly after you have posted it. – tripleee Jul 29 '12 at 17:19