PHP Requires Chilkat v11.0.0+
PHP
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 Downloads
<?php
use Chilkat2\BinData;
use Chilkat2\Email;
use Chilkat2\StringBuilder;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$email = new Email();
try {
$email->LoadEml('with-attachments.eml');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$filename = new StringBuilder();
$mediaType = new StringBuilder();
$found = false;
$i = 0;
while ($i < $email->NumAttachments) {
$filename->SetString($email->GetAttachmentFilename($i));
$mediaType->SetString($email->GetAttachmentContentType($i));
if (($filename->ContentsEqual('agenda.csv', false)) && (($mediaType->ContentsEqual('text/csv', false)) && (!$found))) {
$data = new BinData();
try {
$email->GetAttachmentBd($i, $data);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
try {
$data->WriteFile('received-agenda.csv');
} catch (\Chilkat2\Exception $e) {
echo "Failed: data.WriteFile.\n";
exit;
}
echo 'Saved attachment: ', $data->NumBytes, " bytes.\n";
$found = true;
}
$i = $i + 1;
}
if (!$found) {
echo "No matching attachment.\n";
}
$i = 0;
while ($i < $email->NumRelatedItems) {
echo 'Related: ', $email->GetRelatedContentType($i), ' Content-ID: ', $email->GetRelatedContentID($i), "\n";
$i = $i + 1;
}