doutong2132 2014-05-26 09:33
浏览 31
已采纳

GMAIL Zend IMAP如何获取特定消息的ID

i am trying to implement Zend IMAP framework when using the GMAIL api when programming in PHP

all goes well and i get the messages - email (from / to) but for storing purposes i need the unique id of the message.

are there any ways of getting that? i use the standard code.

thank you very much, hope this question is clearly enough described.

the reason i want the unique message id is because i need to store the data. if i do not use the unique id i cannot compare the messages to what is in the db, the messagenumber as given in zend is as an example 8. if i then delete message nr. 7 then nr. 8 will become nr. 7 which makes this not very useful.

i can see that i have to use : X-GM-MSGID but i dont know where to put it... when i put it in my fetch it does not work:

$test = $imap->fetch("X-GM-MSGID", null, $emailto);

 public function fetch($items, $from, $to = null)
    {
        if (is_array($from)) {
            $set = implode(',', $from);
        } else if ($to === null) {
            $set = (int)$from;
        } else if ($to === INF) {
            $set = (int)$from . ':*';
        } else {
            $set = (int)$from . ':' . (int)$to;
        }

        $items = (array)$items;
        $itemList = $this->escapeList($items);

        $this->sendRequest('FETCH', array($set, $itemList), $tag);

        $result = array();
        while (!$this->readLine($tokens, $tag)) {
            // ignore other responses
            if ($tokens[1] != 'FETCH') {
                continue;
            }
            // ignore other messages
            if ($to === null && !is_array($from) && $tokens[0] != $from) {
                continue;
            }
            // if we only want one item we return that one directly
            if (count($items) == 1) {
                if ($tokens[2][0] == $items[0]) {
                    $data = $tokens[2][1];
                } else {
                    // maybe the server send an other field we didn't wanted
                    $count = count($tokens[2]);
                    // we start with 2, because 0 was already checked
                    for ($i = 2; $i < $count; $i += 2) {
                        if ($tokens[2][$i] != $items[0]) {
                            continue;
                        }
                        $data = $tokens[2][$i + 1];
                        break;
                    }
                }
            } else {
                $data = array();
                while (key($tokens[2]) !== null) {
                    $data[current($tokens[2])] = next($tokens[2]);
                    next($tokens[2]);
                }
            }
            // if we want only one message we can ignore everything else and just return
            if ($to === null && !is_array($from) && $tokens[0] == $from) {
                // we still need to read all lines
                while (!$this->readLine($tokens, $tag));
                return $data;
            }
            $result[$tokens[0]] = $data;
        }

        if ($to === null && !is_array($from)) {
            /**
             * @see Zend_Mail_Protocol_Exception
             */
            require_once 'Zend/Mail/Protocol/Exception.php';
            throw new Zend_Mail_Protocol_Exception('the single id was not found in response');
        }

        return $result;
    }
  • 写回答

1条回答 默认 最新

  • doucou19961205 2014-05-26 11:50
    关注
     public function getMessage($id)
        {
            $data = $this->_protocol->fetch(array('FLAGS', 'RFC822.HEADER', 'RFC822.TEXT', 'X-GM-MSGID'), $id);
            $header = $data['RFC822.HEADER'];
            $content = $data['RFC822.TEXT'];
            $messageid = $data['X-GM-MSGID'];
    
            var_dump($messageid);
    
    
    
            $flags = array();
            foreach ($data['FLAGS'] as $flag) {
                $flags[] = isset(self::$_knownFlags[$flag]) ? self::$_knownFlags[$flag] : $flag;
            }
    
            return new $this->_messageClass(array('handler' => $this, 'id' => $id, 'headers' => $header, 'content' => $content, 'flags' => $flags, 'messageid' => $messageid));
        }
    

    for some reason it works fine when extracting it from the IMAP class.

    just pass it to your other classes and use it as you will

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)