[imp] How to download attachment from Mime_Part?

Michael J Rubinsky mrubinsk at horde.org
Thu May 7 14:57:46 UTC 2015


Quoting Kristers Zariņš <sharrpenized at gmail.com>:

> I cannot figure out how to download attachments on my server. Below is the
> code I'm using. getContents() soundes like the method I need, but it doesnt
> return anything:
>
> $Mime_Part = $Data_Fetch->getStructure();
> $Mime_Parts = $Mime_Part->partIterator();
>
> foreach ($Mime_Parts as $p) {
>     if ( $this->isAttachment( $p ) ) {// This actually works. Copied from
> IMP source
>
>         // How do I download attachments and save them on my server here?
>
>         $attachment_1 = $p->getContents(); // Always empty string
>         $attachment_1 = $p->getContents(['stream' => true]); // Always NULL
>         //What now?
>     }
> }
>
> How can I actually download? Thanks.

You need to actually fetch the attachment part from the IMAP server.  
getStructure only contains the MIME structure of the message, not the  
actual contents. So, something like:

$q = new Horde_Imap_Fetch_Query();
$q->bodyPart($mime_id, array(
     'decode' => true,
     'peek' => true
));
$mbox = new Horde_Imap_Client_Mailbox('INBOX');
$ids = new Horde_Imap_Client_Ids(array($imap_message_uid));
$list = $client->fetch($mbox, $q, array('ids' => $ids));
$email = array_pop($list);
$part = $email->getBodyPart($mime_id);

$contents = $part->getContents();

IIRC, you might need to still transfer decode the contents...

if (!$email->getBodyPartDecode($mime_id)) {
     // Quick way to transfer decode contents
     $body->setContents($email->getBodyPart($id));
     $contents = $body->getContents();
}

Horde_ActiveSync_Imap_* (especially _Message and _Adapter) contains a  
whole bunch of imap/mime/fetching type code if you want to look around  
also.

-- 
mike
The Horde Project
http://www.horde.org
https://www.facebook.com/hordeproject
https://www.twitter.com/hordeproject
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5869 bytes
Desc: S/MIME Signature
URL: <http://lists.horde.org/archives/imp/attachments/20150507/0c9e0c0b/attachment.bin>


More information about the imp mailing list