[Mimedefang] MD w/SpamAssassin: body/subject not rewritten

Sidney Markowitz sidney at sidney.com
Sat Sep 28 20:55:00 EDT 2002


Jeff Powell <jeff at jeffpowell.com> wrote:
> I am using MIMEDefang 2.21 and SpamAssassin 2.41.

MIMEDefang calls a Perl subroutine within SpamAssassin directly to get the score and
the spam report. It does not use SpamAssassin to actually do anything to the headers
or body. If you look at the sample mimedefang-filter you can see where it calls the
SpamAssassin routines, where it checks the score, and where it is getting the spam
report and putting it into an attachment. If you want MIMEDefang to instead rewrite
the subject and output the report some other way you will have to code those actions
yourself in mimedefang-filter. I suggest that you leave the report as an attachment:
MIMEDefang does you a favor by avoiding all the problems you would get having the
spam report put inside of a body if mail is HTML or otherwise MIME encoded. It is
easy to add or change headers.

For reference, here is my personal modified version of filter_end which does not do
exactly what you want but may contain enough hints for you to work with. I'm sure
that my or your mailer will wrap the lines to confuse things:

# If SpamAssassin found SPAM, append report.  We do it as a separate
# attachment of type text/plain
sub filter_end ($) {
    my($entity) = @_;

    # No sense doing any extra work
    return if message_rejected();
        # Don't do spam processing of outgoing messages
        # These are ip addresses I accept mail from xx'd out in this email
        return if ($RelayAddr =~ "^xx\.xx\.xx\.");
        return if ($RelayAddr =~ "^xx\.xx\.xx\.");

    # Spam checks if SpamAssassin is installed
    if ($Features{"SpamAssassin"}) {
        if (-s "./INPUTMSG" < 256*1024) {
            # Only scan messages smaller than 256kB.  Larger messages
            # are extremely unlikely to be spam, and SpamAssassin is
            # dreadfully slow on very large messages.
            my($hits, $req, $names, $report) = spam_assassin_check();
            if ($hits >= $req) {
                # We add a header which looks like this:
                # X-Spam-Level: ******
                # The number of asterisks in parens is the integer part
                # of the spam score clamped to a maximum of 40.
                # MUA filters can easily be written to trigger on a
                # minimum number of asterisks...
                my($score);
                if ($hits < 40) {
                    $score = "*" x int($hits);
                } else {
                    $score = "*" x 40;
                }

                # similar to the SpamAssassin defang option. protect against evil
spam
                my $head = $entity->head;
                my $mt = $head->mime_type;
                if (defined $mt && $mt ne '' && $mt !~ m{text/plain}i) {
                        action_defang($entity, "", "", "text/plain");
                }

                # allow for easy recovery of original message by making a pristine
attachment
                if (open (IN, "<./INPUTMSG")) {
                        my @msg = <IN>;
                        close(IN);
                        unshift @msg, "Received: from $RelayHostname ($RelayHostname
[$RelayAddr])";
                        unshift @msg, "Return-Path: $Sender";
                        action_add_part($entity, "message/rfc822", "-suggest",
                                                        join('', @msg),
                                                        "backup_of_spam.eml",
"attachment");
                }

                action_change_header("X-Spam-Status",
                                     "Yes, hits=$hits required=$req tests=$names
version=$Mail::SpamAssassin::VERSION");
                action_change_header("X-Spam-Flag", "YES");
                action_change_header("X-Spam-Level", "$score");
                action_change_header("X-Spam-Checker-Version",
                                     "SpamAssassin $Mail::SpamAssassin::VERSION ".
                                     "($Mail::SpamAssassin::SUB_VERSION)");

                # If you find the SA report useful, add it, I guess...
                action_add_part($entity, "text/plain", "-suggest",
                                "$report\n",
                                "SpamAssassinReport.txt", "inline");
            } else {
                action_change_header("X-Spam-Status",
                                     "No, hits=$hits required=$req tests=$names
version=$Mail::SpamAssassin::VERSION");
                # remove any spamassassin headers that the sender may have included
                action_delete_header("X-Spam-Flag");
                action_delete_header("X-Spam-Level");
                action_delete_header("X-Spam-Checker-Version");
            }
        }
    }
}




More information about the MIMEDefang mailing list