Following the qmail threads in this blog, and after a successful experience filtering emails in the server with a php script! time was upon to thinker a bit with the elder of all email server filters, Procmail – the mail processing utility for Unix.
Whe are talking really old stuff, as Wikipedia states the initial release in 1990, so 26 years from this writing, and about a zillion years in computer time (a date so old that it’s closer to the Unix Epoch than it is of today).
As usual, the easy part in FreeBSD is the installation:
cd /usr/ports/mail/procmail make install
there. Now the tricky part, that is to make it play nicely with Qmail+Vpopmail setup. For the first experiences you probably should setup a couple of test accounts.
The concept is pretty simple, for an account that you want to filter email with Procmail we are going to add/or change the .qmail file that controls the email delivery to a filter script that invokes procmail and throw back a proper qmail exit code according to Procmail result.
It is very important to take into account that in this setup Procmail DOES NOT deliver the email directly, it filters the email, and according with the recipes rules, it can stop the delivery chain, forward the email, invoke an external command, etc.
Without further delays. The customized .qmail that calls the filter script:
| /home/vpopmail/domains/mydomain.com/teste/procmail_filter /usr/home/vpopmail/domains/mydomain.com/teste/Maildir/
this is pretty simple and standard stuff. The qmail-local parses the .qmail file. The line with a pipe means to feed the message to the specified program. The command is invoked by qmail-command that runs sh -c command in the home directory, makes the email messsage available on standard input and setups some environment variables.
For this thread the most important stuff are the exit codes:
Command’s exit codes are interpreted as follows:
0 means that the delivery was successful;
99 means that the delivery was successful, but that qmail-local should ignore all further delivery instructions;
100 means that the delivery failed permanently (hard error);
111 means that the delivery failed but should be tried again in a little while (soft error).
Currently 64, 65, 70, 76, 77, 78, and 112 are considered hard errors, and all other codes are considered soft errors, but command should avoid relying on this.
With this info it’s pretty straight forward to devise the procmail_filter script.
#!/bin/sh /var/qmail/bin/preline /usr/local/bin/procmail ./.procmailrc status=$? if [ $status -eq 99 ] then status=99 elif [ $status -eq 0 ] then status=0 elif [ $status -le 77 ] then status=111 else status=100 fi exit $status
and mark it executable.
The first line (after the shebang) the script calls qmail preline program, it simply inserts at the top of each message a UUCP-style From_ line, a Return-Path line, and a Delivered-To line because Procmail doesn’t understand the qmail environment variables. Calls procmail and sets Procmail configuration file the .procmailrc in the same directory.
Now for the .procmailrc stripped down to a very simple example:
SHELL = /bin/sh LOGFILE=./pm.log LOG=" " VERBOSE=yes # Exitcodes # 0 normal delivery # 99 silent discard a message # 100 bounce a message # Recipes :0 * ^From: email@domain.com { EXITCODE=99 :0 | /usr/local/libexec/dovecot/deliver -d delivertothis@email.com } # Avoid duplicates :0 /dev/null
The top lines are the configuration variables, I would strongly suggest to use a log file for testing, in this example called pm.log that lives in the same directory. The strange LOG directive simple adds a new line to each log entry.
Then the recipes, in this example we match all emails from email@domain.com and deliver it to a local email delivertothis@email.com using Dovecot deliver command (so it takes care of Maildir quota), and set exitcode 99 to discard the message. Exit code 99 means that the delivery was successful, so all .qmail further delivery instructions will be ignored.
We could simply put an email address for a external email address, or even a local address but with less efficiency as it will trigger a new delivery process. This is auto-magical thanks to FreeBSD mailer.conf wrapper.
:0 * ^From: email@domain.com { EXITCODE=99 :0 delivertothis@email.com }
The last lines avoid duplicates, Procmail /dev/nulls the message and gives back the delivery control to the .qmail flow.
That’s it, everything playing nice with each other in old good UNIX tradition.