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

Perl
use strict;
use warnings;
use Chilkat2;

# Assumes Chilkat is unlocked. See Global Unlock Sample.

my $email = Chilkat2::Email->new;
my $success = $email->LoadEml('with-attachments.eml');
if (!$success) {
    print $email->LastErrorText, "\n";
    exit;
}

my $preview = Chilkat2::Email->new;
$success = $email->MakeCopy($preview);
if (!$success) {
    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()) {
    print "This message has no HTML preview.\n";
    exit;
}

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

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

print "Saved preview.html.\n";