Mikael
Mikael

Reputation: 1319

PHP Horde imap how to fetch

I saw the similar questions, but it has not helped me. I am trying to fetch message. I need full message with all parts, headers, attachments.

$fetchQuery =  new Horde_Imap_Client_Fetch_Query();
$fetchQuery->fullText();


/** @var Horde_Imap_Client_Fetch_Results $mail */
$results = $client->fetch('INBOX', $fetchQuery, ['ids' => new Horde_Imap_Client_Ids(11632)]);

var_dump($results->first()->getEnvelope()->subject);

I tried a lot of variants. But I can't get any info about message. The subject is empty string. I am sure, such mail with that uid exists, I got this uid with Horde also.

Upvotes: 0

Views: 289

Answers (2)

Bimal
Bimal

Reputation: 911

Try the code mentioned below. $results array has all the items you need.

    $uids = new \Horde_Imap_Client_Ids($thread_uids); 

    $query = new \Horde_Imap_Client_Fetch_Query();
    $query->envelope();
    $query->structure();


    $messages = $oClient->fetch($mailbox, $query, array('ids' => $uids));



    $results = [];
    foreach($messages as $message){

    $envelope = $message->getEnvelope();
    $structure = $message->getStructure();


        $msghdr = new StdClass;
        $msghdr->recipients = $envelope->to->bare_addresses;
        $msghdr->senders    = $envelope->from->bare_addresses;
        $msghdr->cc         = $envelope->cc->bare_addresses;
        $msghdr->bcc         = $envelope->bcc->bare_addresses;
        $msghdr->subject    = $envelope->subject;
        $msghdr->timestamp  = $envelope->date->getTimestamp();



        $query = new Horde_Imap_Client_Fetch_Query();
        $query->fullText();

        $typemap = $structure->contentTypeMap();
        foreach ($typemap as $part => $type) {
            // The body of the part - attempt to decode it on the server.
            $query->bodyPart($part, array(
                'decode' => true,
                'peek' => true,
            ));
            $query->bodyPartSize($part);
        }

        $id = new Horde_Imap_Client_Ids($message->getUid());
        $messagedata = $oClient->fetch($mailbox, $query, array('ids' => $id))->first();
        $msgdata = new StdClass;
        $msgdata->id = $id;
        $msgdata->contentplain = '';
        $msgdata->contenthtml  = '';
        $msgdata->attachments  = array(
            'inline'     => array(),
            'attachment' => array(),
        );

        $plainpartid = $structure->findBody('plain');
        $htmlpartid  = $structure->findBody('html');

        foreach ($typemap as $part => $type) {
            // Get the message data from the body part, and combine it with the structure to give a fully-formed output.
            $stream = $messagedata->getBodyPart($part, true);
            $partdata = $structure->getPart($part);
            $partdata->setContents($stream, array('usestream' => true));
            if ($part == $plainpartid) {
                $msgdata->contentplain = $partdata->getContents();
            } else if ($part == $htmlpartid) {
                $msgdata->contenthtml = $partdata->getContents();
            } else if ($filename = $partdata->getName($part)) {
                $disposition = $partdata->getDisposition();
                $disposition = ($disposition == 'inline') ? 'inline' : 'attachment';
                $attachment = new StdClass;
                $attachment->name    = $filename;
                $attachment->type    = $partdata->getType();
                $attachment->content = $partdata->getContents();
                $attachment->size    = strlen($attachment->content);
                $msgdata->attachments[$disposition][] = $attachment;
            }
        }


        $data = [
            'uid' => implode("",$id->ids),
            'from' => implode(",",$msghdr->senders),
            'cc' => implode(",",$msghdr->cc),
            'bcc' => implode(",",$msghdr->bcc),
            'to' => implode(",",$msghdr->recipients),
            'date' => $msghdr->timestamp,
            'subject' => $envelope->subject,
            'hasAttachments' => $structure->getDisposition(),
            'folder' => $mailbox,
            'messageId' =>  $envelope->message_id,
            'attachment' => $msgdata->attachments
        ];

        $data['body'] = empty($msgdata->contenthtml) ? $msgdata->contenttext: $msgdata->contenthtml;    


        array_push($results,$data);



    }

Upvotes: 0

Mikael
Mikael

Reputation: 1319

$fetchQuery =  new Horde_Imap_Client_Fetch_Query();
$fetchQuery->fullText();


/** @var Horde_Imap_Client_Fetch_Results $mail */
$results = $client->fetch('INBOX', $fetchQuery, ['ids' => new Horde_Imap_Client_Ids(11632)]);

var_dump($results->first()->getFullMsg());

Upvotes: -1

Related Questions