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'
then
    logwrite "Mail loop?"
    finish
endif

headers add "X-Loop-SystemFilter: ok"

if $sender_address contains "@test.com" and $h_to contains "destination@example.org" 
then
    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
endif

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.


Posted

in

by

Tags:

Comments

3 responses to “Exim selective copying/bcc’ing of mail based on sender and recipient”

  1. Brian Avatar
    Brian

    Exactly what I needed! Thanks!

  2. Anant Avatar
    Anant

    Can you please help me with one of my problem with exim mail server on cPanel ? I want to change the from field for the mails being forwarded from a particular mail ID. Right now it forwards the emails with Keeping FROM field of original sender.I want to change it to the mail ID from which it is being forwarded.

  3. Nirav Shah Avatar
    Nirav Shah

    Hi
    We are facing a very typical issue, we have configured all incoming mails from user1@domain.com and user2@domain.com to be forwarded to user3@domain.com if unseen. but the problem is that when the emails are coming from a few hosting companies especially rediffmail hosting then if the email is sent to user1@domain.com and user2@domain.com and cc to user3@domain.com ideally since user3@domain.com is seen hence the rule should deliver only 1 copy of the email to this user3@domain.com. this scenario works well 85% – 90% of times but in a few cases this same message carries 3 different ESMTP IDs and hence the rules get triggered thrice and the user3@domain.com recieves 3 copies. can you please help address this.

Leave a Reply

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