Copy Outgoing Email on a cPanel Server with Exim

4.9/5 - (61 votes)

Note: It is mentioned in the comments that recent versions of cPanel include an Email Archiving feature that stores copies of email on the server for you.  If this is what you’re trying to do, this feature is probably the better option. However, if you’re trying to forward copies of outgoing email to other addresses, the instructions in this post are appropriate.

A common request we get is how a user can automatically BCC outgoing email to another address. This is most often requested by law firms that need to be able to retain copies of communication. Forwarding incoming email is easy – there’s already a section in cPanel for it. However, if you want to forward outgoing email there’s a small twist.

First, go to WHM > Exim Configuration Editor, then click “Advanced Editor” at the bottom. Don’t make any changes, just click “save”. This should create a file on the server called /etc/exim.conf.local. From command line as root, you’ll need to edit /etc/exim.conf.local and add some options to the @CONFIG@ and @TRANSPORTSTART@ sections. The end result would look like this (plus any edits you may have already made):


@AUTH@

@BEGINACL@

@CONFIG@
system_filter_directory_transport = local_copy_outgoing

@DIRECTOREND@

@DIRECTORMIDDLE@

@DIRECTORSTART@

@ENDACL@

@RETRYEND@

@RETRYSTART@

@REWRITE@

@ROUTEREND@

@ROUTERSTART@

@TRANSPORTEND@

@TRANSPORTMIDDLE@

@TRANSPORTSTART@
local_copy_outgoing:
driver = appendfile
delivery_date_add
envelope_to_add
return_path_add
group = cpaneleximfilter
user = cpaneleximfilter
mode = 0660
maildir_format = true
create_directory = true

Now, make a copy of the existing system filter file to a custom location, so cPanel updates don’t mess with it:

cp /etc/cpanel_exim_system_filter /etc/cpanel_exim_system_filter_custom

Edit /etc/exim.conf.localopts and change the “systemfilter” option to match our new file:

systemfilter=/etc/cpanel_exim_system_filter_custom

Now, open /etc/cpanel_exim_system_filter_custom and add the following block of code to the end of the file:

if $header_from: contains "@senderdomain.com"
then
unseen deliver "other@emailaddress"
endif

Replace the blue text with the sender domain (or email address) and the email address that outgoing mail should be copied to.

Apply these new settings to the exim.conf and restart:

/scripts/buildeximconf
service exim restart

Now when you send email from the email address or domain specified in the filter file, it will be copied to the other email address.

41 Comments

  1. Roy Reply

    In your example, it only copies if it matches a certain “FROM domain”

    What if we wanted to base the rule on a user?
    I want to copy all outgoing emails, even those sent by PHP’s mail() function without a FROM
    so the pseudo-code is :

    if ( user = someuser )
    then
    unseen deliver “other@emailaddress”
    endif

    Is this possible?

    And is it possible to create a “copy all outgoing emails”. So ALL outgoing emails from the servers, even by PHP’s mail(), will be copied ?

    Thanks in advance Vanessa.
    Roy

  2. Pingback: Copy Outgoing Email on a cPanel Server with Exim | whatever...

  3. am3n Reply

    ### START OF OUTGOING + INCOMING MAIL FORWARDING ###
    ### domain tappeddomain.com ###

    if first_delivery
    and (“$h_to:, $h_cc:” contains “@tappeddomain.com”)
    or (“$h_from:” contains “@tappeddomain.com”)
    then
    unseen deliver “[email protected]
    endif

    ### END OF OUTGOING + INCOMING MAIL FORWARDING ###

  4. am3n Reply

    Vanessa,
    I just update cPanel from 11.28 into 11.30
    and on /etc/exim.conf.localops

    systemfilter no longer point to /etc/cpanel_exim_system_filter_custom

    instead it go to
    systemfilter=/usr/local/cpanel/base/eximacl/antivirusandspam.exim

    what should I do?
    and I get this message after cPanel update complete:
    ==================================
    cPanel was unable to automatically merge your Exim configuration with the new settings that shipped
    with the build you have installed (11.30.4 (build 6)) because you have a custom or broken configuration which
    cannot be automatically configured.

    Your current acl configuration is not compatible with this version of cPanel.
    This version of cPanel has an update that is critical, and must be installed to keep email functional on your system.

    To avoid any mail downtime, we have replaced the acl configuration with the default configuration.
    A copy of the old configuration has been saved to to /etc/exim.conf.local.1318092123.before_acl_version_8.2

    If you wish to preserve the old acl configuration, you need to manually merge your configuration
    with the new configuration settings.

  5. Ahmed Reply

    Hi all,

    My webhosting company uses SpamAssassin, can someone help me to achieve the same thing “Copy Outgoing Email on a cPanel Server with SpamAssassin”
    If it can’t be done is there any other way, because it seems like my hosting company don’t have qualified ppl to help, or I am not getting to them clearly.

    Please support.
    thanks.

    1. Vanessa Reply

      The server we tested this procedure on also uses SpamAssassin, and it worked. Keep in mind that the SpamAssassin implementation on cPanel servers only scans incoming mail – outgoing mail is not affected.

  6. Pingback: Using DKIM with Exim and cPanel › Lime VPS

  7. Suresh Reply

    Thanks.. It is working fine ..How to collect the incoming emails send via bcc ? Is it possible .. If so just assist me how to proceed ?

  8. Dave Reply

    if first_delivery
    and (“$h_from:, $h_to:, $h_cc:, $h_bcc:” contains “@345.com”)
    or (“$h_from:, $h_to:, $h_cc:, $h_bcc:” contains “@345.com”)
    or (“$h_from:, $h_to:, $h_cc:, $h_bcc:” contains “@45345.com”)
    or (“$h_from:, $h_to:, $h_cc:, $h_bcc:” contains “@fdghdfh.com”)
    or (“$h_from:, $h_to:, $h_cc:, $h_bcc:” contains “@345.com”)
    or (“$h_from:, $h_to:, $h_cc:, $h_bcc:” contains “@345.com”)
    then
    unseen deliver “[email protected]

  9. ron Reply

    I have done this but mails getting stuck in queue and erro msg
    LOG: MAIN
    cwd=/usr/local/cpanel/whostmgr/docroot 4 args: /usr/sbin/exim -v -M 1SmhbU-0004HM-FH
    delivering 1SmhbU-0004HM-FH
    LOG: MAIN PANIC
    Error in system filter: malformed address “[email protected].” in filter file: domain missing or malformed

    Could u please guide

  10. sree Reply

    we have our hosting account with hostmonster and we would like to have all incoming and outgoing mails to be bcc to a particular mail id. can anyone help me…

  11. Pingback: Some Fixes | firingdreams

  12. Pingback: How to Easily Monitor Cluster Status | The Server Blog

  13. Pingback: Exim / Cpanel auto bcc all sent emails – catalint.ro

  14. vipin Reply

    Hello Vanessa,

    Excellent work!!.

    How can we make this for all the domains in the server. I mean all the incoming and outgoing mails to be copied to a single account. What do you suggest to add instead of “if $header_from: contains “@senderdomain.com””. For specifying all the domains at a single line.

    Thanks.

  15. praveen Reply

    we have hosted multiple domains in server we have already implemented the Copy Outgoing Email on a cPanel Server with Exim filter with one domain so kindly help how to implement with one more domain. and how to mention the additional domain filter for outgoing mails for another domain.

    And how to mention this file /etc/exim.conf.localopts for multiple domains

    or directly we can mention the custom filter with multiple domains so please provide the information how to implement this.

    1. Vanessa Vasile Reply

      Thank you for your comment. I’ve updated the post with the link to the documentation, noting that the email archiving feature may not actually be a replacement in some cases. If the intention is to copy email for archiving purposes, yes. However, the post was initially intended for users that want to forward outgoing email to another address, which email archiving does not do.

  16. Cpanel user Reply

    Dear Vanessa Vasile
    I implemented this setting and i have only one issue.

    Everything works but when i filter the incoming emails to be unseen delivery to my gmail account, i will receive the email in my gmail but the sender email address is changed to system-filter@servername
    (change servername with your server hostname)

    But if i check the message source i can see the email address of the original sender.
    How to fix it?
    regards

    1. Vanessa Vasile Reply

      Does this happen with other mail providers? Try sending the email to another email address that is not @gmail.com and see if it still happens. Some mail providers will rewrite the sender if it thinks the sender is being spoofed, but I haven’t confirmed nor seen this issue myself.

    1. Vanessa Vasile Reply

      You need to use SSH. If you’re not familiar with how to do this, you should contact someone who does. Using root when you’re not familiar with how to even log in as root is probably not a good idea.

  17. Hung Reply

    I can’t edit /etc/cpanel_exim_system_filter_custom

    Waring :
    root@ns [~]# /etc/cpanel_exim_system_filter_custom
    -bash: /etc/cpanel_exim_system_filter_custom: Permission denied

    How to do next step

  18. Mihir Sheth Reply

    Any method where we can do this without WHM? My service provider doesn’t provide access to WHM, just cPanel. So I can’t access the SSH! 🙁
    I really need to get a copy of the sent emails!

Leave a Reply

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

Log in