Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

Create an HTML preview with embedded images

See more Email Object Examples

Make an independent copy of with-attachments.eml, remove ordinary file attachments from the copy, and export its HTML with related images as data URLs. The original retains its attachments. Use a sanitized viewer when showing untrusted HTML.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    let email = CkoEmail()!
    success = email.loadEml(mimePath: "with-attachments.eml")
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    let preview = CkoEmail()!
    success = email.makeCopy(destEmail: preview)
    if success != true {
        print("\(email.lastErrorText!)")
        return
    }

    //  Keep related items: the HTML still refers to them.
    preview.dropAttachments()
    print("Files in original: \(email.numAttachments.intValue)  Files in preview: \(preview.numAttachments.intValue)")
    if preview.hasHtmlBody() != true {
        print("This message has no HTML preview.")
        return
    }

    let html = CkoStringBuilder()!
    success = preview.getHtmlBodySb(inlineImageData: true, sb: html)
    if success != true {
        print("\(preview.lastErrorText!)")
        return
    }

    success = html.writeFile(path: "preview.html", charset: "utf-8", emitBom: false)
    if success != true {
        print("Failed: html.WriteFile.")
        return
    }

    print("Saved preview.html.")

}