postfix: redirect emails to script

In this example, we will configure postfix to redirect any email sent to * to a php script

  1. Update /etc/postfix/ (don’t forget to create a backup copy) to create a new transport instruction:/etc/postfix/
    transport_maps = hash:/etc/postfix/<mytransport>

    Note: replace <mytransport> with any name you want.

  2. Create corresponding transport file:/etc/postfix/<mytransport>:  myPipeName:

    Note: don’t forget to change ‘’ to match your own config, and replace ‘myPipeName’ with any string you want (ensure it is not defined elsewhere)

  3. Update (don’t forget to create a backup copy before) to pipe email through your script:
    myPipeName unix - n n - - pipe flags=O user=www-data:www-data argv=/usr/bin/php -c /etc/php-cli.ini -q path/to/my/script.php ${recipient}

    Note1: update previous command to match your pipe name and the path to your php script
    Note2: don’t hesitate to customize the user:group being used, set to www-data in this example, but could be set to less generic user to ensure execution is not triggered from the web

  4. generate a db file readable by posfix.
    When reloading postfix, looking at /var/log/mail.log I noticed the following error: 

    postfix/trivial-rewrite[32101]: fatal: open database /etc/postfix/rdTransport.db: Invalid argument

    After researching the web, I found that you need to run:

    postmap /etc/postfix/<mystransport>

    so that a postfix-readable .db binary file gets generated.
    Note: run ls -l /etc/postifx/<mystransport>* and check that /etc/postfix/<mytransport>.db file has been created

  5. reload postfix
    postfix reload


One Comment Trackback URL | Comments RSS

  1. Sep Says:

    Would this work for python Scripts? where argv would point to python and the python scripts

Post a Comment

Your email is never published nor shared. You're allow to say what you want...