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.
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
AspUnpacktargets, keeping the on-disksaveDirseparate from the web-facingurlPath). - 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 PHP ActiveX Downloads
<?php
$success = 0;
// 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.
$email = new COM("Chilkat.Email");
$success = $email->LoadEml('qa_data/eml/html_with_images.eml');
if ($success == 0) {
print $email->LastErrorText . "\n";
exit;
}
// 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.
$success = $email->AspUnpack('msg','qa_output/unpacked','/unpacked',1);
if ($success == 0) {
print $email->LastErrorText . "\n";
exit;
}
print 'Unpacked the HTML email to disk for web display.' . "\n";
// Note: Paths such as "qa_data/..." and "qa_output/..." are relative local
// filesystem paths, relative to the current working directory of the running application.
?>