Sample code for 30+ languages & platforms
PureBasic 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 PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckLoadEml(email,"with-attachments.eml")
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    preview.i = CkEmail::ckCreate()
    If preview.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckMakeCopy(email,preview)
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkEmail::ckDispose(email)
        CkEmail::ckDispose(preview)
        ProcedureReturn
    EndIf

    ;  Keep related items: the HTML still refers to them.
    CkEmail::ckDropAttachments(preview)
    Debug "Files in original: " + Str(CkEmail::ckNumAttachments(email)) + "  Files in preview: " + Str(CkEmail::ckNumAttachments(preview))
    If CkEmail::ckHasHtmlBody(preview) <> 1
        Debug "This message has no HTML preview."
        CkEmail::ckDispose(email)
        CkEmail::ckDispose(preview)
        ProcedureReturn
    EndIf

    html.i = CkStringBuilder::ckCreate()
    If html.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkEmail::ckGetHtmlBodySb(preview,1,html)
    If success <> 1
        Debug CkEmail::ckLastErrorText(preview)
        CkEmail::ckDispose(email)
        CkEmail::ckDispose(preview)
        CkStringBuilder::ckDispose(html)
        ProcedureReturn
    EndIf

    success = CkStringBuilder::ckWriteFile(html,"preview.html","utf-8",0)
    If success <> 1
        Debug "Failed: html.WriteFile."
        CkEmail::ckDispose(email)
        CkEmail::ckDispose(preview)
        CkStringBuilder::ckDispose(html)
        ProcedureReturn
    EndIf

    Debug "Saved preview.html."


    CkEmail::ckDispose(email)
    CkEmail::ckDispose(preview)
    CkStringBuilder::ckDispose(html)


    ProcedureReturn
EndProcedure