Question

I can't find any documentation about reading email attachments. Can anyone suggest something?

I am using Zend\Mail\Storage\Imap to read my emails text, but I cant find any methods to get attachments.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top