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

PHP
<?php

use Chilkat2\BinData;
use Chilkat2\Email;

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

$mimeBytes = new BinData();
try {
    $mimeBytes->LoadFile('with-attachments.eml');
} catch (\Chilkat2\Exception $e) {
    echo "Failed: mimeBytes.LoadFile.\n";
    exit;
}

try {
    $mimeBytes->WriteFile('received-original.eml');
} catch (\Chilkat2\Exception $e) {
    echo "Failed: mimeBytes.WriteFile.\n";
    exit;
}

$email = new Email();
try {
    $email->SetFromMimeBd($mimeBytes);

    $email->AddHeaderField('X-Workflow-State', 'reviewed');
    // GetMimeBd appends to its destination. Use a fresh buffer for this message.
    $rendered = new BinData();
    $email->GetMimeBd($rendered);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

try {
    $rendered->WriteFile('reviewed.eml');
} catch (\Chilkat2\Exception $e) {
    echo "Failed: rendered.WriteFile.\n";
    exit;
}

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