[EXIM] Sending e-mails from random IP
Description: sometimes we have a server with a few IP addresses and we want to send e-mails not from one IP. So we will configure exim for sending e-mails randomly from IP range with different PTRs and helo for every IP.
Assumptions:
For this article we will use this subnet and revdns as examples:
Remamber to set valid PTR(revdns) and A records for IP.
[table="width: 500"]
[tr]
[td]IPv4[/td]
[td]RevDSN/Helo[/td]
[/tr]
[tr]
[td]127.10.0.1[/td]
[td]ip1.example.net[/td]
[/tr]
[tr]
[td]127.10.0.2[/td]
[td]ip2.example.net[/td]
[/tr]
[tr]
[td]127.10.0.3[/td]
[td]ip3.example.net[/td]
[/tr]
[tr]
[td]127.10.0.4[/td]
[td]ip4.example.net[/td]
[/tr]
[/table]
How to:
We use it in our company and it works for many IP addresses. Some configs aren't mine, but I think that they aren't copyrighted
FAQ
Remember to make backups and change IPs from 127.10.0.x to your own. And sorry for my English.
Description: sometimes we have a server with a few IP addresses and we want to send e-mails not from one IP. So we will configure exim for sending e-mails randomly from IP range with different PTRs and helo for every IP.
Assumptions:
- exim
- more than one public IP address
- may not work with IPv4 and IPv6 together
For this article we will use this subnet and revdns as examples:
Remamber to set valid PTR(revdns) and A records for IP.
[table="width: 500"]
[tr]
[td]IPv4[/td]
[td]RevDSN/Helo[/td]
[/tr]
[tr]
[td]127.10.0.1[/td]
[td]ip1.example.net[/td]
[/tr]
[tr]
[td]127.10.0.2[/td]
[td]ip2.example.net[/td]
[/tr]
[tr]
[td]127.10.0.3[/td]
[td]ip3.example.net[/td]
[/tr]
[tr]
[td]127.10.0.4[/td]
[td]ip4.example.net[/td]
[/tr]
[/table]
How to:
- Login to server on a console as root or user with sudo access
- Create file with helo mapping to IP: /etc/exim.helo.conf with:
Code:127.10.0.1:ip1.example.net 127.10.0.2:ip2.example.net 127.10.0.3:ip3.example.net 127.10.0.4:ip4.example.net
- Open /etc/exim.conf and find:
Code:remote_smtp: driver = smtp
Code:.include_if_exists /etc/exim.ip.conf
- Create file /etc/exim.ip.conf with:
Code:interface = "${perl{randinet}}" helo_data = ${lookup{$sending_ip_address}lsearch*{/etc/exim.helo.conf}{$value} {$primary_hostname}}
- Edit file /etc/exim.pl, and add below existing content:
Code:# Random ip selection sub randinet { @inet = ("127.10.0.1","127.10.0.2","127.10.0.3","127.10.0.4"); return $inet[int rand($#inet+1)]; }
- Create file /usr/local/directadmin/data/templates/custom/dns_txt.conf with:
Code:|DOMAIN|.="v=spf1 a mx ip4:127.10.0.1 ip4:127.10.0.2 ip4:127.10.0.3 ip4:127.10.0.4 -all"
- Restart exim:
Code:service exim restart
- Check you have no errors. That's all.
We use it in our company and it works for many IP addresses. Some configs aren't mine, but I think that they aren't copyrighted

FAQ
- How to modify existing SPF records?
It's not possible to use DirectAdmin task system to rewrite bind/named zones, so you can use this script to perform that operation:
Code:#!/bin/bash OLD="v=spf1 a mx ip4:YOUR_OLD_IP ~all" NEW="v=spf1 a mx ip4:127.10.0.1 ip4:127.10.0.2 ip4:127.10.0.3 ip4:127.10.0.4 -all" DPATH="/var/named/*.db" BPATH="/root/backup_DNS" TFILE="/tmp/out.tmp.$$" [ ! -d $BPATH ] && mkdir -p $BPATH || : for f in $DPATH do if [ -f $f -a -r $f ]; then /bin/cp -f $f $BPATH sed "s/$OLD/$NEW/g" "$f" > $TFILE && mv $TFILE "$f" else echo "Error: Cannot read $f" fi done
Code:service named restart
Remember to make backups and change IPs from 127.10.0.x to your own. And sorry for my English.
Last edited: