5

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=<000a01d06282$fd7c59c0$f8750d40$@bananatag.com>
Mar 19 20:26:29 ip-172-31-2-6 postfix/qmgr[29229]: 2ACDD24199: from=<cody@email.com>, size=2715, nrcpt=2 (queue active)
Mar 19 20:26:30 ip-172-31-2-6 postfix/pipe[18640]: 2ACDD24199: to=<cody@example.com>, 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=<cody@testing.com>, 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
ctp_9
  • 151
  • 1
  • 4

3 Answers3

2

Add these parameters in main.cf

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

Then in deduplicate file, put

/.*/   dummy@gmail.com

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 dummy@gmail.com. So you will lose original recipients

masegaloeh
  • 18,236
  • 10
  • 57
  • 106
  • Is it possible to prevent Postfix from splitting the original message rather than removing duplicates? I'd really like to preserve the original recipients. – ctp_9 Mar 23 '15 at 16:50
  • What do you mean by *prevent Postfix from splitting the original message rather than removing duplicates*? Can you give an example? – masegaloeh Mar 23 '15 at 16:52
  • My understanding of Postfix is that messages regardless of the number of recipients enter Postfix's incoming queue as a single message. Then the Postfix qmgr sends delivery requests to the pipe delivery agent. Rather than sending one delivery request per recipient, I would like to send one delivery request to the pipe delivery agent per incoming message. – ctp_9 Mar 23 '15 at 17:05
  • Hmmm, can you edit the question and add the output of `postconf -n`? – masegaloeh Mar 24 '15 at 10:11
  • I've edited the original post to include the configuration settings. – ctp_9 Mar 24 '15 at 23:50
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 user@user.com test@example.org root@example.org < mail generates the following message in /tmp/postfix_transport.log:

Sun, 03 May 2015 19:19:47 +0000
user@user.com root@example.org test@example.org
Zulakis
  • 4,153
  • 14
  • 48
  • 76
  • I tried `local_delivery = mysmtp` but failed to trigger pipe once for all recipients. Other noted by you configurations are default. What else may I do? –  Sep 22 '16 at 13:06
-2

https://github.com/ureyni/postfix.3.1 may be a solution to your question

ureyni
  • 1
  • 2