Actually, Instyle's answer is very difficult to implement if you want to support many different domains and it is wrong because:
a) With his example of transport_maps
, all the emails sent to that domain are sent to that specific service without any regard to whether the emails are bounced emails or not. Since it uses a specific domain name, it should indeed only be bounced emails... but it cannot be guaranteed that way.
b) The data sent to your script is the email itself and not the bounce message. In other words, your code may have no idea why the email was bounced (i.e. local bounce will send you the original email only.)
The correct way to do that setup in postfix is to use the bounce notification class.
1) In /etc/postfix/main.cf
notify_classes = bounce
bounce_notice_recipient = bounces@example.com
transport_maps = hash:/etc/postfix/transport_maps
2) In /etc/postfix/transport_maps
# when you make changes to this file, run:
# sudo postmap /etc/postfix/transport_maps
bounces@example.com bulkbounce:
As you can see, we now tell postfix to use bounces@example.com
whenever an email gets bounced. Then in the transport map, to use bulkbounce
as the service to handle any email address to bounces@example.com
.
Finally you can define bulkbounce
with your script:
3) In /etc/postfix/master.cf
bulkbounce unix - n n - - pipe
flags=FRq user=bounce argv=/home/bounce/bin/snapbounce --sender ${sender} --recipient ${recipient}
This script requires you to have a user. nobody
is a good choice too. If you want to have a specific user, you can create it with:
useradd bounce
Without the script in master.cf
, the emails are sent to the bulkbounce account. So if you have a script that parses emails from files, this would work without the transport_maps
and master.cf
changes.
From a comment below:
fyi - re: double bounces...
if you're modifying the return address (VERP address such as user+id@fromdomain.com
, then you will want to comment out the line in main.cf
for the bounce_notice_recipient
, if you're interested in parsing the +id
bounce only in your script.