Gentoo Wiki


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Please format this article according to the guidelines and Wikification suggestions, then remove this notice {{Wikify}} from the article

This howto will show you how to setup server-side filtering for virtual users (stored in mysql). It assumes you are starting from having an operational virtual mail system, as outlined at procmail is not able to filter on the virtual domain because it cannot lookup the user information correctly. So we use maildrop (which has nice perl regexp expressions) for filtering mail.


Postfix Changes

First we need make some changes to postfix configuration files,

Code: Postfix Configuration
$EDITOR /etc/postfix/

Change the following to read,

File: /etc/postfix/
# This is what used to be set
#virtual_transport = virtual

# Add these 2 lines so we use the mysql transport table instead
transport_maps = mysql:/etc/postfix/
virtual_transport = $transport_maps

# I added this because I read it in the MAILDROP_README file in postfix doc
# INFO: this is needed so that Postfix will provide one recipient at a time to
# the maildrop delivery agent. maildrop cannot handle more than one recipient at
# a time and if this is not set, mails to multiple recipients are not delivered.
maildrop_destination_recipient_limit = 1

Next edit the file

Code: Postfix Configuration
$EDITOR /etc/postfix/

Fix the path of maildrop to /usr/bin/maildrop instead of /usr/local/bin/maildrop

File: /etc/postfix/
# maildrop. See the Postfix MAILDROP_README file for details.
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}

Maildrop Setup

Code: Postfix Configuration
emerge -DuN maildrop

LittleJo : I am sorry to edit this like that, but this method is deprecated. Maildrop is now completely integrated with authdaemon, even the standalone version of maildrop. As a consequence, the config file is in fact /etc/courier/authlib/authmysqlrc. Of course, you need authlib installed. gaelic: /etc/maildrop/ is definitly not needed. only authmysql.rc as little jo mentioned. the virtual_transport in should be configured as maildrop

Next we need to create the file to tell it how to connect to our database,

# $EDITOR /etc/maildrop/
Note: If a /etc/maildrop/ was created by portage, remove it. If /etc/maildrop/ exists, maildrop will time out (3 minutes) with LDAP before trying MySQL!

Creating and inserting the following:

File: /etc/maildrop/
hostname   localhost
port       3306
socket     /var/run/mysqld/mysqld.sock
database   mailsql
dbuser     mailsql
dbpw       <secret>

dbtable    users

default_uidnumber   1001
default_gidnumber   1001

uid_field              email
uidnumber_field        uid
gidnumber_field        gid
maildir_field          maildir
homedirectory_field    homedir

#quota_field            quota
# if you set this table up according to the howto at 
# you
# will need to somehow pass the quota to maildrop in the 
# format 1000000S or 1000000S,10000C (S for size, C for count).
# Either adjust the table or use something like this
quota_field            concat(quota,'S')

mailstatus_field       postfix 

Make sure to change the database connection information, and the uid/gid of your virtual mail user.

Now if you want any of your virtual domains to use maildrop instead of virtual for delivery just change the transport table's destination field to 'maildrop:' instead of 'virtual:'

Code: Changing to maildrop
mysql> UPDATE users SET transport='maildrop:' WHERE domain='';

Mail Filters

Since all virtual domains are using the vmail account, I set up a /home/vmail/.mailfilter file:

# $EDITOR /home/vmail/.mailfilter

Creating the file and adding the following (LOGNAME gets set to

File: /home/vmail/.mailfilter
xfilter "/usr/bin/spamc"
# Check for user defined filter file
exception {
    include "$HOME/mailfilters/$LOGNAME"

Next we create a directory to store the .mailfilter files of individual users. Then we create a mail filter for a virtual user.

# mkdir -m 0700 /home/vmail/mailfilters
# $EDITOR /home/vmail/mailfilters/
File: /home/vmail/mailfilters/
if (/^X-Spam-Flag:.*YES/)
    exception {
        to $DEFAULT/.spam/
if (/^List-Id:.*gentoo-user/)
    exception {
        to $DEFAULT/.Gentoo.User/
if (/^List-Id:.*gentoo-security/)
    exception {
        to $DEFAULT/.Gentoo.Security/
if (/^List-Id:.*gentoo-announce/)
    exception {
        to $DEFAULT/.Gentoo.Announce/
if (/^List-Id:.*gentoo-gwn/)
    exception {
        to $DEFAULT/.Gentoo.GWN/

Next we set the proper permissions and ownership of the files:

# chmod 600 /home/vmail/.mailfilter
# chmod 600 /home/vmail/mailfilters/
# chown vmail:vmail /home/vmail/.mailfilter
# chown -R vmail:vmail /home/vmail/mailfilters

Versions Used


Retrieved from ""

Last modified: Sun, 25 May 2008 01:28:00 +0000 Hits: 42,703