簡體   English   中英

PHP Horde imap 如何獲取

[英]PHP Horde imap how to fetch

我看到了類似的問題,但對我沒有幫助。 我正在嘗試獲取消息。 我需要包含所有部分、標題、附件的完整消息。

$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);

我嘗試了很多變體。 但我無法獲得有關消息的任何信息。 主題是空字符串。 我敢肯定,存在帶有該 uid 的此類郵件,我也通過 Horde 獲得了該 uid。

試試下面提到的代碼。 $results數組包含您需要的所有項目。

    $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);



    }
$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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM