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

PHP
<?php

use Chilkat2\Email;
use Chilkat2\StringBuilder;

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

$email = new Email();
try {
    $email->LoadEml('with-attachments.eml');

    $preview = new Email();
    $email->MakeCopy($preview);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

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

$html = new StringBuilder();
try {
    $preview->GetHtmlBodySb(true, $html);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

try {
    $html->WriteFile('preview.html', 'utf-8', false);
} catch (\Chilkat2\Exception $e) {
    echo "Failed: html.WriteFile.\n";
    exit;
}

echo "Saved preview.html.\n";