Shami's Blog

Sysadmin, Because Even Developers Need Heroes

Randomize Source IP Addresses With Postfix

2016-04-27 by Mohammad H. Al-Shami

Sometimes when you have a high number of outgoing messages services like Yahoo! and Gmail might block you. To prevent that you need to distribute your outgoing emails through a set of IPs. I used HAProxy for that along with a number of FreeBSD Jails. That solution is a bit tedious, even though after I started using Salt things became a bit easier it was still too complicated for my taste.

Enters Postfix version 3. It introduced a new randmap table which made this much easier to accomplish.

In main.cf, just add the following lines

sender_dependent_default_transport_maps = 
  randmap:{relay1,relay2,relay3,relay4,relay5}
smtp_connection_cache_on_demand=no

Those lines have 2 effects:

  • Randomly select an SMTP client for sending each email
  • Prevent the SMTP clients from caching connections, so sending multiple emails to a single domain does not end up using the same SMTP client.

In master.cf, just add new SMTP services and configure them to bind the designated IPs

relay1     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP1
  -o smtp_helo_name=foo1.bar.com
  -o syslog_name=relay1
relay2     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP2
  -o smtp_helo_name=foo2.bar.com
  -o syslog_name=relay2
relay3     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP3
  -o smtp_helo_name=foo3.bar.com
  -o syslog_name=relay3
relay4     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP4
  -o smtp_helo_name=foo4.bar.com
  -o syslog_name=relay4
relay5     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP5
  -o smtp_helo_name=foo5.bar.com
  -o syslog_name=relay5


comment: