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

CkPhp
<?php

include("chilkat.php");

$success = false;

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

$email = new CkEmail();
$success = $email->LoadEml('with-attachments.eml');
if ($success != true) {
    print $email->lastErrorText() . "\n";
    exit;
}

$filename = new CkStringBuilder();
$mediaType = new CkStringBuilder();
$found = false;
$i = 0;
while (($i < $email->get_NumAttachments())) {
    $filename->SetString($email->getAttachmentFilename($i));
    $mediaType->SetString($email->getAttachmentContentType($i));
    if (($filename->ContentsEqual('agenda.csv',false) == true) and ($mediaType->ContentsEqual('text/csv',false) == true) and ($found == false)) {
        $data = new CkBinData();
        $success = $email->GetAttachmentBd($i,$data);
        if ($success != true) {
            print $email->lastErrorText() . "\n";
            exit;
        }

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

        print 'Saved attachment: ' . $data->get_NumBytes() . ' bytes.' . "\n";
        $found = true;
    }

    $i = $i + 1;
}

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

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


?>