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

Read text and binary MIME parts by content type

See more Email Object Examples

Use the same wildcard and inclusion flags for both counting and retrieval. Request decoded text for textual parts and BinData for binary parts. Use GetAttachedEmail when the goal is to inspect a nested email as a message.

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('workshop.eml');
if ($success != 1) {
    print $email->LastErrorText . "\n";
    exit;
}

$types = '*/*';
$inlineOnly = 0;
$excludeAttachments = 0;
$count = $email->GetNumPartsOfType($types,$inlineOnly,$excludeAttachments);
$mediaType = new COM("Chilkat.StringBuilder");
$i = 0;
while (($i < $count)) {
    $mediaType->SetString($email->getNthContentType($i,$types,$inlineOnly,$excludeAttachments));
    if ($mediaType->StartsWith('text/',0) == 1) {
        $partText = $email->getNthTextPartOfType($i,$types,$inlineOnly,$excludeAttachments);
        if ($email->LastMethodSuccess != 1) {
            print $email->LastErrorText . "\n";
            exit;
        }

        print $mediaType->getAsString() . '  ' . $partText . "\n";
    }
    else {
        $data = new COM("Chilkat.BinData");
        $success = $email->GetNthBinaryPartOfTypeBd($i,$types,$inlineOnly,$excludeAttachments,$data);
        if ($success != 1) {
            print $email->LastErrorText . "\n";
            exit;
        }

        print $mediaType->getAsString() . '  ' . $data->NumBytes . ' bytes' . "\n";
    }

    $i = $i + 1;
}


?>