//
you're reading...

Blog

How to use Procmail with ZCS

This article will guide you through setting up Procmail along with Zimbra Collaboration Server (ZCS). For those who do not know about procmail, it is a mail delivery agent (MDA) capable of sorting incoming mail into various directories and filtering out spam messages. The important feature of procmail is its ability to filter out incoming/outgoing mails based on regular expression, and take action upon the sorted out mail (e.g. move to a different folder, mark as spam, forward it to another address, or discard/delete it).

ZCS does not natively support Procmail, and hence there are 3 ways to use procmail with ZCS:

  1. Use ZCS with a MTA set to a server/installation with procmail configured (e.g. ZCS has a MTA set to another machine which has postfix with procmail configured and all the filtering is done by that server)
  2. Use ZCS as normal, and run procmail on your mailbox (through the procmailrc file), to do the sorting/filtering.
  3. Use ZCS with procmail as a pipe for mail delivery. (This guide will discuss this way of using procmail.)

Let’s assume:

  • The mail server with ZCS has already been installed and configured. If not, check my ZCS installation guide and install ZCS.
  • We want to use procmail to filter all emails.
  • We want to discard emails with attachments that are destined for outside of our domain (i.e. test-example.com), but allow email with attachments for inside our domain.
  • We are using a CentOS 6 machine (works on Fedora, RHEL as well).
  • sudo is configured for the user or we are executing commands as root user.

Install Procmail

sudo yum -y install procmail

Set ZCS Transport Map

  • Login as zimbra user.
su - zimbra
  • Check the currently configured transport map for postfix:
cat /opt/zimbra/postfix/conf/main.cf | grep transport_maps

The output should look like:

transport_maps = proxy:ldap:/opt/zimbra/conf/ldap-transport.cf
  • Add the transport file to the zimbra configuration.
zmlocalconfig -e postfix_transport_maps=' hash:/opt/zimbra/postfix/conf/transport,proxy:ldap:/opt/zimbra/conf/ldap-transport.cf'
Note that the second part of the value of the postfix_transport_maps key (i.e. proxy:ldap:/opt/zimbra/conf/ldap-transport.cf) is the same as the output of the previous step/command.
  • Add procmail pipe to the transport file (requires to edit the file as root user).
su -
vim /opt/zimbra/postfix/conf/transport

Add the following content to the file:

*    procmail-pipe
The above configuration means to configure the pipe of every mail (hence *). For other configuration sets, please see Postfix Manualfor details.
  • Create the db file for the transport map.
su - zimbra
postmap /opt/zimbra/postfix/conf/transport
  • For the pipe to work, a corresponding set of configuration needs to be done in the postfix’s master.cf file. In this case master.cf.in file.
vim /opt/zimbra/postfix/conf/master.cf.in

Add the following text at the end of the file:

procmail-pipe    unix    -    n    n    -    -    pipe
flags=    user=zimbra    argv=/usr/bin/procmail /opt/zimbra/procmail/procmailrc
Note that the first line does not start with white space, the second line should be just below the first line and should start with a whitespace.

Create the Procmail Recipe

  • Create the Procmail recipe file (i.e. /opt/zimbra/procmail/procmailrc file that we have configured in the previous step).
mkdir /opt/zimbra/procmail/
cd /opt/zimbra/procmail/
touch procmailrc
chown -R zimbra:zimbra .
vim procmailrc

Add the following content to the file:

SHELL = /bin/sh
PATH=$HOME/bin:/usr/bin:/bin:.
MAILDIR=/opt/zimbra/procmail/ # You better make sure it exists
DEFAULT=$MAILDIR
LOGFILE=.procmaillog
LOCKFILE=.procmaillock
VERBOSE=yes

:0
* !To:.*test-example\.com.*
* ! ^Content-Type:.*text/plain.*
{
:0B
* ^(Content-(Type|Disposition):.*|[ ]*(file)?)name=("[^"]*|[^]*)\..*
/dev/null
}
You can add your own procmail recipe in the procmailrc file depending upon what you want procmail to do. In this case we are discarding outgoing mail with attachment destined for users outside our domain.
  • Restart Postfix.
zmmtactl stop
zmmtactl start

Thanks to the Zimbra forum entry and Zimbra Wiki on Configuring Postfix to work with piped scripts from where I adapted my installation and hence wrote this guide.

Further Reading

Discussion

Trackbacks/Pingbacks

  1. […] not sure of what went wrong in the previous attempt. Now that I've jotted down the configuration at How to use Procmail with ZCS | asingh. Thanks for you […]

Post a Comment