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

PHP ActiveX
<?php

$success = 0;

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

$email = new COM("Chilkat.Email");
$success = $email->LoadEml('with-attachments.eml');
if ($success != 1) {
    print $email->LastErrorText . "\n";
    exit;
}

$preview = new COM("Chilkat.Email");
$success = $email->MakeCopy($preview);
if ($success != 1) {
    print $email->LastErrorText . "\n";
    exit;
}

//  Keep related items: the HTML still refers to them.
$preview->DropAttachments();
print 'Files in original: ' . $email->NumAttachments . '  Files in preview: ' . $preview->NumAttachments . "\n";
if ($preview->HasHtmlBody() != 1) {
    print 'This message has no HTML preview.' . "\n";
    exit;
}

$html = new COM("Chilkat.StringBuilder");
$success = $preview->GetHtmlBodySb(1,$html);
if ($success != 1) {
    print $preview->LastErrorText . "\n";
    exit;
}

$success = $html->WriteFile('preview.html','utf-8',0);
if ($success != 1) {
    print 'Failed: html.WriteFile.' . "\n";
    exit;
}

print 'Saved preview.html.' . "\n";

?>