Tip: Add Disclaimer To Only Outgoing Email Using Altermime

On our mail server at work, we added a disclaimer to all outgoing emails using Altermime. This has been working fine for a few months now. There was just one thing that annoyed me about it, it added to all outgoing emails, including internal mail between users.

So I went about trying to find a solution and eventually found out how to do it so thought I’d share it here and also keep it as a reference to me as it took me ages to find it ๐Ÿ˜›

  1. # wget http://www.pldaniels.com/altermime/altermime-0.2.2.tar.gz
    # tar -xzvf altermime-0.2.2.tar.gz
    # cd altermime-0.2.2
    # make

    # cp altermime /usr/bin/
    # chown root.root /usr/bin/altermime
    # chmod 755 /usr/bin/altermime

  2. # useradd -r -c “Postfix Filters”ย -d /var/spool/filter filter
    # mkdir /var/spool/filter
    # chown filter.filter /var/spool/filter
    # chmod 750 /var/spool/filter
  3. # Creating The Script To Run alterMIME

    # vi /etc/postfix/disclaimer

    #!/bin/sh
    # Localize these.
    INSPECT_DIR=/var/spool/filter
    SENDMAIL=/usr/sbin/sendmail
    
    #Exit codes from
    EX_TEMPFAIL=75
    EX_UNAVAILABLE=69
    
    RECIP=`echo $* | awk '{print $NF}'| tr [A-Z] [a-z]`
    SENDER=`echo $* | awk '{print $2}'| tr [A-Z] [a-z]`
    RECIP_DOMAIN=`echo $RECIP | awk -F"@" '{print $2}'`
    SEND_DOMAIN=`echo $SENDER | awk -F"@" '{print $2}'`
    
    # Clean up when done or when aborting.
    trap "rm -f in.$$" 0 1 2 3 15
    
    # Start processing.
    cd $INSPECT_DIR || { echo $INSPECT_DIR does not exist; exit $EX_TEMPFAIL; }
    cat >in.$$ || { echo Cannot save mail to file; exit $EX_TEMPFAIL; }
    
    if [ $RECIP_DOMAIN == $SEND_DOMAIN ]; then
    DISCLAIMER=/etc/postfix/empty_disclaimer.txt
    else
    DISCLAIMER=/etc/postfix/disclaimer.txt
    fi
    
    /usr/bin/altermime --input=in.$$ 
    --disclaimer=$DISCLAIMER 
    --disclaimer-html=$DISCLAIMER || 
    { echo Message content rejected; exit $EX_UNAVAILABLE; }
    $SENDMAIL "$@" 
    

    # chgrp filter /etc/postfix/disclaimer
    # chmod 750 /etc/postfix/disclaimer

    /etc/postfix/empty_disclaimer.txt is the disclaimer which will get used for internal email, ie when the sending domain is the same as the receiving domain, in my case I made it a blank file.
    /etc/postfix/disclaimer.txt is the disclaimer which gets added to all other emails, in that we have some stuff saying it may be copyrighted or something, can't remember now.

  4. # Creating The Disclaimer Files

    # vi /etc/postfix/disclaimer.txt
    # vi /etc/postfix/empty_disclaimer.txt

  5. # Postfix Configuration

    Edit master.cf :

    # vi /etc/postfix/master.cf

    127.0.0.1:smtp inet n - y - - smtpd
    smtp inet n - y - - smtpd
    -o content_filter=dfilt:
    dfilt unix - n n - - pipe
    flags=Rq user=filter argv=/etc/postfix/disclaimer -f ${sender} รขโ‚ฌโ€ ${recipient}
    

  6. # Restart Postfix

    # postfix reload

10 Replies to “Tip: Add Disclaimer To Only Outgoing Email Using Altermime”

  1. Here’s the version I’m using. It’s probably faster since it spawns fewer processes. N.B.: I’m assuming dnsdomainname returns a lowercase value.

    # Only append the disclaimer if it’s leaving our domain
    RCPT_DOMAIN=$(echo $4 | awk ‘BEGIN {FS=”@”} {print tolower($NF)}’)
    LOCAL_DOMAIN=$(dnsdomainname)
    if [ “$RCPT_DOMAIN” != “$LOCAL_DOMAIN” ]; then
    # Call alterMIME, hand over the message and
    # tell alterMIME what to do with it
    $ALTERMIME –input=in.$$
    –disclaimer=/opt/altermime/disclaimer.txt
    –disclaimer-html=/opt/altermime/disclaimer.htm
    –force-for-bad-html –log-syslog
    || { echo Message content rejected; exit $UNAVAILABLE; }
    fi

    # Call sendmail to reinject the message into Postfix
    $SENDMAIL “$@”

  2. Oops, my pre tags were eaten in the comment.

    Also, the $( … ) construct should be replaced with backticks if you want to be truly /bin/sh compatible.

  3. Hi,

    I tested it with the latest altermime 0.3.8 and followd your instructions.

    Got some problems :
    – no mails are sent
    – if sent the disclaimer do not appears

    Can you help me?
    Thanks

  4. balakrishnan – I’ve added some more detailed instructions now, does that help?

    Said – Not sure, did you follow each step? Did email send fine before you tried this?

  5. Yes, the server is up for one year now, since i’ve added the disclaimer,
    I can’t add the 127.0.0.1 line as it says that it cna’t bind to address 0.0.0.0

    the error is ;
    hostname LOCALHOST verification failed: Name or service not known

    2. why do you use a chroot environnement for smtpd?

    my master contains :

    smtp inet n – n – – smtpd
    -o content_filter=dfilt:

  6. I’m not going to be able to be of much help as the main script I originally found on the internet and then just modified it for not sending to local address’s, it was originally setup by a colleague of mine and I just made the changes, sorry.

  7. Hi,
    got it,
    there is a bug with the latest version
    working with 0.3.7

    still a last problem if you got it : i can’t insert html code it convert it to plain text

  8. trying to work with the same instead of postfix ,using sendmail 8.13 ,dovecot 0.99

    how to do it ..
    thanks in adv

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.