Mail::SpamAssassin(3pm) | User Contributed Perl Documentation | Mail::SpamAssassin(3pm) |
Mail::SpamAssassin - Spam detector and markup engine
my $spamtest = Mail::SpamAssassin->new(); my $mail = $spamtest->parse($message); my $status = $spamtest->check($mail); if ($status->is_spam()) { $message = $status->rewrite_mail(); } else { ... } ... $status->finish(); $mail->finish(); $spamtest->finish();
Mail::SpamAssassin is a module to identify spam using several methods including text analysis, internet-based realtime blocklists, statistical analysis, and internet-based hashing algorithms.
Using its rule base, it uses a wide range of heuristic tests on mail headers and body text to identify "spam", also known as unsolicited bulk email. Once identified as spam, the mail can then be tagged as spam for later filtering using the user's own mail user agent application or at the mail transfer agent.
If you wish to use a command-line filter tool, try the "spamassassin" or the "spamd"/"spamc" tools provided.
There are also two special cases: (1) if the special case of "info" is passed as a debug facility, then all informational messages are enabled; (2) if the special case of "all" is passed as a debug facility, then all debugging facilities are enabled.
A value of the option can either be a string (a comma-delimited list of tag names), or a reference to a list of individual tag names. A caller may provide the list in advance, specifying his intention to later collect the information through $pms->get_tag() calls. If a name of a tag starts with a 'NO' (case insensitive), it shows that a caller will not be interested in such tag, although there is no guarantee it would save any resources, nor that a tag value will be empty. Currently no built-in tags start with 'NO'. A later entry overrides previous one, e.g. ASN,NOASN,ASN,TIMING,NOASN is equivalent to TIMING,NOASN.
For backward compatibility, all tags available as of version 3.2.4 will be available by default (unless disabled by NOtag), even if not requested through need_tags option. Future versions may provide new tags conditionally available.
Currently the only tag that needs to be explicitly requested is 'TIMING'. Not requesting it can save a millisecond or two - it mostly serves to illustrate the usage of need_tags.
Example:
need_tags => 'TIMING,noLANGUAGES,RELAYCOUNTRY,ASN,noASNCIDR', or:
need_tags => [qw(TIMING noLANGUAGES RELAYCOUNTRY ASN noASNCIDR)],
Prior to version 3.4.0, calling srand() was handled by the SpamAssassin library.
This setting requires the caller to decide when to call srand(). This choice may be desired to preserve the entropy of a PRNG. The default value of skip_prng_reseeding is false to maintain backward compatibility.
This option should only be set by a caller if it calls srand() upon spawning child processes. Unless you are certain you need it, leave this setting as false.
NOTE: The skip_prng_reseeding feature is implemented in spamd as of 3.4.0 which allows spamd to call srand() right after forking a child process.
If none of "rules_filename", "site_rules_filename", "userprefs_filename", or "config_text" is set, the "Mail::SpamAssassin" module will search for the configuration files in the usual installed locations using the below variable definitions which can be passed in.
'__prefix__/etc/mail/spamassassin' '__prefix__/etc/spamassassin'
Defaults to "@@PREFIX@@".
The $parse_now option, by default, is set to false (0). This allows SpamAssassin to not have to generate the tree of internal data nodes if the information is not going to be used. This is handy, for instance, when running "spamassassin -d", which only needs the pristine header and body which is always parsed and stored by this function.
The optional last argument $suppl_attrib provides a way for a caller to pass additional information about a message to SpamAssassin. It is either undef, or a ref to a hash where each key/value pair provides some supplementary attribute of the message, typically information that cannot be deduced from the message itself, or is hard to do so reliably, or would represent unnecessary work for SpamAssassin to obtain it. The argument will be stored to a Mail::SpamAssassin::Message object as 'suppl_attrib', thus made available to the rest of the code as well as to plugins. The exact list of attributes will evolve through time, any unknown attribute should be ignored. Possible examples are: SMTP envelope information, a flag indicating that a message as supplied by a caller was truncated due to size limit, an already verified list of DKIM signature objects, or perhaps a list of rule hits predetermined by a caller, which makes another possible way for a caller to provide meta information (instead of having to insert made-up header fields in order to pass information), or maybe just plain rule hits.
For more information, please see the "Mail::SpamAssassin::Message" and "Mail::SpamAssassin::Message::Node" POD.
Returns a "Mail::SpamAssassin::PerMsgStatus" object which can be used to test or manipulate the mail message.
Note that the "Mail::SpamAssassin" object can be re-used for further messages without affecting this check; in OO terminology, the "Mail::SpamAssassin" object is a "factory". However, if you do this, be sure to call the finish() method on the status objects when you're done with them.
Otherwise identical to check() above.
If $isspam is set, the mail is assumed to be spam, otherwise it will be learnt as non-spam.
If $forget is set, the attributes of the mail will be removed from both the non-spam and spam learning databases.
$id is an optional message-identification string, used internally to tag the message. If it is "undef", the Message-Id of the message will be used. It should be unique to that message.
Returns a "Mail::SpamAssassin::PerMsgLearner" object which can be used to manipulate the learning process for each mail.
Note that the "Mail::SpamAssassin" object can be re-used for further messages without affecting this check; in OO terminology, the "Mail::SpamAssassin" object is a "factory". However, if you do this, be sure to call the finish() method on the learner objects when you're done with them.
learn() and check() can be run using the same factory. init_learner() must be called before using this method.
Note that this should be called after reading any per-user configuration, as that data may override some paths opened in this method. You may pass the following attribute-value pairs:
It will also submit the mail to SpamAssassin's Bayesian learner.
Options is an optional reference to a hash of options. Currently these can be:
It will also submit the mail to SpamAssassin's Bayesian learner as nonspam.
Options is an optional reference to a hash of options. Currently these can be:
Given a string containing an email address, add it to the automatic welcomelist database.
If $cli_p is set then underlying plugin may give visual feedback on additions/failures.
Given a mail message, find as many addresses in the usual headers (To, Cc, From etc.), and the message body, and add them to the automatic welcomelist database.
If $cli_p is set then underlying plugin may give visual feedback on additions/failures.
Given a string containing an email address, remove it from the automatic welcomelist database.
If $cli_p is set then underlying plugin may give visual feedback on additions/failures.
Given a mail message, find as many addresses in the usual headers (To, Cc, From etc.), and the message body, and remove them from the automatic welcomelist database.
If $cli_p is set then underlying plugin may give visual feedback on additions/failures.
Given a string containing an email address, add it to the automatic welcomelist database with a high score, effectively blocklisting them.
If $cli_p is set then underlying plugin may give visual feedback on additions/failures.
Given a mail message, find addresses in the From headers and add them to the automatic welcomelist database with a high score, effectively blocklisting them.
Note that To and Cc addresses are not used.
If $cli_p is set then underlying plugin may give visual feedback on additions/failures.
Note that the $mail object is not modified.
Warning: if the input message in $mail contains a mixture of CR-LF (Windows-style) and LF (UNIX-style) line endings, it will be "canonicalized" to use one or the other consistently throughout.
User preferences are as defined in the "Mail::SpamAssassin::Conf" manual page. In other words, they include scoring options, scores, welcomelists and blocklists, and so on, but do not include rule definitions, privileged settings, etc. unless "allow_user_rules" is enabled; and they never include the administrator settings.
The username in $username will also be used for the "username" attribute of the Mail::SpamAssassin object.
The username in $username will also be used for the "username" attribute of the Mail::SpamAssassin object.
Normally, Mail::SpamAssassin uses lazy evaluation where possible, but if you plan to fork() or start a new perl interpreter thread to process a message, this is suboptimal, as each process/thread will have to perform these actions.
Call this function in the master thread or process to perform the actions straight away, so that the sub-processes will not have to.
If $use_user_prefs is 0, this will initialise the SpamAssassin configuration without reading the per-user configuration file and it will assume that you will call "read_scoreonly_config" at a later point.
If $keep_userstate is true, compile_now() will revert any configuration options which have a default with __userstate__ in it post-init(), and then re-change the option before returning. This lets you change $ENV{'HOME'} to a temp directory, have compile_now() and create any files there as necessary without disturbing the actual files as changed by a configuration option. By default, this is disabled.
(This API was added in SpamAssassin 3.1.1.)
# create object w/ configuration my $spamtest = Mail::SpamAssassin->new( ... ); # backup configuration to %conf_backup my %conf_backup; $spamtest->copy_config(undef, \%conf_backup) || die "config: error returned from copy_config!\n"; ... do stuff, perhaps modify the config, etc ... # reset the configuration back to the original $spamtest->copy_config(\%conf_backup, undef) || die "config: error returned from copy_config!\n";
Note that the contents of the associative arrays should be considered opaque by calling code.
(This API was added in SpamAssassin 3.2.0.)
"HTML::Parser" "Sys::Syslog"
See also <https://spamassassin.apache.org/> and <https://wiki.apache.org/spamassassin/> for more information.
Mail::SpamAssassin::Conf(3) Mail::SpamAssassin::PerMsgStatus(3) spamassassin(1) sa-update(1)
See <https://issues.apache.org/SpamAssassin/>
The SpamAssassin(tm) Project <https://spamassassin.apache.org/>
SpamAssassin is distributed under the Apache License, Version 2.0, as described in the file "LICENSE" included with the distribution.
The latest version of this library is likely to be available from CPAN as well as:
<https://spamassassin.apache.org/>
2024-04-12 | perl v5.38.2 |