Sample code for 30+ languages & platforms
Swift

Get SpamAssassin Score for an Email

See more Email Object Examples

Uses Postmark’s spam API (a RESTfull interface to the SpamAssassin filter tool) to analyze an email to get a spam score.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // First build an email to check.
    let email = CkoEmail()!
    email.subject = "this is a test"
    email.from = "support@chilkatsoft.com"
    email.add(to: "John Doe", emailAddress: "john@example.com")
    email.addPlainTextAlternativeBody(body: "this is a test")
    email.addHtmlAlternativeBody(body: "<html><body><b>Hello John!</b><p>This is a test</p></body></html>")
    success = email.addFileAttachment2(path: "qa_data/jpg/starfish.jpg", contentType: "image/jpeg")

    // Check this email by implementing this curl command:

    // curl -X POST "https://spamcheck.postmarkapp.com/filter"
    // -H "Accept: application/json"
    // -H "Content-Type: application/json"
    // -v
    // -d '{"email":"raw dump of email", "options":"short"}'

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "email", value: email.getMime())
    json.updateString(jsonPath: "options", value: "short")

    let http = CkoHttp()!
    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: "https://spamcheck.postmarkapp.com/filter", json: json, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("response status code = \(resp.statusCode.intValue)")
    print("response body: ")
    print("\(resp.bodyStr!)")

}