mscdex / node-imap

An IMAP client module for node.js.
MIT License
2.17k stars 382 forks source link

Search then Fetch #820

Closed georgeglessner closed 4 years ago

georgeglessner commented 4 years ago

I just started using this package today and am having some trouble. I am trying to get the emails just from the current day, and only emails that come from our domain. I have the following code-snippet right now

imap.once("ready", function () {
    openInbox(function (err, box) {
            if (err) throw err;
            imap.search([['SINCE', `${dfns.format(new Date(), "LLLL dd, yyyy")}`], ['FROM', '@serviceexpress.com']], function(err, results) {
                if (err) throw err;
        var f = imap.seq.fetch(results, {
                bodies: ["HEADER.FIELDS (FROM SUBJECT)", "TEXT"],
                struct: true,
            });

I had it working just using imap.seq.fetch, but I was getting some results I did not want. The following is part of debug output

<= 'A3 OK LOGIN completed.'
=> 'A4 CAPABILITY'
<= '* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=NTLM AUTH=GSSAPI SASL-IR UIDPLUS MOVE ID UNSELECT XPROXY3 CLIENTNETWORKPRESENCELOCATION CHILDREN IDLE NAMESPACE LITERAL+'
<= 'A4 OK CAPABILITY completed.'
=> 'A5 NAMESPACE'
<= '* NAMESPACE (("" "/")) NIL NIL'
<= 'A5 OK NAMESPACE completed.'
=> 'A6 LIST "" ""'
<= '* LIST (\\Noselect \\HasChildren) "/" ""'
<= 'A6 OK LIST completed.'
=> 'A7 EXAMINE "INBOX"'
<= '* 4181 EXISTS'
<= '* 2443 RECENT'
<= '* FLAGS (\\Seen \\Answered \\Flagged \\Deleted \\Draft $MDNSent)'
<= '* OK [PERMANENTFLAGS ()] Permanent flags'
<= '* OK [UNSEEN 1740] Is the first unseen message'
<= '* OK [UIDVALIDITY 14] UIDVALIDITY value'
<= '* OK [UIDNEXT 8623] The next unique identifier value'
<= 'A7 OK [READ-ONLY] EXAMINE completed.'
=> 'A8 UID SEARCH SINCE 16-Sep-2020 FROM "@serviceexpress.com"'
<= '* SEARCH 8583 8584 8585 8586 8587 8588 8592 8593 8594 8595 8596 8597 8598 8599'
<= 'A8 OK SEARCH completed.'
[
  8583, 8584, 8585,
  8586, 8587, 8588,
  8592, 8593, 8594,
  8595, 8596, 8597,
  8598, 8599
]
=> 'A9 FETCH 8583,8584,8585,8586,8587,8588,8592,8593,8594,8595,8596,8597,8598,8599 (UID FLAGS INTERNALDATE BODYSTRUCTURE BODY.PEEK[HEADER.FIELDS (FROM SUBJECT)] BODY.PEEK[TEXT])'
<= 'A9 NO The specified message set is invalid.'
Fetch error: Error: The specified message set is invalid.
=> 'IDLE IDLE'
Done fetching all messages!
<= '+ IDLE accepted, awaiting DONE command.'
=> DONE
<= 'IDLE OK IDLE completed.'
=> 'A10 LOGOUT'
<= '* BYE Microsoft Exchange Server 2016 IMAP4 server signing off.'
<= 'A10 OK LOGOUT completed.'
[connection] Closed

I am assuming it is something simple that I am missing but right now I am banging my head on the wall. Any help is appreciated!

mscdex commented 4 years ago

imap.search() returns UIDs, not sequence numbers. If you want the latter, you need imap.seq.search().

georgeglessner commented 4 years ago

@mscdex I knew it was something simple, thank you so much I will give that a try!