postfix: send a copy of every email to a given email address

flybywire picture flybywire · Apr 16, 2009 · Viewed 22.9k times · Source

I have a postfix email server and there is an issue I want to debug. How can I configure it to send a copy of every email to my email (in addition to delivering the email to its intended recipients).

Answer

Marc Munro picture Marc Munro · Nov 28, 2012

I recently got this working, so though I'd share:

Sending all outgoing mail into Sent folders with postfix and cyrus imap.

Summary

Make postfix send bcc copies of all outgoing emails to a special "sent" mail account. Use sieve on this special account to redirect all emails to the Sent folder associated with the account.

  1. Create the email user account for sent

    The easiest way to do this is to create a new unix account for sent, setting the shell to /bin/false to prevent anyone from being able to log in:

    host$ sudo useradd sent
    host$ sudo chsh -s /bin/false sent
    
  2. Set up imap for the sent user

    Using cyradm we create a new mailbox (ie user) and give that user append access to all of our imap "Sent" folders:

    host$ $ cyradm -user cyrus localhost
    Password: <enter you cyrus user admin password here>
    localhost> createmailbox user.sent
    localhost> setaclmailbox user.%.Sent sent append
    Setting ACL on user.userx.Sent...OK.
    Setting ACL on user.usery.Sent...OK.
    . . .
    Setting ACL on user.userz.Sent...OK.
    localhost> exit
    
  3. Create a sieve script for the sent account

    This script will redirect all incoming email to the sent account, to the Sent folder in the senders Inbox.

    My script is called sent.sieve and looks something like this:

     # Sieve script for sent.  If outgoing email is bcc'ed to this account,
     # this sieve script will redirect it to the sender's Sent folder
     require ["fileinto"];
    
     if address :is :localpart "From" "userx" {
       fileinto "user.userx.Sent";
     }
     elsif address :is :localpart "From" "usery" {
       fileinto "user.usery.Sent";
     }
     elsif address :is :localpart "From" "userz" {
       fileinto "user.userz.Sent";
    }
    

    You will need to put in an entry for each of your users (userx, usery, userz in the example above). I have not been able to find a better way of doing this. Suggestions are welcomed to [email protected]

    Install the sieve script like this:

    host$ sieveshell localhost -user=sent -a=cyrus
    Password: <enter you cyrus user admin password here>
    > put sent.sieve
    > activate sent.sieve
    > quit
    
  4. Set up the bcc mapping for postfix

    In the postfix directory (/etc/postfix on debian) create a file called bcc_map that looks like this:

    # copy all locally sent mail to the sent account
    @yourdomain.com       [email protected]
    

    Compile this into a postfix hash file using:

    host$ sudo postmap bcc_map
    

    Add the following line to the postfix main.cf configuration file:

    sender_bcc_maps = hash:/etc/postfix/bcc_map
    

    And make postfix reload its configuration:

    host$ sudo /etc/init.d/postfix reload

  5. Test and Debug

    Send some email and check that it is copied into your Sent folder.

    In the event of problems you should check the cyrus and postfix logs (all logged to /var/log/syslog on my debian host). Typos and incorrect access permissions will usually result in some clue being sent to the logs.