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 main.cf, just add the following lines
1sender_dependent_default_transport_maps =
2 randmap:{relay1,relay2,relay3,relay4,relay5}
3smtp_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
1relay1 unix - - n - - smtp
2 -o smtp_bind_address=IP1
3 -o smtp_helo_name=foo1.bar.com
4 -o syslog_name=relay1
5relay2 unix - - n - - smtp
6 -o smtp_bind_address=IP2
7 -o smtp_helo_name=foo2.bar.com
8 -o syslog_name=relay2
9relay3 unix - - n - - smtp
10 -o smtp_bind_address=IP3
11 -o smtp_helo_name=foo3.bar.com
12 -o syslog_name=relay3
13relay4 unix - - n - - smtp
14 -o smtp_bind_address=IP4
15 -o smtp_helo_name=foo4.bar.com
16 -o syslog_name=relay4
17relay5 unix - - n - - smtp
18 -o smtp_bind_address=IP5
19 -o smtp_helo_name=foo5.bar.com
20 -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.
Recent Posts