Text::SpamAssassin - Detects spamminess of arbitrary text, suitable for wiki and blog defense


version 2.001


    use Text::SpamAssassin;

    my $sa = Text::SpamAssassin->new(
        sa_options => {
            userprefs_filename => '',


    my $result = $sa->analyze;
    print "result: $result->{verdict}\n";


Text::SpamAssassin is a wrapper around Mail::SpamAssassin that makes it easy to check simple blocks of text or HTML for spam content. Its main purpose is to help integrate SpamAssassin into non-mail contexts like blog comments. It works by creating a minimal email message based on the text or HTML you pass it, then handing that email to SpamAssassin for analysis. See "MESSAGE GENERATION" for more details.


    my $sa = Text::SpamAssassin->new(
        sa_options => {
            userprefs_filename => '',

As well as initializing the object the constructor creates a Mail::SpamAssassin object for the actual analysis work. The following options may be passed to the constructor


A hashref. This will be passed as-is to the Mail::SpamAssassin constructor. At the very least you probably want to provide the userprefs_filename as the default configuration isn't particularly well suited to non-mail spam. See "SPAMASSASSIN CONFIGURATION" for details.


By default the Mail::SpamAssassin object will be fully created in the Text::SpamAssassin constructor. This requires it to compile the rulesets and load any modules it needs which can take a little while. If the lazy option is set to a true value, this setup will be deferred until the first scan is done.


All the set_* and reset_* methods return a copy of Text::SpamAssasin object they are invoked on to allow easy call chaining:

    my $result = $sa->reset
                    ->set_metadata("ip", "");


    $sa->set_text("some comment text");

Store some text content and stores it for later analysis. Any content previously set with set_text or set_html will be overwritten.


    $sa->set_html("<p>see <a href='#'>here</a> for more info</p>");

Store some HTML content and stores it for later analysis. Any content previously set with set_text or set_html will be overwritten.


    $sa->set_header("Subject", "your blog is stupid");

Set a header that will be added to the constructed message that gets passed to SpamAssassin. This will override any header of the same name that would normally be generated by Text::SpamAssassin. To set multiple headers with the same name, provide an arrayref as the value instead.


    $sa->set_metadata("ip", "");

Sets metadata related to the text, usually taken from additional fields in a blog comment form. Some of these values are used when constructing the message header for SpamAssassin. When scanning text (but not HTML) this data will also be added to the message body so they can be scanned. Any additional data that you want scanned (such as URLs) should be added here.



Calls reset_headers and reset_headers to reset the object state. You should use this if you have a long-lived Text::SpamAssassin object that will be used multiple times.



Removes any headers previously set with set_header.



Removes any metadata previously set with set_metadata.


    my $result = $sa->analyze;

Scan the previously-supplied data. Returns a hashref containing three values:


One of the following values:


The message was considered to be clean by SpamAssassin.


The message was considered to be spam by SpamAssassin.


The scan failed for an unknown reason.


The score that SpamAssassin gave the message.


The list of rules that SpamAssassin matched when considering the message.


Because SpamAssassin only knows how to scan email messages, its necessary for Text::SpamAssassin to generate a message from the data you provide. This section details how that message is created.

A message body is created from the supplied text or HTML data and the supplied metadata. If text is supplied then the message body contains the data supplied to set_metadata as lines of "key: value", one per line, followed by the supplied message text. If HTML supplied then the body is wrapped in a HTML doctype and header, and the metadata is included as a unordered list.

The header is mostly hardcoded, but the following metadata items will be included if present.


Included in the From: header as the sender name.


Included in the From: header as the sender address.


Used as-is for the Subject: header.


Included in the Received: header as the originating IP.

Sane defaults will be used for any metadata that is not provided.

Additionally, the Content-Type: will be set to either text/plain or text/html depending on the type of message content provided.


By default SpamAssassin is configured in a way that does a good job of detecting spam in email traffic. Many of its rules that work well in that context are unsuitable for use in other scenarios. An example of this is DUN/DUL rulesets that check for known "dial-up" IP networks (such as those used by ISP customers) are almost always useless for something that scans blog comments, as you likely want home users to be able to comment on our blog when you'd never dream of accepting mail from them directly.

For this reason, its highly recommended that you specify an alternate configuration using the userprefs_filename option in sa_options. Sample configuration files can be found in the examples directory of the Text-SpamAssassin distribution.


None known. Please report bugs via the CPAN Request Tracker at


If you find this module useful, please consider rating it on the CPAN Ratings service at

If you like (or hate) this module, please tell the author! Send mail to <>.




Originally by Bob Apthorpe <>

Cleanup for 2.0 and CPAN release by Robert Norris <>


Copyright 2004 by Bob Apthorpe

Copyright 2010 by Robert Norris

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.