postfix: redirect emails to script

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

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

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

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

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

  3. Update master.cf (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

sources

Post a Comment

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

Powered by sweetCaptcha