Thursday, October 21, 2010

Redirecting emails to a PIPE in Zimbra

Sometimes there is a need to redirect/forward incoming emails to STDIN of a script/program or in other words redirect emails to a pipe. Although such program can connect to email server using POP or IMAP to process the emails, a redirect through pipe appears to be much more efficient. Such program can also have much simpler logic as it just needs to deal with the incoming emails and not to worry about differentiation between the processed and unprocessed ones.

I had a need to set such redirect of emails to a PHP script for one of our projects. Zimbra server does not support this through its GUI, so here are the steps I did to make it happen:

  1. Email accounts that need to redirect the incoming emails to the pipe should forward a copy (or redirect without leaving local copy, depends on the need) of an email to the address of a special account. This special account will be configured to redirect any incoming emails to the pipe.If the email server name is “mymailserver.my.domain.com”, I will create a new local account:  my_pipe_acct@mymailserver.my.domain.com
  2.  In the forwarding options of the account above I’ll forward all messages to the following dummy address. It can be anything, even not a valid address:                                      my_pipe_acct@my_pipe.mymailserver.my.domain.com
  3.  New service "my_pipe" will need to be set in master.cf file and will define pipe to “script.php” script that will process the emails. It actually says that if email needs to be processed with service “my_pipe”, then “script.php” is the service to invoke.
    Add this to /opt/zimbra/postfix/conf/master.cf.in file:
    my_pipe     unix    -       n       n       -       -       pipe
        flags= user=zimbra argv=/path/to/pipe/script/script.php

    and run # su – zimbra –c "zmmtactl stop && zmmtactl start"
    The lines should be added before # AMAVISD-NEW line. Note that the 2nd line should start with space. Don't add directly to master.cf file as it will be overwritten by Zimbra restarts. Edit master.cf.in!!
  4.  Now we need to define that any messages arriving to this dummy address (or actually any address of a type  <XYZ>@my_pipe. mymailserver.my.domain.com ) will be redirected to "my_pipe" service for processing. This is done in /opt/zimbra/postfix/conf/transport.
    Add this line there:
    my_pipe.mymailserver.my.domain.com           my_pipe
    Run #/opt/zimbra/postfix/sbin/postmap /opt/zimbra/postfix/conf/transport.
  5. To configure zimbra to check the transport_maps, the following line in /opt/zimbra/postfix/conf/main.cf should look like:
         transport_maps = hash:/opt/zimbra/postfix/conf/transport,proxy:ldap:/opt/zimbra/conf/ldap-transport.cf
         NOTE: Don't update main.cf directly as it will be overwritten by Zimbra restarts.
         Instead, run the following:
        # su – zimbra
        # zmlocalconfig -e postfix_transport_maps="hash:/opt/zimbra/postfix/conf/transport,proxy:ldap:/opt/zimbra/conf/ldap-transport.cf
"
    This will permanently add the line to the main.cf.

   To enable:
        #/opt/zimbra/postfix/sbin/postfix reload
        OR
        # su – zimbra –c "zmcontrol stop"
        # su – zimbra –c "zmcontrol start"



Obviously it is possible to configure more than one pipe on a server. Just repeat the same steps above to set another special account, which will forward to yet another dummy address, which will be redirecting to another pipe service, etc.

1 comment: