Sending e-mail from random IP

hape

Verified User
Joined
Jul 22, 2010
Messages
80
Location
Poland
[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:
  • 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.
IPv4RevDSN/Helo
127.10.0.1ip1.example.net
127.10.0.2ip2.example.net
127.10.0.3ip3.example.net
127.10.0.4ip4.example.net

How to:
  1. Login to server on a console as root or user with sudo access
  2. 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
  3. Open /etc/exim.conf and find:
    Code:
    remote_smtp:
    driver = smtp
    add below:
    Code:
    .include_if_exists /etc/exim.ip.conf
  4. 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}}
  5. 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)];
    
    }
  6. 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"
    You can also use ~all insted off -all - it depends on your needs, for more use Google.
  7. Restart exim:
    Code:
    service exim restart
  8. 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
  1. 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
    and restart named
    Code:
    service named restart
All operations has been performed on Centos 6.
Remember to make backups and change IPs from 127.10.0.x to your own. And sorry for my English.
 
Last edited:
thank you. a client sends many mails (not spam mails) to hotmail and hotmail blocks ips. could this be a workaround?
 
Back
Top