Author Topic: How to add an e-mail disclaimer to outgoing mail  (Read 8813 times)

Barrydocks

  • Zen Warrior
  • ***
  • Posts: 141
  • Karma: +4/-0
    • View Profile
How to add an e-mail disclaimer to outgoing mail
« on: April 16, 2011, 12:28:27 pm »
This is really for my own benefit so that I have documented what I did to get this to work.

The idea it to allow a disclaimer or footer to be added to all mail leaving the zentyal mail server.  The problem is that (as per usual) zentyal will over write any change you make to the config files.

So here goes (you will need root access from the cli and a working mail setup):
1.  install alterMIME:
Code: [Select]
apt-get install altermime2. create the user filter with the home directory /var/spool/filter filter - alterMIME will be run as that user:
Code: [Select]
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
3a. create the file /etc/postfix/disclaimer_addresses which holds all sender email addresses (one per line) for which alterMIME should add a disclaimer:
Code: [Select]
nano /etc/postfix/disclaimer_addresses3b. add your addresses:
Code: [Select]
#list individual address here
user1@yourdomain.com
user2@anotherdomin.com
# or for all addresses on a single domain (you will need to amend the /etc/postfix/disclaimer file appropriately:
yourdomain.com
sub.yourdomain.com
4a. create the script /etc/postfix/disclaimer which executes alterMIME, with the appropriate modifications:
Code: [Select]
nano /etc/postfix/disclaimer4b. paste the following:
Code: [Select]
#!/bin/sh
# Localize these.
INSPECT_DIR=/var/spool/filter
SENDMAIL=/usr/sbin/sendmail

####### Changed From Original Script #######
DISCLAIMER_ADDRESSES=/etc/postfix/disclaimer_addresses
####### Changed From Original Script END #######

# 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
# 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; }

####### Changed From Original Script #######
# obtain From address
# uncomment this line if you have individual e-mail addresses in the /etc/postfix/disclaimer_addresses file
#from_address=`grep -m 1 "From:" in.$$ | cut -d "<" -f 2 | cut -d ">" -f 1`

# uncomment this line if you have and entire domain listed in the /etc/postfix/disclaimer_addresses file
from_address=`grep -m 1 "From:" in.$$ | cut -d "<" -f 2 | cut -d ">" -f 1 | cut -d "@" -f 2`

if [ `grep -wi ^${from_address}$ ${DISCLAIMER_ADDRESSES}` ]; then
  /usr/bin/altermime --input=in.$$ \
                   --disclaimer=/etc/postfix/disclaimer.txt \
                   --disclaimer-html=/etc/postfix/disclaimer.txt \
                   --xheader="X-Copyrighted-Material: Please visit http://www.c$
                    { echo Message content rejected; exit $EX_UNAVAILABLE; }
fi
####### Changed From Original Script END #######

$SENDMAIL "$@" <in.$$
exit $?
You will need to uncomment the correct line depending on whether you have individual e-mail addresses listed in the /etc/postfix/disclaimer_addresses file or an entire domain
4c. set the permissions:
Code: [Select]
chgrp filter /etc/postfix/disclaimer
chmod 750 /etc/postfix/disclaimer

5a.  now for the trick to get zentyal to modify the /etc/postfix/master.cf automatically.  Create a postsetconf script for the mail module:
Code: [Select]
nano /etc/ebox/hooks/mail.postsetconf5b. paste the following:
Code: [Select]
#!/bin/sh

#adds filter for smtp messages
sed 's/smtp      inet  n       -       -       -       -       smtpd.*/& \n  -o content_filter=dfilt:/' /etc/postfix/master.cf > /etc/postfix/temp_file
mv /etc/postfix/temp_file /etc/postfix/master.cf

#adds filter for smtps messages
sed 's/smtps     inet  n       -       -       -       -       smtpd.*/& \n  -o content_filter=dfilt:/' /etc/postfix/master.cf > /etc/postfix/temp_file
mv /etc/postfix/temp_file /etc/postfix/master.cf

#no idea what these lines do but they are needed at the bottom of the master.cf file
echo dfilt unix - n n - - pipe >> /etc/postfix/master.cf
echo '   flags=Rq user=filter argv=/etc/postfix/disclaimer -f ${sender} -- ${recipient}' >> /etc/postfix/master.cf

exit 0
5c. Set the permissions:
Code: [Select]
chmod +x /etc/ebox/hooks/mail.postsetconf 6.  edit the /etc/postfix/disclaimer.txt and insert whatever you want added:
Code: [Select]
nano /etc/postfix/disclaimer.txt7. reset the mail module on the zentyal dashboard
Done  :)

Troubleshooting:
1. the /etc/postfix/master.cf seems to be very fussy over the spaces between characters especially if the mail.postsetconf script was pasted from windows, it is worth opening a cli text editor and replacing the spaces
2.  when the disclaimer is set up to be added to all messages on a domain (rather than individual addresses) if an e-mail is set to a recipient on the same domain they will get the disclaimer twice - this is due to the fact that postfix can't differentiate between incoming and outgoing mail, the script deals with this for everything other than the same domain.

References:
http://www.howtoforge.com/add-disclaimers-to-outgoing-emails-with-altermime-postfix-debian-etch
http://www.howtoforge.com/add-automatic-disclaimers-to-outgoing-emails-with-altermime-postfix-fedora8

Please point out any typos you might spot, any additional suggestions are warmly welcome ;D
« Last Edit: April 16, 2011, 12:41:12 pm by Barrydocks »

duncan

  • Zen Apprentice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: How to add an e-mail disclaimer to outgoing mail
« Reply #1 on: April 16, 2012, 05:36:30 pm »
I tried this approach and when I restart the mail module I get no response back from the server. When I run top there is a Sed process using all the CPU and disk. Any ideas would be grateful.

Cheers