How to Configure Exim to receive email for domain only from specific IP addresses

This article describes one way to limit incoming SMTP connections for specific domains to a list of specific safe relay IP addresses. Let’s suppose that you have a domain, – and you want to filter all email for this domain via an external host – lets call it

You would typically set the MX records for to point to as follows: 14400 IN MX 10

And, presumably, you would configure to send your sanitised, cleaned mail back to and set your exim config to accept all mail locally for your domain. Great, so now you are happily filtering your mail for spam – but wait… some sneaky monkey decides to try sending spam directly to, and your exim happily receives it, because exim isn’t aware of the DNS settings, and doesn’t know any better.

So, we need to tell exim NOT to accept any mail for UNLESS it is coming from, lets say for the sake of argument, Let’s say that has an IP address of

Build the safe lists

The first thing we want to do is create a couple of files. One to contain the names of the domains we want to restrict, and a second list, showing the IP address(es) of the approved relays (i.e. the IP addresses of the machines that will be sending out cleaned mail back to us).

Let’s call the first file /etc/filterdomains – this will contain the list of domains we want to filter, and have set up the special MX records for. It is just a list of domain names (one per line):

The second file we will call /etc/filterrelays – this will contain a line separated list of the safe relay IP addresses – which might look like this:

Modify exim.conf

Firstly, if you are using WHM, then just use the advanced Exim Configuration editor to make these changes and they “should” stick. It would be a good idea to take a copy of all /etc/exim* files first, just in case you make a hash of it!

OK, now we are in the bear pit. We need to add a couple of definitions at the top of the exim.conf file as follows :

hostlist filter_relays = net-lsearch;/etc/filterrelays : net-lsearch;/etc/relayhosts
domainlist filter_domains = lsearch;/etc/filterdomains

Then, we are looking for the check_recipient: block and shortly after that there should be a line :

accept  hosts = :

Then, following this line, you can add the following :

!hosts = +filter_relays
domains = +filter_domains
message = Please use the proper domain MX record

Once you have done this, restart Exim using either

/etc/init.d/exim restart

or if you are running WHM you can run


And if everything is well you should find that mail for the domains in /etc/filterdomains will only be accepted from your mail filtering service IP addresses, keeping the cheaky monkeys out!

Notes on usage

Again, this was done using exim4 under the WHM 11.25.* environment on CentOS. Always test things like this in a safe environment, not your production servers. Use of this information is don so entirely at your own risk!

Tags: , ,

5 Responses to “How to Configure Exim to receive email for domain only from specific IP addresses”