====== Alter out-going and in-coming e-mail with postfix: Howto ====== This howto describes how to setup the Postfix smpt server to alter e-mails before forwarding them to local mail delivery agent (e.g cyrus) and remote smtp servers. ===== Steps ===== The modify e-mails in their way to external smtp servers or to local MTAs, postfix offers a filtering mechanism that can be easily configured by modifying the **master.cf** file. These are the steps that need to be done. ====1. Setup /etc/postfix/master.cf ==== Replace the line smtp inet n - n - - smtpd by smtp inet n - n - - smtpd -o content_filter=filter: The '-o content_filter=filter:' option causes postfix to forward all incoming e-mails to the 'filter transport'. The 'filter transport' is defined in the same file (/etc/postfix/master.cf) by adding a line as follows filter unix - n n - - pipe flags=Rq user=mailnull argv=/usr/local/bin/script.sh The parameter argv specifies a program/script (/usr/local/bin/script.sh) to which the message will be piped. ====2. Create a filtering program/script ==== The program/script to which the message will be piped can perform changes of any sort on the messages. If the message is to be sent to the final destination, the script has the responsibility of forwarding the message using sendmail. A simple script could contain just the following lines. #!/bin/sh cat | sed 's/xxx/yyy/g' | /usr/sbin/sendmail -t To test the filtering on the smtp server, we need to configure the e-mail client to use the smtp server then send a message that contains the string 'xxx'. The e-mail in receiving mailbox, will have the string 'xxx' replaced by 'yyy'. This is a very simple example, but you can use the same mechanism to develop more useful features such as adding header/footer/disclaimer/signature, removing attachments, converting html to text, scanning for viruses, etc... {{tag>howto unix}}