I have setup a postfix server that will receive several emails and deliver them to OpenERP. It's currently working, but I would like to improve the overall schema so that postfix delivers (i.e Delivered-To) to the OpenERP email alias.

More details about the instalation

The big schema is the following:

  1. Installed postfix-pgsql. In my main.cf file I have something like:

    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    myorigin = /etc/mailname
    mydestination = localhost
    virtual_alias_domains = mydomain.com
    virtual_alias_maps = pgsql:/etc/postfix/pgsql-aliases.cf
  2. The query in pgsql-aliases.cf

    query = SELECT 'oerp@localhost'
            FROM mail_alias a, ir_config_parameter d
            WHERE (d.key = 'mail.catchall.domain')
                  AND (d.value = '%d')
                  AND (a.alias_name = '%u');
  3. Then I have the "oerp" alias in /etc/aliases:

    oerp: "| openerp_mailgate ...args... "

The issue

This setup works fairly well. Mails are being delivered, etc. But then, I have an out-of-my-control MTA (my postfix can relay to that for outsiders) that relays emails coming to "mydomain.com" to my postfix.

In that MTA there are several rules. For instance, I you email to "contact@external-domain.com", the MTA will re-deliver the email to "contact@mydomain.com" and "boss@external-domain.com". That redelivery won't touch original headers.

The "contact@mydomain.com" will handed to my postfix and, ultimately, it will end up being piped to the mailgate script. But the "Delivered-To" header will be changed to "oerp@localhost".

OpenERP looks for the "Delivered-To" header (among others) of the message to know what to do with it. But in this case the "Delivered-To" will always be the same and thus we can't use it for anything important.

I would like to change the schema so that postfix will deliver the email to my mailgate script without resorting to "oerp@localhost" alias and the "Delivered-To" remains unchanged (in the example it is expected to be "contact@mydomain.com").

Any ideas? I've been playing with the master.cf but failed to accomplish anything.

  • I've just came across the postfix-to-mailmain.py script that have some very useful tips for my situation. I will try it and report. – manu Sep 11 '14 at 0:03

Summary of the solution I found:

  1. Remove the alias that matches 'oerp@localhost' with the script.

  2. Place the script as a pipe in master.cf.

  3. Create a transport map that matches your OpenERP domain to be delivered to the script previously defined in master.cf.

  4. Use the pg_aliases.cf for a virtual mailbox map instead for alias. Then remove the oerp alias in /etc/aliases.

Long answer

The previous solution simply gave the same alias for every match resulting in the unreliability of the 'Delivered-To'. Now what I have done is to recast the entire solution and use a virtual mailbox instead, like:

virtual_mailbox_domains = mydomain.com
virtual_mailbox_maps = pgsql:/etc/postfix/pg-aliases.cf

This take another view to the problem, and IMO is more correct: Instead giving the same "aliased" mailbox to each message, simply find a positive virtual mailbox.

The pg-aliases.cf was modified (though this is not strictly need I find it more convenient for logging purposes) to:

query = SELECT '%u@%d'
      FROM mail_alias a, ir_config_parameter d
      WHERE (d.key = 'mail.catchall.domain')
            AND (d.value = '%d')
            AND (a.alias_name = '%u');

Now you need to define a transport that actually delivers the incoming emails to the mailgate script. This is done in two steps:

First, configure the mailgate script in the master.cf file, like this:

openerp   unix  -       n       n       -       1       pipe
   flags=DFR user=openerp argv=/path/to/mailgate.py -d dbname ... etc ..

Notice that 1 as the limit. This is done so that concurrent access to OpenERP server is not possible otherwise messages coming to several @mydomain.com address may double in the DB because of transactional isolation.

The write a simple /etc/postfix/transports file:

mydomain.com  openerp

And instruct main.cf to read this transport file:

transport_maps = hash:/etc/postfix/transports

You may forget about the transport map use virtual_transport=openerp if the only virtual domain you have is this one. However, you may find useful to have other internal domains for internal messages, etc.

  • Anyway, looks like you have missing : character after openerp string in /etc/postfix/transports file – masegaloeh Feb 11 '15 at 2:06
  • Is not missing. See postfix.org/postmap.1.html – manu Feb 11 '15 at 13:42
  • Umm, the format of transport_table was defined in postfix.org/transport.5.html , especially section RESULT FORMAT. Or maybe postfix silently ignored the missing : :) – masegaloeh Feb 11 '15 at 13:49
  • Ah! You referred to the result, I misread you thinking about a colon separating the pattern from the result. The colon seems to be optional when the nexthop is null, though is not documented. I tested the trailing colon in my workstation and everything kept working. – manu Feb 11 '15 at 14:31

One alternative solution: You can turn off the Delivery-To header when pipe to command by using parameter prepend_delivered_header. When delivery-To header was missing, OpenERP will looks into To header.

Add this line to main.cf

prepend_delivered_header = file, forwarding

This will disabled Delivery-To being added when deliver via command.

The docs of postfix said below about prepend_delivered_header

prepend_delivered_header (default: command, file, forward)

The message delivery contexts where the Postfix local(8) delivery agent prepends a Delivered-To: message header with the address that the mail was delivered to. This information is used for mail delivery loop detection.

By default, the Postfix local delivery agent prepends a Delivered-To: header when forwarding mail and when delivering to file (mailbox) and command. Turning off the Delivered-To: header when forwarding mail is not recommended.

Specify zero or more of forward, file, or command.

  • Hi @masegaloeh, I've actually solve this issue long ago, but at the time I got stuck in other issues, that afterwards I solved as well, so I forgot to answer my own question. – manu Feb 10 '15 at 13:39
  • Hi, would you like to share the solution? You can put it in answer section below and don't forget to accept the answer :) – masegaloeh Feb 10 '15 at 13:51
  • Yes I will. It's rather lengthy and since it's for a very specific problem I will point to several specific tools. – manu Feb 10 '15 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.