This method supports unique IDs instead of message numbers. See
{@link ezcMailImapTransportOptions} for how to enable unique IDs
referencing.
If the command failed or if it was not supported by the server an empty
string is returned.
This method is useful for retrieving the headers of messages from the
mailbox as strings, which can be later parsed with {@link ezcMailParser}
and {@link ezcMailVariableSet}. In this way the retrieval of the full
messages from the server is avoided when building a list of messages.
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 of listing the mail headers of all the messages in the current
mailbox:
$imap = new ezcMailImapTransport( 'imap.example.com' );
$imap->authenticate( 'username', 'password' );
$imap->selectMailbox( 'Inbox' );
$parser = new ezcMailParser();
$messages = $imap->listMessages();
foreach ( $messages as $messageNr => $size )
{
$set = new ezcMailVariableSet( $imap->top( $messageNr ) );
$mail = $parser->parseMail( $set );
$mail = $mail[0];
echo "From: {$mail->from}, Subject: {$mail->subject}, Size: {$size}\n";
}
For a more advanced example see the "Mail listing example" in the online
documentation. $messages = $transport->listMessages(); // Calculate how many pages of mails there will be based on pageSize $numberOfPages = (int) floor(count($messages) / $options["pageSize"] + 1); // See if currentPage fits in the range 1..numberOfPages if ($options["currentPage"] <= 0 || $options["currentPage"] > $numberOfPages || count($messages) % $options["pageSize"] === 0 && $options["currentPage"] >= $numberOfPages) { $options["currentPage"] = 1; } // Slice the array to the range defined by currentPage $sizes = array_slice(array_values($messages), ($options["currentPage"] - 1) * $options["pageSize"], $options["pageSize"]); $mailIDs = array_slice($mailIDs, ($options["currentPage"] - 1) * $options["pageSize"], $options["pageSize"]); $messages = array_keys($messages); // Read and parse the headers of the mails in the currentPage from the IMAP server $mails = array(); $parser = new ezcMailParser(); for ($i = ($options["currentPage"] - 1) * $options["pageSize"]; $i < min($options["currentPage"] * $options["pageSize"], count($messages)); $i++) { $msg = $transport->top($messages[$i]); $lines = preg_split("/\r\n|\n/", $msg); $msg = null; foreach ($lines as $line) { // eliminate the line that contains "Content-Type" at it would throw // a notice for "multipart/related" (because the multipart object cannot // be created due to missing the body) if (stripos($line, "Content-Type:") === false) { $msg .= $line . PHP_EOL; } else { // insert code to analyse the Content-Type of the mail // and add an "attachment" icon in case it is "multipart" } } $set = new ezcMailVariableSet($msg); $mail = $parser->parseMail($set);
public function testUidInvalidTop() { $imap = new ezcMailImapTransport(self::$server, self::$port, array('uidReferencing' => true)); $imap->authenticate(self::$user, self::$password); $imap->selectMailbox('inbox'); try { $imap->top(1, 1); $this->fail("Didn't get exception when expected"); } catch (ezcMailTransportException $e) { $this->assertEquals("An error occured while sending or receiving mail. The IMAP server could not fetch the message '1': A0003 OK Fetch completed..", $e->getMessage()); } }
/** * Test for issue #14360: problems with $imap->top() command in gmail. */ public function testTopGmailHeadersOnly() { $options = new ezcMailImapTransportOptions(); $options->ssl = true; $imap = new ezcMailImapTransport('imap.gmail.com', '993', $options); // please don't use this account :) $imap->authenticate('ezcomponents' . '@' . 'gmail.com', 'wee12345'); $imap->selectMailbox('inbox'); $text = $imap->top(1, 1); $this->assertEquals(382, strlen($text)); }