I have found a way to save all attachments:
$attachment_files = array();
foreach (new \RecursiveIteratorIterator($message) as $part) {
try {
if (strtok($part->contentType, ';') == 'text/plain' ||
strtok($part->contentType, ';') == 'text/html') {
$foundPart = $part;
} else {
$filename = $part->getHeaderField('Content-Description');
$extention = pathinfo($filename, PATHINFO_EXTENSION);
$new_filename = ROOT_PATH . '/data/tmp/' . uniqid() . '.' . $extention;
$content = base64_decode($part->getContent());
$fh = fopen($new_filename, 'w');
fwrite($fh, $content);
fclose($fh);
$attachment_files[] = $new_filename;
}
}
catch (\Exception $e) {
// ignore
}
}
File name is put in Content-Description
header and I am saving all parts which are not text.