Bash – postfix/dovecot – run script when mail is received

bashdovecotpostfix

I need to implement solution on my postfix/dovecot server which will execute bash script every time message arrives. For this purpose I did following things in master.cf configuration

myhook        unix  -       n       n       -       -       pipe
    flags=F user=customUser argv=/path/to/script.sh ${sender} ${recipient}

smtp      inet  n       -       -       -       -       smtpd
    -o content_filter=myhook:dummy

Works fine as long as you don't want to emails being delivered to mailboxes via dovecot. The problem is, that somehow I need to 'throw' back everything to dovecot. Or maybe there is a better approach, which is not known by me.

I appreciate every idea or tip in this case.

Best Answer

Your script must act like Simple content filter example from Postfix official documentation. After postfix send the email via pipe, you must resubmit it via sendmail command. Snippet from that page

Postfix receives unfiltered mail from the network with the smtpd(8) server, and delivers unfiltered mail to a content filter with the Postfix pipe(8) delivery agent. The content filter injects filtered mail back into Postfix with the Postfix sendmail(1) command, so that Postfix can deliver it to the final destination.