Consulting Ian Tighe Putting Technology To Work

Adding Value

Technology and Project Advice




Making Things Work Better

Know-How
Postfix::Footers and Disclaimers on email messages

Postfix does not support putting a disclaimer or footer on outgoing email messages. To do this you have to inject a bit of extra code to make it work. There are decent articles that does this but they do not do this on a domain basis (rather it does a mailbox by mailbox basis). Also every time a message in a to/reply/reply/reply scenario is generated the disclaimer gets added on each turn around of the message which is somewhat tedious. The decent articles if you search the web.A set we used that follow the same theme are here and especially here. Below is our modified script to ensure all mail boxes for a domain or set of domains have footers added and our footer is only inserted once.

Our design goals were to:

  • To use the abilities of postfix to provide a simple filter mechanism to insert footers
  • To be able to handle footers for both individual mailboxes and for domains
  • Provide a footer that would service mutiple configurable domains thus provide different footers per domain if required
  • Provide a footer that could be used for individual mailboxes thus providing different footers per mailbox if required
  • Provide each mailbox or domain with a configurable header tag to allow different domains to be referenced in the mail message headers
  • To be able to exclude domains services by postfix from any disclaimer footer by simply leaving out confugation details of that domain
  • Not to keep adding a disclaimer as a to-->reply-->reply conversation takes place
  • The address file contains all the configurable data and an example of mixed domains, mailboxes, footers and http links is shown at the end of this article.

    Our master.cf enrties are configured as per the articles with entries as below.

    smtp inet n - n - - smtpd
    -o content_filter=dfilt:
    and at the bottom of the file:

    # add footer to all mail with altermime

    dfilt unix - n n - - pipe
    flags=Rq user=filter argv=/etc/postfix/disclaimer -f ${sender} -- ${recipient}

    Our disclaimer script looks like this:

    #!/bin/sh
    # Consulting Ian Tighe and others 2009

    INSPECT_DIR=/var/spool/filter
    SENDMAIL=/usr/sbin/sendmail.postfix
    DISCLAIMER_ADDRESSES=/etc/postfix/disclaimer_addresses
    #
    # Exit codes from <sysexits.h>
    #
    EX_TEMPFAIL=75
    EX_UNAVAILABLE=69
    #
    # Clean up when done or when aborting.
    #
    trap "rm -f in.$$" 0 1 2 3 15
    cd $INSPECT_DIR || { "echo $INSPECT_DIR does not exist" >> /var/spool/filter/log; exit
    $EX_TEMPFAIL; }

    cat >in.$$ || { echo "Cannot save mail to file" >> /var/spool/filter/log; exit $EX_TEMPFAIL; }
    #
    # if the email already has a disclaimer of our then do not add another one. Skip everything and send the email back.
    #
    if [ -z `grep -m 1 "* CONFIDENTIALITY, PRIVACY, COPYRIGHT AND SECURITY NOTICE *" in.$$ |awk '{print $3}'` ]
    then

    #
    # obtain From mailbox and domain from the mail message being sent
    #
    from_domain=`grep -m 1 "From:" in.$$ | cut -d "<" -f 2 | cut -d ">" -f 1 | cut -d "@" -f 2`
    from_mailbox=`grep -m 1 "From:" in.$$ | cut -d "<" -f 2 | cut -d ">" -f 1 `
    #
    # Get and process the address file data -
    # Record format is domain or mailbox name and optional disclaimer file name and optionally a web link.
    # Do mailbox first to get to the most granular possibility first
    #
    server_mailbox=`grep -i ^${from_mailbox} ${DISCLAIMER_ADDRESSES} | awk '{print $1}'`
    server_domain=`grep -i ^${from_domain} ${DISCLAIMER_ADDRESSES} | awk '{print $1}'`
    if [ ! -z $server_mailbox ]
    then
    from=$server_mailbox
    elif [ ! -z $server_domain ] then
    from=$server_domain
    else
    #echo `date` >> /var/spool/filter/log
    #echo "Nothing found. Not all domains and mailboxes use disclaimers.Passing email on." >> /var/spool/filter/log
    #echo "Debug: $from_domain,$from_mailbox,$server_mailbox,$server_domain,$from" >> /var/spool/filter/log
    $SENDMAIL "$@" <in.$$;
    exit $?
    fi
    #
    # set a default disclaimer file if none stated. Set default if stated disclaimer file does not exist.
    #
    disclaimer_filename=`grep -i ^${from} ${DISCLAIMER_ADDRESSES} | awk '{print $2}'`
    if [ -z $disclaimer_filename ]
    then
    disclaimer_filename="/etc/postfix/disclaimer.txt"
    elif [ ! -r $disclaimer_filename ]
    then
    disclaimer_filename="/etc/postfix/disclaimer.txt"
    fi
    #
    # Set copyright tag header information per mailbox or per domain
    #
    copyright=`grep -i ^${from} ${DISCLAIMER_ADDRESSES} | awk '{print $3}'`
    if [ -z $copyright ]
    then
    copyright="http://www.renegade-hosting.co.uk"
    fi
    #
    # alter the email
    #
    /usr/bin/altermime --input=in.$$ --disclaimer=$disclaimer_filename \
    --disclaimer-html=$disclaimer_filename \
    --xheader="X-Copyrighted-Material: Please visit $copyright" || \
    { echo Message content rejected; exit $EX_UNAVAILABLE; }
    fi

    #
    # send the altered email
    #
    $SENDMAIL "$@" <in.$$;
    exit $?

    Our address file looks like this:

    domain1.com /users/web/joe/disclaimer.txt http://www.domain1.com
    domain2.co.uk
    bill@domain2.net /users/web/bill/disc.txt

    Our disclaimer.txt looks like this:

    _________________________________________________________________________________________________________________________________ * CONFIDENTIALITY, PRIVACY, COPYRIGHT AND SECURITY NOTICE * This mail is confidential to the adressee only and any other use of it is unauthorised. If you are not the adressee and you receive it by accident or otherwise please destroy it and notify the sender of such. To act on it or disminate any of its contents on an unauthorised basis may be a criminal offence and you leave youself open to prosecution. All the material in this email is Copyright. The presence of this notice applies to all parts of this entire email whether those elements pre or post date the insertion of this message. This mail has been scanned by Consulting Ian Tighe @ renegade-hosting.co.uk and was free of viruses and harmful objects when dispatched. Great care has been taken to try and reassure you that this email is genuine. DKIM and SPF techniques are used to allow your mail server to assure you this is a bone fide email. We promote these methods and would encourage your technical team to adopt them to help reduce spam and harmful emails. __________________________________________________________________________________________________________________________________

    Finally

    Note that the scripts searches for the key phrase in the disclaimer.txt file to see if the message alreay contains a disclaimer. You would need to change this for whatever pharse you choose in your text file. When we added the user "filter" we made it so that it could not be logged into.

    Return to Know-How Index of articles
    Return to Home Page
    Copyright © Consulting Ian Tighe 2005-2008.