flourishlib / flourish-classes

The class files for Flourish
http://flourishlib.com
182 stars 78 forks source link

fMailbox - Get latest emails #188

Open gemal opened 11 years ago

gemal commented 11 years ago

I'm writing a app that needs to get the latest 10 emails from a huge inbox.

Using your class is very good, but it needs to parse all mesages in the inbox.

I could really need a function that could get me the latest 10 UIDs which I can the itterate over.

I could imagine that others also needed this.

gemal commented 11 years ago

Let me know if you need a patch. Shouldn't that difficult since the code is already there

novastream commented 11 years ago

found this solution, works pretty good

fMailbox.php

public function listMessages($limit=NULL, $page=NULL, $order='newest')

Add this to IMAP and POP function inside listMessages

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

if (!$limit) {
    $start = 1;
    $end   = '*';
} else {
    if (!$page) {
        $page = 1;
    }
    if ($order == 'newest') {
        $start = $total_messages - $limit * $page + 1;
        $end   = $start + $limit - 1;
    } else {
        $start = ($limit * ($page-1)) + 1;
        $end   = $start + $limit - 1;
    }
}

and just above the return $output, add this

if ($order == 'newest') {
     arsort($output);
}

if you try it out, please tell us how the performance was, I'm grabbing 10 emails from a inbox with 4000 messages in 1 second.

emilsundberg commented 11 years ago

@novastream Where does $folder come from?

novastream commented 11 years ago

oh sorry forgot that...

public function listMessages($limit=NULL, $page=NULL, $order='newest', $folder=NULL)

and than just above the larger pice of code add

if(!isset($folder)) {
$folder = 'INBOX';
}

if you want to fetch other folders just pass them in $folder like $folder = 'INBOX.SENT';

if you want you can check out my updated fMailbox.php, I've added sorting, folders and counting messages. https://github.com/novastream/flourish-imap

Gummibeer commented 8 years ago

@novastream Thanks for your fork! :+1: But one thing you have missed is to select the folder, bacause that is required to fetch the messages from the folder:

Line 1239 - https://github.com/novastream/flourish-imap/blob/master/flourish/fMailbox.php#L1239

$this->write('SELECT "'.$folder.'"');

Here I found a very nice cheatsheet: http://donsutherland.org/crib/imap

novastream commented 8 years ago

Hi Tom,

Haven't updated the Flourish library in a while BUT have some changes that I can commit soon.

There's a lot of bug fixes.

Med vänlig hälsning / Best regards

Jonathan Lindgren Web Developer / Unified Collab Tech

Kanslihusvägen 13A 281 35 Hässleholm Sweden Phone: +46(0)451-76 96 00 jonathan@novastream.se www.novastream.se

vSphere ESXi | pfSense | Zimbra | CollabNet

Från: "Tom Witkowski" notifications@github.com Till: "flourishlib/flourish-classes" flourish-classes@noreply.github.com Kopia: "Jonathan Lindgren" jonathan@novastream.se Skickat: tisdag, 19 jan 2016 15:24:53 Ämne: Re: [flourish-classes] fMailbox - Get latest emails (#188)

@novastream Thanks for your fork! But one thing you have missed is to select the folder, bacause that is required to fetch the messages from the folder:

Line 1239 - https://github.com/novastream/flourish-imap/blob/master/flourish/fMailbox.php#L1239 $this -> write( ' SELECT " ' . $folder . ' " ' );

— Reply to this email directly or view it on GitHub .

Gummibeer commented 8 years ago

Have you added any function to get a list of all folders? And would be cool if you can push all the changes you made - and I think that the Folder change is something that can be merged in the main repo!?