Perl filter script

From: Chris Wik (
Date: Sat 17 Apr 2004 - 14:56:30 GMT

  • Next message: Alan Munday: "RE: Perl filter script"

    Dear List,

    Pardon me if this is not an anomy specific question, but I thought
    this would probably be the best place to ask for assistance of this

    I've written a Perl script which calls Anomy, and SpamAssassin. I'm
    invoking this script from Postfix with only one argument, the "to"
    address. I'm reading my mail message from STDIN, as the standard
    Anomy script does. The script runs as the user "filter", as
    does spamd. In postfix's, "mydomain" is set to ""

    My problem is this: when my script passes the message back to Postfix
    with the sendmail command, the message appears to be coming from
    "". How can I make the message appear to come from a
    local user, as it does when using the script?

    Apr 17 04:43:54 mx3 postfix/pipe[21399]: 7F0F113A74A:
    to=<>, relay=filter, delay=4, status=sent (dummy)
    Apr 17 04:43:54 mx3 postfix/qmgr[12225]: 2234E13A74F:
    from=<>, size=3091, nrcpt=1 (queue active)
    Apr 17 04:43:54 mx3 postfix/smtp[21415]: 2234E13A74F:
    to=<>,[], delay=0,
    status=bounced (host[] said: 550 5.1.1
    user not known (in reply to MAIL FROM command))

    My mail server is bouncing this message because "" did
    not exist, not because "" does not exist. If I create a
    user "filter" in my domain, whose login is disabled and mail
    goes to /dev/null, then the script works.

    Comments, suggestions welcome!



    # define paths
    $spamc = '/usr/bin/spamc -f';
    $sendmail = '/usr/sbin/sendmail -i';
    $workdir = '/var/spool/filter/';

    # anomy setup
    $anomy = '/usr/local/anomy/bin/';
    $anomyconf = '/usr/local/anomy/sanitizer.cfg';
    $anomylog = '/var/log/anomy.log';
    $ENV{'ANOMY'} = '/usr/local/anomy';

    # define script variables
    $to = $ARGV[0];
    $message = '';
    $spamc = $spamc . ' -u "' . $to . '"';
    $parsed_file = $workdir . 'parsed' . $$;
    $anomy_file = $workdir . 'anomy' . $$;
    $delete_msg = "no";
    $spam_score = '0';
    $spam_level = '';

    # read in message
    while(<STDIN>) { $message .= $_ }

    # pass message to spamc for processing with spamd
    # output redirected to file $parsed_file
    open(SPAMC, "|$spamc > $parsed_file") or die "Could not open spamc
    for piping: $!";
    print SPAMC "$message";
    close SPAMC;

    # run the messsage through Anomy sanitizer script
    `cat $parsed_file | $anomy $anomyconf 2>>$anomylog > $anomy_file`;

    # read in parsed message to determine if we need to take further action
    open(PMSG, "$anomy_file") or die "Could not open parsed message: $!";
    while(<PMSG>) {
            if(/^X-Spam-Delete: /) { $delete_msg = substr($_, 15) }
            if(/^X-Spam-Level: /) { $spam_level = substr($_, 15) }
            if(/^X-Spam-Redirect: /) { $to = substr($_, 17) }
    # find out how many stars (hits) the message has
    $spam_score = length $spam_level;

    if(($delete_msg eq 'no') or ($delete_msg <= $spam_score)) {
            # pass on parsed message to sendmail
            system("cat $anomy_file | $sendmail $to");

    Chris Wik
    Systems Admin
    ANU Internet Services

    hosted by