Q: How to fetch just the newest unread e-mail, if there is one #359

Closed
thomasfr opened this Issue Mar 8, 2014 · 2 comments

Comments

Projects
None yet
2 participants
@thomasfr

thomasfr commented Mar 8, 2014

In a stateless shell script i execute via a cron-job periodically i fetch the newest e-mail like:

fetch = imap.seq.fetch messagebox.messages.total + ':*', {
    bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT']
    struct: true
    markSeen: true
  }

This works like intended. It returns just the (single) newest e-mail. Is there a way to just get the newest single e-mail, only when it is unread? While ignoring all older unread messages?
Thank you

@mscdex

This comment has been minimized.

Show comment Hide comment
@mscdex

mscdex Mar 8, 2014

Owner

One way might be to search for messages that are 'NEW' (the client session is the first to be notified of the messages ('RECENT') but they have not been read/seen yet ('UNSEEN')). This could return more than 1 result however. Example:

imap.search([ 'NEW' ], function(err, results) {
   if (err || !results.length) return imap.end();

   // fetch all resulting messages
   imap.fetch(results, ....);
   // or just fetch only the first result... which may not necessarily be newest
   // because the server can return results in any order
   imap.fetch(results[0], ...);
});

If your server supports the 'SORT' capability (imap.serverSupports('SORT'); returns true), then you can modify the above to use the following, which will sort the results on the server side:

imap.sort([ '-ARRIVAL' ], [ 'NEW' ], function(err, results) {
   if (err || !results.length) return imap.end();

   // fetch all resulting messages
   imap.fetch(results, ....);
   // or just fetch the only the first (guaranteed to be newest) result
   imap.fetch(results[0], ...);
});

The '-ARRIVAL' sorts in reverse based upon the arrival time of each message.

Owner

mscdex commented Mar 8, 2014

One way might be to search for messages that are 'NEW' (the client session is the first to be notified of the messages ('RECENT') but they have not been read/seen yet ('UNSEEN')). This could return more than 1 result however. Example:

imap.search([ 'NEW' ], function(err, results) {
   if (err || !results.length) return imap.end();

   // fetch all resulting messages
   imap.fetch(results, ....);
   // or just fetch only the first result... which may not necessarily be newest
   // because the server can return results in any order
   imap.fetch(results[0], ...);
});

If your server supports the 'SORT' capability (imap.serverSupports('SORT'); returns true), then you can modify the above to use the following, which will sort the results on the server side:

imap.sort([ '-ARRIVAL' ], [ 'NEW' ], function(err, results) {
   if (err || !results.length) return imap.end();

   // fetch all resulting messages
   imap.fetch(results, ....);
   // or just fetch the only the first (guaranteed to be newest) result
   imap.fetch(results[0], ...);
});

The '-ARRIVAL' sorts in reverse based upon the arrival time of each message.

@mscdex mscdex closed this May 1, 2014

@thomasfr

This comment has been minimized.

Show comment Hide comment
@thomasfr

thomasfr May 1, 2014

Sorry,...

thanks for your help! This was exactly what i was searching for. My server had the SORT capabality! 👍

thomasfr commented May 1, 2014

Sorry,...

thanks for your help! This was exactly what i was searching for. My server had the SORT capabality! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment