go-spamcheck package

Because there was no Golang package for Postmark’s spam API I decided to build one that can be easily used.

Example Usage

package main
import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	spamcheck "github.com/psyb0t/go-spamcheck"
)
func main() {
	rawEmail := `RAW EMAIL`
	result, err := spamcheck.Check(context.Background(), rawEmail, spamcheck.ReportLong)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Score:", result.Score)
	rules, _ := json.MarshalIndent(result.Rules, "", "  ")
	fmt.Println("Rules:\n", string(rules))
	fmt.Println("Report:\n", result.Report)
}

Two things worth knowing since the v1.0.0 rewrite. The import is the root package now, github.com/psyb0t/go-spamcheck, not the old /spamcheck subpackage. And Check takes a context.Context and returns an error instead of panicking, so you handle failures like any other Go call. The third argument picks how much comes back: ReportShort for just the score, ReportLong (used above) for the score plus the matched-rule breakdown and the full report. For repeated calls, build a client once with spamcheck.New(), optionally with spamcheck.WithBaseURL(...) or spamcheck.WithHTTPDoer(...), and reuse it.

Well that’s simple, right? With ReportLong this will have the following output format:

Score: 3.9
Rules:
 [
  {
    "score": "0.0",
    "description": "ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: xxxpersonals.com]"
  },
  {
    "score": "0.8",
    "description": "BODY: Bayes spam probability is 40 to 60% [score: 0.5000]"
  },
  {
    "score": "0.0",
    "description": "BODY: Test for Invalidly Named or Formatted Colors in HTML"
  },
  {
    "score": "0.8",
    "description": "BODY: HTML and text parts are different"
  },
  {
    "score": "0.7",
    "description": "BODY: Message only has text/html MIME parts"
  },
  {
    "score": "0.0",
    "description": "BODY: HTML included in message"
  },
  {
    "score": "0.1",
    "description": "Message has a DKIM or DK signature, not necessarily valid"
  },
  {
    "score": "0.0",
    "description": "Multipart message only has text/html MIME parts"
  },
  {
    "score": "0.1",
    "description": "DKIM or DK signature exists, but is not valid"
  },
  {
    "score": "1.4",
    "description": "Missing Date: header"
  }
]
Report:
  pts rule                    description
---- ----------------------  --------------------------------------------------
 0.0 URIBL_BLOCKED           ADMINISTRATOR NOTICE: The query to URIBL was
                             blocked. See
                             http://wiki.apache.org/spamassassin/DnsBlocklists…
                             #dnsbl-block for more information. [URIs:
                             xxxpersonals.com]
 0.8 BAYES_50                BODY: Bayes spam probability is 40 to 60% [score:
                             0.5000]
 0.0 T_KAM_HTML_FONT_INVALID BODY: Test for Invalidly Named or Formatted Colors
                             in HTML
 0.8 MPART_ALT_DIFF          BODY: HTML and text parts are different
 0.7 MIME_HTML_ONLY          BODY: Message only has text/html MIME parts
 0.0 HTML_MESSAGE            BODY: HTML included in message
 0.1 DKIM_SIGNED             Message has a DKIM or DK signature, not
                             necessarily valid
 0.0 MIME_HTML_ONLY_MULTI    Multipart message only has text/html MIME parts
 0.1 DKIM_INVALID            DKIM or DK signature exists, but is not valid
 1.4 MISSING_DATE            Missing Date: header

You can check out the package on my github: https://github.com/psyb0t/go-spamcheck