Skip to content

HOW TO Handle bounce emails in Postfix with script

April 7, 2012

…continue from HOWTO: Install and Configure Postfix + DKIM in FreeBSD jail for LARGE SENDING

Handle all bounce emails for domain: domain.com , run shell/perl/php/etc. script with sensitive info.

1. Create transport map in main.cf , and postmap the map

# tail -1 /usr/local/etc/postfix/main.cf

transport_maps = hash:/usr/local/etc/postfix/transport.maps

# cat /usr/local/etc/postfix/transport.maps

xssoftgames.com bounces:

# postmap /usr/local/etc/postfix/transport.maps

2. Create user for executing the script (can not be root, can not be postfix master user! ) – no shell, nologin

# useradd bounce

3. Create pipe service in master.cf (pipe service name: bounces , as it’s bounces: in the transport map). Simple usage.

# tail -2 /usr/local/etc/postfix/master.cf

bounces unix – n n – – pipe
flags=FRq user=bounce argv=/usr/local/etc/postfix/bounces.sh ${sender} ${recipient}

4. create /usr/local/etc/postfix/bounces.sh script

# cat /usr/local/etc/postfix/bounces.sh

#!/bin/sh
echo “$1 $2” > /tmp/kur

5. reload Postfix

# postfix reload

THats all.

Extending

More arguments to argv for better bounce service servicing

 ${client_address}
                     This macro expands to the remote client net-
                     work address.

                     This feature is available as of Postfix 2.2.

              ${client_helo}
                     This macro expands to the remote client HELO
                     command parameter.

                     This feature is available as of Postfix 2.2.

              ${client_hostname}
                     This  macro  expands  to  the  remote client
                     hostname.

                     This feature is available as of Postfix 2.2.

              ${client_port}
                     This  macro expands to the remote client TCP
                     port number.

                     This feature is available as of Postfix 2.5.

              ${client_protocol}
                     This macro expands to the remote client pro-
                     tocol.

                     This feature is available as of Postfix 2.2.

              ${domain}
                     This  macro expands to the domain portion of
                     the recipient address.  For example, with an
                     address   user+foo@domain   the   domain  is
                     domain.

                     This information is modified by the  h  flag
                     for case folding.

                     This feature is available as of Postfix 2.5.

              ${extension}
                     This macro expands to the extension part  of
                     a  recipient  address.  For example, with an
                     address  user+foo@domain  the  extension  is
                     foo.

                     A   command-line   argument   that  contains
                     ${extension} expands into as  many  command-
                     line arguments as there are recipients.

                     This  information  is modified by the u flag
                     for case folding.

              ${mailbox}
                     This macro expands  to  the  complete  local
                     part  of  a recipient address.  For example,
                     with an address user+foo@domain the  mailbox
                     is user+foo.

                     A   command-line   argument   that  contains
                     ${mailbox} expands to as  many  command-line
                     arguments as there are recipients.

                     This  information  is modified by the u flag
                     for case folding.

              ${nexthop}
                     This macro expands to the next-hop hostname.

                     This  information  is modified by the h flag
                     for case folding.

              ${original_recipient}
                     This macro expands to the complete recipient
                     address  before  any  address  rewriting  or
                     aliasing.

                     A  command-line   argument   that   contains
                     ${original_recipient}  expands  to  as  many
                     command-line arguments as there are  recipi-
                     ents.

                     This  information  is  modified  by  the hqu
                     flags for quoting and case folding.

                     This feature is available as of Postfix 2.5.

              ${recipient}
                     This macro expands to the complete recipient
                     address.

                     A  command-line   argument   that   contains
                     ${recipient} expands to as many command-line
                     arguments as there are recipients.

                     This information  is  modified  by  the  hqu
                     flags for quoting and case folding.

              ${sasl_method}
                     This  macro  expands to the name of the SASL
                     authentication mechanism in the AUTH command
                     when  the  Postfix  SMTP server received the
                     message.

                     This feature is available as of Postfix 2.2.

              ${sasl_sender}
                     This  macro  expands to the SASL sender name
                     (i.e. the  original  submitter  as  per  RFC
                     4954)  in  the  MAIL  FROM  command when the
                     Postfix SMTP server received the message.

                     This feature is available as of Postfix 2.2.

              ${sasl_username}
                     This  macro expands to the SASL user name in
                     the  AUTH  command  when  the  Postfix  SMTP
                     server received the message.

                     This feature is available as of Postfix 2.2.

              ${sender}
                     This macro expands to  the  envelope  sender
                     address. By default, the null sender address
                     expands  to  MAILER-DAEMON;  this   can   be
                     changed  with  the null_sender attribute, as
                     described above.

                     This information is modified by the  q  flag
                     for quoting.

              ${size}
                     This  macro expands to Postfix's idea of the
                     message size, which is an  approximation  of
                     the size of the message as delivered.

              ${user}
                     This macro expands to the username part of a
                     recipient address.   For  example,  with  an
                     address user+foo@domain the username part is
                     user.

                     A  command-line   argument   that   contains
                     ${user}  expands  into  as many command-line
                     arguments as there are recipients.

                     This information is modified by the  u  flag
                     for case folding.
Original at: http://www.postfix.org/pipe.8.html
Advertisements
7 Comments
  1. hi dude.. thanx for your code.. it was working fine.. but your code is blocking the entire domain..but my scenario is to call the bounces.sh only when the email id is not a valid one..so it would be greatfull if u help me with what changes should i do to make it work for my scenario. awaiting for your response

    • of course I can help you, just please explain your case in more details

  2. Mickael permalink

    Very interesting! But $ {sender} is useless always returns “MAILER-DAEMON” …

    • no, there is a mistake.
      sender is the sender, if your mailer-daemon is sender then yes 🙂

  3. As Gopi mentioned, a bounced email is not the same as just an email being forwarded to somehow on the same domain. So if I email user X@example.com from the mail server example.com, the transport.maps you offer will redirect that perfectly valid email to the bounce script…

    The transport.maps is used to redirect all local emails, not just bouncing emails. One way is to setup the return email address to a special address such as bounces.example.com and then redirect only those emails in your transport.maps file.

    bounces.example.com bounces:

    This means you define the From in your email such as unwanted@bounces.example.com (WARNING: this is the From at the very beginning of the email, not the “From:” header. It is very important since the From: header is likely going to be used to send you a reply…)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: