How to block external mails in exim

Sometimes we need to block the mails from our domain to external domains. This can be achieved by adding custom lines in  exim configuration. This article explains the configuration details to block mails to outside domains.

Open exim configuration file /etc/exim.conf and enter the below lines in the "ROUTERS CONFIGURATION" block.

check_outgoing:
driver = redirect
domains = ! +local_domains
senders = ! : ! lsearch;/etc/whitelisted_mailids
allow_fail
data = :fail: External mails blocked by Administrator
Restart exim service

The working of this set up is explained below using real time conditions for the domain example.com.

Case1
-------
from: user1@example.com
to: user@gmail.com >>> Mail failed with message "External mails blocked by Administrator"

Case2
-------
from: user1@example.com
to: user2@example.com >>> Mail Sent with out any issues
cc: user@gmail.com >>> Mail failed with message "External mails blocked by Administrator"

Case3
-------
from: user1@example.com
to: user2@example.com >>> Mail Sent with out any issues
cc: user3@example.com >>> Mail Sent with out any issues
bcc: user@gmail.com >>> Mail failed with message "External mails blocked by Administrator"
Note1:
If we need to whitelist any mailids from this restriction, just save those ids in the file /etc/whitelisted_mailids. Those id's in the file whitelisted_mailids can send mails to outside domains.

Note2:
Its important that we could not whitelist a domain by simply adding *@domain.com as in the case of exim filter. We need to specify each and every ids seperately like wise user1@domain.com and user2@domain.com for all domains to be whitelisted.

Note3:
If we need to use wildcards in whitelist file, just change the custom configuration in exim.conf as given below.

check_outgoing:
driver = redirect
domains = ! +local_domains
senders = ! : ! wildlsearch;/etc/whitelisted_mailids
allow_fail
data = :fail: External mails blocked by Administrator
Now we can whitelist a domain by simply adding *@domain.com to the file /etc/whitelisted_mailids.

Leave a Reply

Your email address will not be published. Required fields are marked *