PHP Horde imap如何获取

时间:2018-07-15 10:27:49

标签: php imap horde

我看到了类似的问题,但并没有帮助我。我正在尝试获取消息。我需要完整的消息,包括所有部分,标题,附件。

$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的此类邮件,我也通过部落获得了该uid。

2 个答案:

答案 0 :(得分:0)

尝试下面提到的代码。 $ 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);



    }

答案 1 :(得分:-1)

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