ezcMailImapTransport::sortFromOffset PHP Method

sortFromOffset() public method

This method supports unique IDs instead of message numbers. See {@link ezcMailImapTransportOptions} for how to enable unique IDs referencing. It is useful for paging through a mailbox. Fetches $count messages starting from the $offset and returns them as a {@link ezcMailImapSet}. If $count is is 0, it fetches all messages starting from the $offset. $sortCriteria is an email header like: Subject, To, From, Date, Sender, etc. 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. Example: $imap = new ezcMailImapTransport( 'imap.example.com' ); $imap->authenticate( 'username', 'password' ); $imap->selectMailbox( 'mailbox' ); // Inbox or another mailbox Fetch a range of messages sorted by Date $set = $imap->sortFromOffset( 1, 10, "Date" ); $set can be parsed with ezcMailParser
public sortFromOffset ( integer $offset, integer $count, string $sortCriteria, boolean $reverse = false ) : ezcMailImapSet
$offset integer
$count integer
$sortCriteria string
$reverse boolean
return ezcMailImapSet
    public function sortFromOffset($offset, $count = 0, $sortCriteria, $reverse = false)
    {
        if ($count < 0) {
            throw new ezcMailInvalidLimitException($offset, $count);
        }
        $range = array();
        if ($this->options->uidReferencing) {
            $uids = array_values($this->listUniqueIdentifiers());
            $flip = array_flip($uids);
            if (!isset($flip[$offset])) {
                throw new ezcMailOffsetOutOfRangeException($offset, $count);
            }
            $start = $flip[$offset];
            $messages = $this->sort($uids, $sortCriteria, $reverse);
            if ($count === 0) {
                $count = count($messages);
            }
            $ids = array_keys($messages);
            for ($i = $start; $i < $count; $i++) {
                $range[] = $ids[$i];
            }
        } else {
            $messageCount = $this->countByFlag('ALL');
            $messages = array_keys($this->sort(range(1, $messageCount), $sortCriteria, $reverse));
            if ($count === 0) {
                $count = count($messages);
            }
            $range = array_slice($messages, $offset - 1, $count, true);
            if (!isset($range[$offset - 1])) {
                throw new ezcMailOffsetOutOfRangeException($offset, $count);
            }
        }
        return new ezcMailImapSet($this->connection, $range, false, array('uidReferencing' => $this->options->uidReferencing));
    }

Usage Example

Exemplo n.º 1
0
 public function testUidSortFromOffsetByDateReverse()
 {
     $imap = new ezcMailImapTransport(self::$server, self::$port, array('uidReferencing' => true));
     $imap->authenticate(self::$user, self::$password);
     $imap->selectMailbox("Inbox");
     $set = $imap->sortFromOffset(self::$ids[0], 4, 'date', true);
     $parser = new ezcMailParser();
     $mail = $parser->parseMail($set);
     $this->assertEquals(4, count($mail));
     $this->assertEquals("pine: test 2 with 8bit norwegian chars", $mail[1]->subject);
     $this->assertEquals("pine: Mail with attachment", $mail[2]->subject);
     $this->assertEquals("pine: test 3 with norwegian chars", $mail[0]->subject);
     $this->assertEquals("pine: Mail with attachment", $mail[3]->subject);
 }
All Usage Examples Of ezcMailImapTransport::sortFromOffset