Shami's Blog

Sysadmin, Because Even Developers Need Heroes

Randomize Source IP Addresses With Postfix


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, just add the following lines

sender_dependent_default_transport_maps = 

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, just add new SMTP services and configure them to bind the designated IPs

relay1     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP1
  -o syslog_name=relay1
relay2     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP2
  -o syslog_name=relay2
relay3     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP3
  -o syslog_name=relay3
relay4     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP4
  -o syslog_name=relay4
relay5     unix  -       -       n       -       -       smtp
  -o smtp_bind_address=IP5
  -o syslog_name=relay5

About Me

Dev gone Ops gone DevOps. Any views expressed on this blog are mine alone and do not necessarily reflect the views of my employer.

twitter linkedin