Skip to content

How to Bind Different Sending Domains to Multiple IPs on a Single Server

Step 1

Set the outgoing IPs for internal network segments (172.66.1.0/24, 172.66.2.0/24, ..., 172.66.255.0/24) using iptables SNAT rules.

Example

shell
iptables -t nat -D POSTROUTING -s 172.66.2.0/24 -j SNAT --to-source YOUR_SERVER_IP

Step 2

Add new networks to docker-compose.yml and configure multiple static IPs for the Postfix service.

Example

yaml
services:
  postfix-billionmail:
    networks:
      billionmail-network:
        ipv4_address: 172.66.1.100
        aliases:
          - postfix
      billionmail-network-2:
        ipv4_address: 172.66.2.100
        aliases:
          - postfix-2

networks:
  billionmail-network:
    driver: bridge
    driver_opts:
      com.docker.network.bridge.name: br-billionmail
    ipam:
      driver: default
      config:
        - subnet: 172.66.1.0/24

  billionmail-network-2:
    driver: bridge
    driver_opts:
      com.docker.network.bridge.name: br-billionmail-2
    ipam:
      driver: default
      config:
        - subnet: 172.66.2.0/24

Step 3

Configure Postfix's main.cf and master.cf to add new SMTP services and bind them to sending domains and static IPs. Remember to set the default static IP.

Example

ini
# main.cf
smtp_bind_address = 172.66.1.100
ini
# master.cf

# Add the following configurations to the end of the file.
smtp1     unix  -       -       n       -       -       smtp
    -o smtp_bind_address=172.66.2.100
    -o smtp_helo_name=mail.example.com

# ...

Step 4

Configure sender_dependent_default_transport_maps in main.cf and write the rules. Note the rule format!

Example

ini
# main.cf
sender_dependent_default_transport_maps = hash:/etc/postfix/conf/sender_transport
ini
# sender_transport
@example.com smtp1:
shell
# Don’t forget to run postmap inside the Postfix container
postmap /etc/postfix/conf/sender_transport

All Done

Simply restart the Postfix container to apply all configurations.

Important Notes

This configuration is temporary. If docker compose down and docker compose up -d are executed (rebuilding network containers), iptables rule priorities may change, causing the configuration to fail.

Recommendations:

  • Re-add iptables rules after updating or rebuilding with docker-compose.

  • Delete old rules before re-adding new ones!

Released under the AGPLv3 License