Sample code for 30+ languages & platforms
Perl Requires Chilkat v11.0.0+

Preserve the original email and save a modified copy

See more Email Object Examples

Use with-attachments.eml as a stand-in for bytes from an IMAP fetch or a message queue. Preserve those bytes first. Parse them into an Email object, add a local processing header, and serialize the result into a separate buffer. The archived input remains suitable for later byte-level inspection.

Chilkat Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

# Assumes Chilkat is unlocked. See Global Unlock Sample.

my $mimeBytes = Chilkat2::BinData->new;
my $success = $mimeBytes->LoadFile('with-attachments.eml');
if (!$success) {
    print "Failed: mimeBytes.LoadFile.\n";
    exit;
}

$success = $mimeBytes->WriteFile('received-original.eml');
if (!$success) {
    print "Failed: mimeBytes.WriteFile.\n";
    exit;
}

my $email = Chilkat2::Email->new;
$success = $email->SetFromMimeBd($mimeBytes);
if (!$success) {
    print $email->LastErrorText, "\n";
    exit;
}

$email->AddHeaderField('X-Workflow-State', 'reviewed');
# GetMimeBd appends to its destination. Use a fresh buffer for this message.
my $rendered = Chilkat2::BinData->new;
$success = $email->GetMimeBd($rendered);
if (!$success) {
    print $email->LastErrorText, "\n";
    exit;
}

$success = $rendered->WriteFile('reviewed.eml');
if (!$success) {
    print "Failed: rendered.WriteFile.\n";
    exit;
}

print "Archived received-original.eml and saved reviewed.eml.\n";