Sample code for 30+ languages & platforms
DataFlex

Unpack an HTML Email to Disk for Web Display

See more Email Object Examples

Demonstrates the Chilkat Email.AspUnpack method, which unpacks an HTML email's body and its related files (images and style sheets) to the filesystem and rewrites the HTML so those parts load as ordinary files. The arguments are prefix (prepended to saved filenames), saveDir (the filesystem directory where files are written), urlPath (the URL base substituted into the rewritten HTML), and cleanFiles (whether to remove previously unpacked files first). Despite the Asp in its name — a historical artifact — it is not limited to classic ASP; it works with any server-side technology (PHP, ASP.NET, Node, Java, Python, etc.). This example loads an HTML email and unpacks it.

Background: An HTML email keeps its images and style sheets inside the message as multipart/related parts referenced by cid: URLs — which a browser cannot fetch directly. "Unpacking" writes each part out as a real file and rewrites the cid: references into normal URLs or paths, making the message displayable outside a mail client. Common reasons to do this include:

  • Web display: showing a received message in a webmail or web app, where the browser loads the saved assets from a URL (this is what AspUnpack targets, keeping the on-disk saveDir separate from the web-facing urlPath).
  • Archiving: storing an email as a self-contained, browsable HTML folder for long-term records or compliance, viewable years later without the original mail client.
  • Embedded viewers: loading the unpacked HTML into a desktop app's built-in browser / WebView control, which renders local files.
  • Format conversion: feeding the HTML to an HTML-to-PDF (or image) renderer, which needs the images and style sheets present as local files.

For purely local viewing with relative paths (rather than a web urlPath), see the companion method UnpackHtml.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoEmail
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the AspUnpack method, which unpacks an HTML email's body and its related
    //  files (images, style sheets) to the filesystem and rewrites the HTML so it can be
    //  displayed in a web application.  Despite the "Asp" name (historical), it is not limited
    //  to classic ASP -- it applies to any server-side technology that serves the HTML.
    //  Arguments: prefix, saveDir, urlPath, cleanFiles.

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End

    Get ComLoadEml Of hoEmail "qa_data/eml/html_with_images.eml" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Unpack the HTML for web display.  "prefix" is prepended to the saved filenames, "saveDir"
    //  is the filesystem directory where files are written, "urlPath" is the URL base used in
    //  the rewritten HTML, and cleanFiles=true removes previously unpacked files first.
    Get ComAspUnpack Of hoEmail "msg" "qa_output/unpacked" "/unpacked" True To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Unpacked the HTML email to disk for web display."

    //  Note: Paths such as "qa_data/..." and "qa_output/..." are relative local
    //  filesystem paths, relative to the current working directory of the running application.


End_Procedure