ezcMailImapTransport::listMessages PHP Method

listMessages() public method

It returns only the messages with the flag DELETED not set. Before calling this method, a connection to the IMAP server must be established and a user must be authenticated successfully, and a mailbox must be selected. The format of the returned array is array( message_id => size ); Example: array( 2 => 1700, 5 => 1450, 6 => 21043 ); If $contentType is set, it returns only the messages with $contentType in the Content-Type header. For example $contentType can be "multipart/mixed" to return only the messages with attachments.
public listMessages ( string $contentType = null ) : array(int)
$contentType string
return array(int)
    public function listMessages($contentType = null)
    {
        if ($this->state != self::STATE_SELECTED && $this->state != self::STATE_SELECTED_READONLY) {
            throw new ezcMailTransportException("Can't call listMessages() on the IMAP transport when a mailbox is not selected.");
        }
        $messageList = array();
        $messages = array();
        // get the numbers of the existing messages
        $tag = $this->getNextTag();
        $command = "{$tag} SEARCH UNDELETED";
        if (!is_null($contentType)) {
            $command .= " HEADER \"Content-Type\" \"{$contentType}\"";
        }
        $this->connection->sendData($command);
        $response = trim($this->getResponse('* SEARCH'));
        if (strpos($response, '* SEARCH') !== false) {
            $ids = trim(substr($response, 9));
            if ($ids !== "") {
                $messageList = explode(' ', $ids);
            }
        }
        // skip the OK response ("{$tag} OK Search completed.")
        $response = trim($this->getResponse($tag, $response));
        if ($this->responseType($response) != self::RESPONSE_OK) {
            throw new ezcMailTransportException("The IMAP server could not list messages: {$response}.");
        }
        if (!empty($messageList)) {
            // get the sizes of the messages
            $tag = $this->getNextTag();
            $query = trim(implode(',', $messageList));
            $this->connection->sendData("{$tag} FETCH {$query} RFC822.SIZE");
            $response = $this->getResponse('FETCH (');
            $currentMessage = trim(reset($messageList));
            while (strpos($response, 'FETCH (') !== false) {
                $line = $response;
                $line = explode(' ', $line);
                $line = trim($line[count($line) - 1]);
                $line = substr($line, 0, strlen($line) - 1);
                $messages[$currentMessage] = intval($line);
                $currentMessage = next($messageList);
                $response = $this->connection->getLine();
            }
            // skip the OK response ("{$tag} OK Fetch completed.")
            $response = trim($this->getResponse($tag, $response));
            if ($this->responseType($response) != self::RESPONSE_OK) {
                throw new ezcMailTransportException("The IMAP server could not list messages: {$response}.");
            }
        }
        return $messages;
    }

Usage Example

 public function testListMessagesReadOnly()
 {
     $imap = new ezcMailImapTransport(self::$server, self::$port);
     $imap->authenticate(self::$user, self::$password);
     $imap->selectMailbox('inbox', true);
     $list = $imap->listMessages();
     $this->assertEquals(array(1 => self::$sizes[0], 2 => self::$sizes[1], 3 => self::$sizes[2], 4 => self::$sizes[3]), $list);
 }
All Usage Examples Of ezcMailImapTransport::listMessages