Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fMailbox - Get latest emails #188

Open
gemal opened this issue Jul 9, 2013 · 7 comments
Open

fMailbox - Get latest emails #188

gemal opened this issue Jul 9, 2013 · 7 comments

Comments

@gemal
Copy link

gemal commented Jul 9, 2013

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
Copy link
Author

gemal commented Jul 9, 2013

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

@novastream
Copy link

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
Copy link

@novastream Where does $folder come from?

@novastream
Copy link

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
Copy link

@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.'"');

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

@novastream
Copy link

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
[email protected]
www.novastream.se

vSphere ESXi | pfSense | Zimbra | CollabNet

Från: "Tom Witkowski" [email protected]
Till: "flourishlib/flourish-classes" [email protected]
Kopia: "Jonathan Lindgren" [email protected]
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
Copy link

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!?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants