Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

fMailbox - Only get unread messages

posted by paul 8 years ago

Hello,

How will I be able to only get a list of unread messages? I tried changing the following:

			$response = $this->write('STATUS "INBOX" (MESSAGES)');
			foreach ($response as $line) {
				if (preg_match('#^\\s*\\*\\s+STATUS\\s+"?MESSAGES"?\\s+\\((.*)\\)$#', $line, $match)) {
					$details = self::parseResponse($match[1], TRUE);
					$total_messages = $details['messages'];
				}
			}

Into:

			$response = $this->write('STATUS "INBOX" (UNSEEN)');
			foreach ($response as $line) {
				if (preg_match('#^\\s*\\*\\s+STATUS\\s+"?INBOX"?\\s+\\((.*)\\)$#', $line, $match)) {
					$details = self::parseResponse($match[1], TRUE);
					$total_messages = $details['unseen'];
				}
			}

This returns the amount of UNSEEN messages (in my case 1), but this still returns all of the emails that I have in my mailbox. I assume there should be something changed/added to the FETCH command, but I don't have any experience with IMAP at all.

I hope someone could help me with this :)

Thanks in advance!

I currently fixed it by doing:

			$total_messages = 0;
			$response = $this->write('SEARCH (UNSEEN)');
			foreach ($response as $line) {
				if (preg_match('#^\\s*\\*\\s+SEARCH\\s+(.*)$#', $line, $match)) {
					$messages = str_replace(" ", ",", $match[1]);
					$total_messages = count(explode(" ", $match[1]));
				}
			}

			if ($start > $total_messages) {
				return array();
			}
			
			if ($end > $total_messages) {
				$end = $total_messages;
			}

			$output = array();
			$response = $this->write('FETCH ' . $messages . ' (UID INTERNALDATE RFC822.SIZE ENVELOPE)');

If anyone knows a better solution for this, please let me know!

posted by paul 8 years ago