GithubHelp home page GithubHelp logo

UIDNext of opened box ? about node-imap HOT 22 CLOSED

mscdex avatar mscdex commented on July 17, 2024
UIDNext of opened box ?

from node-imap.

Comments (22)

mscdex avatar mscdex commented on July 17, 2024

The uidnext value is currently a "private" variable, but you can access it via box._uidnext after opening a mailbox (where 'box' is the value passed in to openBox()'s callback).

The value does not automatically update when new mail arrives because one cannot assume that the uid will necessarily increase by N if N new messages arrive. However, according to this post, it seems you will probably be safe making this assumption in most cases?

from node-imap.

gkrcode avatar gkrcode commented on July 17, 2024

thanks @mscdex ,

If it is updated automatically i guess on 'mail' event we can fetch new messages using Fetch UID currentUIDNext:* instead of search and then fetch.

from node-imap.

gkrcode avatar gkrcode commented on July 17, 2024

I think IMAP's STATUS command gives current UIDNext when we send it but it is not done yet in node-imap :)

from node-imap.

mscdex avatar mscdex commented on July 17, 2024

There are a couple of problems with using the STATUS command (especially every time new mail arrives):

  • It can be slow
  • According to the IMAP RFC 3501: "Note: The STATUS command is intended to access the status of mailboxes other than the currently selected mailbox. Because the STATUS command can cause the mailbox to be opened internally, and because this information is available by other means on the selected mailbox, the STATUS command SHOULD NOT be used on the currently selected mailbox."

Also, regarding fetching "currentUIDNext:*", you cannot assume that the UIDs are contiguous, only that they uniquely identify messages. A better way would be to use sequence numbers instead, which are contiguous where sequence number 1 is the first message in the mailbox. Example:

// var box === currently open mailbox
imap.on('mail', function(num) {
  var fetcher = imap.seq.fetch((box.total-num) + ':*', { markSeen: false });
  fetcher.on('message', function(msg) {
    // do something with msg here
  });
  fetcher.on('end', function() {
   // done obtaining new messages!
  });
});

from node-imap.

gkrcode avatar gkrcode commented on July 17, 2024

I didnt know STATUS command has issues. Thanks @mscdex for informing me.

If we fetching using currentUIDNext:* client don't have to assume/worry about sequential of UIDs because of the end of UID range is * so we get any message that has UID larger than currentUIDNext.

Edit: I didnt read the code sample before posting. Thanks for the code I am more clear now. Is box.total updated automatically when new mail received ?

from node-imap.

gkrcode avatar gkrcode commented on July 17, 2024

I checked @imap._state.box.messages.total with a sample and it works as expected, Thanks @mscdex :)

from node-imap.

mscdex avatar mscdex commented on July 17, 2024

My mistake, it is box.messages.total and yes it is updated when new mail arrives and when messages are deleted. Also there is box.messages.new which always holds the current number of new messages.

from node-imap.

gkrcode avatar gkrcode commented on July 17, 2024

@mscdex, Thanks for your kind support :)

from node-imap.

vvo avatar vvo commented on July 17, 2024

The linked conversation about contiguous uids is now here: this post

from node-imap.

yoggikim avatar yoggikim commented on July 17, 2024

@mscdex the sequence id changes on move and delete, though, so if I wanted to get the messages that arrived after a specific message, what is the best way to do that ?

from node-imap.

mscdex avatar mscdex commented on July 17, 2024

@yoggikim search by date?

from node-imap.

yoggikim avatar yoggikim commented on July 17, 2024

@mscdex date is daily, might return prior emails, isn't it ?

from node-imap.

mscdex avatar mscdex commented on July 17, 2024

@yoggikim I'm not sure what you mean. You said you had a specific message, which would have an arrival date associated with it. To get messages that arrived afterwards, do a search for emails that arrived after that date? If your server supports the SORT capability then you could use 'ARRIVAL' criteria which takes time into account also.

from node-imap.

yoggikim avatar yoggikim commented on July 17, 2024

Suppose it doesn't support sort? I could do a search on the date but that would return all the emails after and on that DAY, including emails which arrived BEFORE my given email

from node-imap.

vvo avatar vvo commented on July 17, 2024

About uids: yahoo has not contiguous uids. What I will do is record the highest uid I know exists in the mailbox and do a search(['ALL', ['UID', '$knownUid:*']])

from node-imap.

vvo avatar vvo commented on July 17, 2024

On yahoo, if you search for a uid that does not exists, you will fail the server badly sometimes.. :D

from node-imap.

yoggikim avatar yoggikim commented on July 17, 2024

So.. whats the approach I should take? I need to support Yahoo as well.

from node-imap.

vvo avatar vvo commented on July 17, 2024

Record the highest uid you know exists in the mailbox and then everytime a new message arrives, do search(['ALL', ['UID', '$knownUid:*']]).

This mean you need to search for all uid in mailbox first when you connect to the server (search(['ALL']))

from node-imap.

yoggikim avatar yoggikim commented on July 17, 2024

but @vvo that won't work on yahoo, wont it ?

from node-imap.

vvo avatar vvo commented on July 17, 2024

No no it will work, what will not work on yahoo is make assumptions that the next uid will be the box.uidnext + 1. It is not true, the next uid will be the highest real uid of the server (which can be different from box.uidnext - 1, don't use it) + *.

So:
Search all uids at connect, keep you local highestuid up to date (based on new message and expunges) and everytime a new mail arrives, do a search highestuidyouknow:*. You will get the new uids.

from node-imap.

yoggikim avatar yoggikim commented on July 17, 2024

My issue is on downtimes and disconnects, otherwise, when I receive an email I just do a search on the exists "total" imap.search([[''+seqno]] and fetch it right after by uid.
Only thing is, why do a search all and not just use uidnext?

from node-imap.

shinux avatar shinux commented on July 17, 2024

@vvo how to search a single latest uid for specific box?

from node-imap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.