Exim4 Outbound IP address based on web domain IP

Just in case someone needs this functionality. This is how i resolve it:

I made the following script that creates a file with a domain: ip mapping:

#!/bin/bash -l

DEFAULT_IP=178.238.238.233
EXIM_DOMAIN_IPS_FILE=/etc/exim4/domainips
HESTIA_USERS_DATA_DIR=/usr/local/hestia/data/users

# First the default ip
echo "*: $DEFAULT_IP" > $EXIM_DOMAIN_IPS_FILE

# Then search for web domains and extract the ip
for USER_DATA_DIR in $HESTIA_USERS_DATA_DIR/*
do
  while IFS="" read -r line || [ -n "$line" ]
  do
    eval $line
    echo "$DOMAIN: $IP" >> $EXIM_DOMAIN_IPS_FILE
  done < $USER_DATA_DIR/web.conf
done

And then in exim conf file, under transports:

remote_smtp:
  driver = smtp
  helo_data = mail.${sender_address_domain}
  dkim_domain = DKIM_DOMAIN
  dkim_selector = mail
  dkim_private_key = DKIM_PRIVATE_KEY
  dkim_canon = relaxed
  dkim_strict = 0
  interface = "${lookup{$sender_address_domain}lsearch*{/etc/exim4/domainips}{$value}}"

Then i configure a cron to run this script every 5 min to keep the list up to date.

3 Likes