Inbound SMTP routing with postfix

Hi,

Recently, I needed to route incoming emails to different SMTP servers for a customer. Specifically, I had to route various incoming emails to different SMTP servers, each managing their own domains, all behind one public IP address.

For example, I needed to route emails addressed to @mybeautifulcompany.com to server A and emails addressed to @othercompany.com to server B.

How can this be achieved with Postfix?

1/Install postfix, I won't go into details here 2/Create or import certificate for your postfix server 3/Edit the postfix main conf

nano /etc/postfix/main.cf

4/ the file looks like this :

smtpd_tls_cert_file = /etc/ssl/certs/postfix.crt
smtpd_tls_key_file = /etc/ssl/private/postfix.key
smtpd_tls_security_level = may
smtpd_tls_loglevel = 1
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_tls_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1
smtpd_tls_ciphers = high
smtpd_tls_exclude_ciphers = aNULL, eNULL, EXPORT, DES, RC4, MD5, PSK, aECDH, EDH-DSS-DES-CBC3-SHA, EDH-RSA-DES-CDB3-SHA, KRB5-DES, CBC3-SHA
smtpd_tls_received_header = yes
smtpd_tls_auth_only = yes
transport_maps = hash:/etc/postfix/transport
smtpd_recipient_restrictions =
    permit_sasl_authenticated
    permit_mynetworks
    permit_auth_destination
    reject_unauth_destination

mynetworks = 127.0.0.0/8

relay_domains = yourdomaina.com, yourotherdomain.com, etc...

5/Create a transport conf file :

nano /etc/postfix/transport

In this file specify your different smtp servers for your different domain where postfix will relay your emails like this :

   domainA.com   smtp:[10.0.0.1]
   domainB.com   smtp:[10.0.0.2]

6/ update the postfix transport database :

postmap /etc/postfix/transport

Works like a charm!

To add resilience, I created a Postfix cluster using Keepalived to handle failures.

Incoming traffic on port 80 is NATed to a virtual IP shared by two different Postfix instances. If the master fails, the backup takes over seamlessly.


Related posts

Published by

valent1ad

valent1ad

Just another HTMLy user