example for ver 0.1 --- * maildir format only * utf-8 as default in whole script (sendmail message, DB queries) * TODO: possibility of combining the filter rules * TODO: maybe HTML format e-mail with sendmail, maybe e-mails from external files --- postfix configuration: 'main.cf' (somewhere) ssqllda_destination_recipient_limit = 1 virtual_transport = ssqllda 'master.cf' (on the bottom) ssqllda unix - n n - - pipe flags=q user=vmails:vmails argv=/usr/sbin/ssqllda.pl ${user} ${domain} ${sender} 'user=' - use your virtual user 'argv=' - put script to your favorite directory When you are ready with config files, reload postfix and do simple test by sending e-mail. (do not add any tables) If all is ok, you should see something like this in 'mail.log': [..] Dec 1 13:43:49 tweety postfix/cleanup[4971]: CDE8DC839A: message-id=<201112011343.49675.jane@doe.org> Dec 1 13:43:49 tweety postfix/qmgr[4637]: CDE8DC839A: from=, size=1253, nrcpt=1 (queue active) Dec 1 13:43:49 tweety postfix/smtpd[4965]: disconnect from doe.org[6.6.6.6] Dec 1 13:43:50 tweety postfix/pipe[4972]: CDE8DC839A: to=, relay=ssqllda, delay=1.2, delays=1.1/0/0/0.15, dsn=2.0.0, status=sent (delivered via ssqllda service) Dec 1 13:43:50 tweety postfix/qmgr[4637]: CDE8DC839A: removed [..] --- database name, for example: 'postifx_lda' (name it as you want) database structure, one table one e-mail address: mysql> describe `tomekk@my_domain.org`; +------------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------+------+-----+---------+----------------+ | id | int(2) | NO | PRI | NULL | auto_increment | | type | varchar(16) | NO | | NULL | | | header | varchar(256) | NO | | NULL | | | pattern | varchar(512) | NO | | NULL | | | action | varchar(8) | NO | | NULL | | | action_opt | varchar(8) | NO | | NULL | | | message | varchar(1024) | NO | | NULL | | | subject | varchar(256) | NO | | NULL | | | maildir | varchar(512) | NO | | NULL | | +------------+---------------+------+-----+---------+----------------+ 9 rows in set (0.00 sec) fields: 'id' - id with auto increment option 'type' - matching source: 'sender' (e-mail address of the sender), 'subject' (incomming e-maill subject), 'header' (header of the e-mail message) 'header' - name of the header field, for example: 'X-Bogosity' 'pattern' - regex pattern for the filter rule (PCRE - $_ =~ m/$sql_pattern/) 'action' - what to do if pattern match: 'respond' (send message via sendmail to the sender), 'move' (move e-mail file to the specified or default directory) 'action_opt' - parameters for the 'respond' action: 'save' (save e-mail and send respond), 'nosave' (just send respond without saving e-mail) 'message' - plain text message for the 'respond' action 'subject' - plain text subject for the 'respond' action 'maildir' - directory name for the 'move' action Order of the filter rules is important. Remember to escape any special characters in pattern field - script won't do it for you. --- examples: When the table with our e-mail address doesn't exist, script tries to deliver e-mail to the default maildir directory ('new' as default), same situation when the table exists but is empty. Oke, let's make some simple rules. Text respond without saving e-mail in maildir, sender's address matches the pattern: mysql> select * from `tomekk@my_domain.org`; +----+--------+--------+--------------+---------+------------+-----------------+----------+---------+ | id | type | header | pattern | action | action_opt | message | subject | maildir | +----+--------+--------+--------------+---------+------------+-----------------+----------+---------+ | 24 | sender | | jane@doe.org | respond | nosave | Hello, go away! | Bastard. | | +----+--------+--------+--------------+---------+------------+-----------------+----------+---------+ 1 row in set (0.00 sec) Text respond without saving e-mail in maildir, when incomming e-mail has 'test' string in subject. mysql> select * from `tomekk@my_domain.org`; +----+---------+--------+---------+---------+------------+-----------------+----------+---------+ | id | type | header | pattern | action | action_opt | message | subject | maildir | +----+---------+--------+---------+---------+------------+-----------------+----------+---------+ | 24 | subject | | test | respond | nosave | Hello, go away! | Bastard. | | +----+---------+--------+---------+---------+------------+-----------------+----------+---------+ 1 row in set (0.00 sec) Move e-mail message to the 'trash' when header pattern matches (example e-mail header field: X-Bogosity: Unsure, tests=bogofilter, spamicity=0.499999, version=1.1.7) mysql> select * from `tomekk@my_domain.org`; +----+--------+------------+---------+--------+------------+---------+---------+-----------------------------------------+ | id | type | header | pattern | action | action_opt | message | subject | maildir | +----+--------+------------+---------+--------+------------+---------+---------+-----------------------------------------+ | 24 | header | X-Bogosity | unsure | move | | | | /home/vmails/my_domain.org/tomekk/trash | +----+--------+------------+---------+--------+------------+---------+---------+-----------------------------------------+ 1 row in set (0.00 sec) (unsure - ;D) Text respond with saving e-mail in default maildir directory and moving e-mail to the second, custom directory. mysql> select * from `tomekk@my_domain.org`; +----+--------+--------+--------------+---------+------------+---------+---------+-----------------------------------------+ | id | type | header | pattern | action | action_opt | message | subject | maildir | +----+--------+--------+--------------+---------+------------+---------+---------+-----------------------------------------+ | 24 | sender | | jane@doe.org | respond | save | Hello. | Hello. | | | 25 | sender | | jane@doe.org | move | | | | /home/vmails/my_domain.org/tomekk/mydir | +----+--------+--------+--------------+---------+------------+---------+---------+-----------------------------------------+ 2 rows in set (0.00 sec)