Postfix - Pipe multiple recipient message as a single message

I am trying to pipe emails through Postfix to a Python script I have. Currently, messages with multiple recipients are piped to the script once for each recipient. I would like these messages to be piped to the script just once regardless of the number of recipients.

In /etc/postfix/main.cf I have:

default_transport = customsmtp

And that transport method is defined in /etc/postfix/master.cf as:

customsmtp  unix  -      n      n     -     -     pipe
    flags=FR  user=cody argv=/var/relay/custom-relay/endpoint.py type:relay env:production sender:${sender} recipient:${recipient}

Here is an excerpt from the Postfix mail logs showing the multiple deliveries to the script:

Mar 19 20:26:29 ip-172-31-2-6 postfix/cleanup[18639]: 2ACDD24199: message-id=<[email protected]>
Mar 19 20:26:29 ip-172-31-2-6 postfix/qmgr[29229]: 2ACDD24199: from=<[email protected]>, size=2715, nrcpt=2 (queue active)
Mar 19 20:26:30 ip-172-31-2-6 postfix/pipe[18640]: 2ACDD24199: to=<[email protected]>, relay=customsmtp, delay=0.89, delays=0.2/0.01/0/0.67, dsn=2.0.0, status=sent (delivered via customsmtp service)
Mar 19 20:26:30 ip-172-31-2-6 postfix/pipe[18641]: 2ACDD24199: to=<[email protected]>, relay=customsmtp, delay=0.9, delays=0.2/0.04/0/0.66, dsn=2.0.0, status=sent (delivered via customsmtp service)
Mar 19 20:26:30 ip-172-31-2-6 postfix/qmgr[29229]: 2ACDD24199: removed

As someone relatively new to Postfix I'm not sure what other settings need to be configured to allow this to happen. What other configuration settings should I be looking to modify or if there is any more information I can provide to get help with this issue?

EDIT

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
broken_sasl_auth_clients = yes
command_directory = /usr/sbin
config_directory = /etc/postfix
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
debug_peer_level = 2
default_transport = customsmtp
html_directory = no
inet_interfaces = all
inet_protocols = all
mail_owner = postfix
mailq_path = /usr/bin/mailq.postfix
manpage_directory = /usr/share/man
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mydomain = cody.example.com
myhostname = cody.example.com
myorigin = $mydomain
newaliases_path = /usr/bin/newaliases.postfix
queue_directory = /var/spool/postfix
readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES
sample_directory = /usr/share/doc/postfix-2.6.6/samples
sendmail_path = /usr/sbin/sendmail.postfix
setgid_group = postdrop
smtpd_helo_required = yes
smtpd_helo_restrictions = reject_invalid_helo_hostname
smtpd_recipient_restrictions = permit_sasl_authenticated, reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_authenticated_header = yes
smtpd_sasl_path = smtpd
smtpd_sasl_security_options = noanonymous
smtpd_sasl_type = cyrus
smtpd_tls_CAfile = /etc/path/to/crt.crt
smtpd_tls_auth_only = yes
smtpd_tls_cert_file = /etc/path/to/crt2.crt
smtpd_tls_key_file = /etc/path/to/key.key
smtpd_tls_loglevel = 1
smtpd_tls_security_level = encrypt
smtpd_tls_session_cache_database = btree:/var/spool/postfix/smtpd_tls_cache
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom
unknown_local_recipient_reject_code = 550

Solution 1:

Add these parameters in main.cf

virtual_alias_maps = pcre:/etc/postfix/deduplicate
enable_original_recipient = no

Then in deduplicate file, put

/.*/   [email protected]

Don't forget to execute postfix reload

Explanation:

Parameter enable_original_recipient will prevent duplication when aliasing to dummy @gmail.com. After aliasing, postfix will pipe the email to customsmtp

Caveat:

Parameter recipient in your script will be replaced with [email protected]. So you will lose original recipients

Solution 2:

The following configuration seems to work for your purpose:

main.cf: (default in ubuntu except for mydestination, default_transport and local_transport)

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

readme_directory = no

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = vagrant-ubuntu-trusty-64
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = vagrant-ubuntu-trusty-64, localhost.localdomain, , localhost, example.org
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
default_transport=test
local_transport=test

master.cf:

test    unix  -    n    n    -    -   pipe
    user=nobody argv=/usr/local/bin/test ${sender} ${recipient}

/usr/local/bin/test:

#!/bin/sh
logfile="/tmp/postfix_transport.log"
content=`cat`
date -R >> "$logfile"
echo "$@" >> "$logfile"


Sending an email using sendmail -f [email protected] [email protected] [email protected] < mail generates the following message in /tmp/postfix_transport.log:

Sun, 03 May 2015 19:19:47 +0000
[email protected] [email protected] [email protected]