Postfix routing emails using LDAP or regexp with different smptd processes

If you want to redirect emails you can use the recipient_canonical_maps option.

This cleanup option is usually specified globally in main.cf.

If you want to treat different domains with different filtering techniques, you can do this in master.cf instead:

# TCP/2525 : routing @ldap.example.org based on LDAP filter
0.0.0.0:2525       inet  n       -       n       -       -       smtpd
        -o cleanup_service_name=cleanup-2525
        -o myhostname=mxrouting.ldap.example.org
        -o smtpd_banner=$myhostname

cleanup-2525 unix  n       -       n       -       0       cleanup
        -o recipient_canonical_maps=ldap:/etc/postfix/routing-ldap.example.org

# TCP/2526 : routing @regexp.example.org based on regexp filter
0.0.0.0:2526       inet  n       -       n       -       -       smtpd
        -o cleanup_service_name=cleanup-2526
        -o myhostname=mxrouting.regexp.example.org
        -o smtpd_banner=$myhostname

cleanup-2526 unix  n       -       n       -       0       cleanup
        -o recipient_canonical_maps=regexp:/etc/postfix/routing-regexp.example.org

This means that Postfix will listen on port TCP/2525 for emails to @ldap.example.org, and apply an LDAP query to know where to route the email.

On port TCP/2526, emails for @regexp.example.org will be redirected based on regexp rules.

Examples or recipient_canonical_maps configurations:

/etc/postfix/routing-ldap.example.org:

server_host = 192.168.0.1
search_base = ou=company,ou=name,o=prd
bind_dn = cn=blah,ou=TechObjects,o=SYSTEM
bind_pw = MyVerySecurePassword

# %s = input --> this is the email address
query_filter = (&(mail=%s))
result_attribute = mailstop
result_format = %U@%s.%D

/etc/postfix/routing-regexp.example.org:

# Adresses starting with support go to server1
/^support(.*@)regexp.example.org$/ ec${1}server1.regexp.example.org
# Adresses starting with business go to server2
/^business(.*@)regexp.example.org$/ ms${1}server2.regexp.example.org