Next: , Up: Interfaces to Third-Party Programs   [Contents][Index]

5.31.1 SpamAssassin

Built-in Function: boolean spamc (number msg, string url, number prec, number command)

Send the message msgt to the SpamAssassin daemon (spamd) listening on the given url. The command argument identifies what kind of processing is needed for the message. Allowed values are:


Process the message and return 1 or 0 depending on whether it is diagnosed as spam or not. Store SpamAssassin keywords in the global variable sa_keywords (see below).


Process the message and return 1 or 0 depending on whether it is diagnosed as spam or not. Store entire SpamAssassin report in the global variable sa_keywords.


Learn the supplied message as spam.


Learn the supplied message as ham.


Forget any prior classification of the message.

The second argument, prec, gives the precision, in decimal digits, to be used when converting SpamAssassin diagnostic data and storing them into mailfromd variables.

The floating point SpamAssassin data are converted to the integer mailfromd variables using the following relation:

var = int(sa-var * 10**prec)

where sa-var stands for the SpamAssassin value and var stands for the corresponding mailfromd one. int() means taking the integer part and ‘**’ denotes the exponentiation operator.

The function returns additional information via the following variables:


The spam score, converted to integer as described above. To convert it to a floating-point representation, use sa_format_score function (see sa_format_score). See also the example below.


The threshold, converted to integer form.


If command is ‘SA_SYMBOLS’, this variable contains a string of comma-separated SpamAssassin keywords identifying this message, e.g.:


If command is ‘SA_REPORT’, the value of this variable is a spam report message. It is a multi-line textual message, containing detailed description of spam scores in a tabular form. It consists of the following parts:

  1. A preamble.
  2. Content preview.

    The words ‘Content preview’, followed by a colon and an excerpt of the message body.

  3. Content analysis details.

    It has the following form:

    Content analysis details: (score points, max required)

    where score and max are spam score and threshold in floating point.

  4. Score table.

    The score table is formatted in three columns:


    The score, as a floating point number with one decimal digit.

    rule name

    SpamAssassin rule name that contributed this score.


    Textual description of the rule

    The score table can be extracted from sa_keywords using sa_format_report_header function (see sa_format_report_header), as illustrated in the example below.

The value of this variable is undefined if command is ‘SA_LEARN_SPAM’, ‘SA_LEARN_HAM’ or ‘SA_FORGET’.

The spamc function can signal the following exceptions: e_failure if the connection fails, e_url if the supplied URL is invalid and e_range if the supplied port number is out of the range 1–65535.

An example of using this function:

prog eom
  if spamc(current_message(), "tcp://", 3,
    reject 550 5.7.0
         "Spam detected, score %sa_score with threshold %sa_threshold"

Here is a more advanced example:

prog eom
  set prec 3
  if spamc(current_message(),
           "tcp://", prec, SA_REPORT)
    add "X-Spamd-Status" "SPAM"
    add "X-Spamd-Status" "OK"
  add "X-Spamd-Score" sa_format_score(sa_score, prec)
  add "X-Spamd-Threshold" sa_format_score(sa_threshold, prec)
  add "X-Spamd-Keywords" sa_format_report_header(sa_keywords)
Library Function: boolean sa (string url, number prec; number command)

Additional interface to the spamc function, provided for backward compatibility. It is equivalent to

spamc(current_message(), url, prec, command)

If command is not supplied, ‘SA_SYMBOLS’ is used.

Next: , Up: Interfaces to Third-Party Programs   [Contents][Index]