Exim selective copying/bcc’ing of mail based on sender and recipient

If you’re unfortunate enough to be using Exim, and you need to configure the server to selectively bcc some emails (e.g. emails going to a specific recipient need bcc’ing to another address for archiving/compliance purposes) then the following may be useful :

1. Edit /etc/exim4/exim4.conf and add near the top the following :

system_filter = /etc/exim4/mail.filter
system_filter_user = Debian-exim

and restart Exim.

2. Edit /etc/exim4/mail.filter as per the documentation creating something a bit like :

# Exim filter

logfile /tmp/mail.log
logwrite "$tod_log $h_to $h_from $h_subject "

if $header_x-loop-systemfilter contains 'ok'
    logwrite "Mail loop?"

headers add "X-Loop-SystemFilter: ok"

if $sender_address contains "@test.com" and $h_to contains "destination@example.org" 
    logwrite "$tod_log Mail to: $h_to from: $sender_address subject: $h_subject bcc'ed to bcc_address@example.org"
    unseen deliver bcc_address@example.org

So, mail sent from someone matching @test.com to destination@example.org will be bcc’ed to bcc_address@example.org. Changing destination@example.org to just @example.org would obviously catch everything going to the example.org domain through the server.

1 thought on “Exim selective copying/bcc’ing of mail based on sender and recipient”

Leave a Reply

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