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

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

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

$filename = new COM("Chilkat.StringBuilder");
$mediaType = new COM("Chilkat.StringBuilder");
$found = 0;
$i = 0;
while (($i < $email->NumAttachments)) {
    $filename->SetString($email->getAttachmentFilename($i));
    $mediaType->SetString($email->getAttachmentContentType($i));
    if (($filename->ContentsEqual('agenda.csv',0) == 1) and ($mediaType->ContentsEqual('text/csv',0) == 1) and ($found == 0)) {
        $data = new COM("Chilkat.BinData");
        $success = $email->GetAttachmentBd($i,$data);
        if ($success != 1) {
            print $email->LastErrorText . "\n";
            exit;
        }

        $success = $data->WriteFile('received-agenda.csv');
        if ($success != 1) {
            print 'Failed: data.WriteFile.' . "\n";
            exit;
        }

        print 'Saved attachment: ' . $data->NumBytes . ' bytes.' . "\n";
        $found = 1;
    }

    $i = $i + 1;
}

if ($found != 1) {
    print 'No matching attachment.' . "\n";
}

$i = 0;
while (($i < $email->NumRelatedItems)) {
    print 'Related: ' . $email->getRelatedContentType($i) . '  Content-ID: ' . $email->getRelatedContentID($i) . "\n";
    $i = $i + 1;
}


?>