emersion / go-imap

📥 An IMAP library for clients and servers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Search with criteria returns empty data

bondar-aleksandr opened this issue · comments

Search returns empty *SearchData, regardless of criteria used

Code example:

criteria := &imap.SearchCriteria{}
criteria.Flag = []imap.Flag{imap.FlagSeen}
criteria.Header = []imap.SearchCriteriaHeaderField{
  {Key: "FROM", Value: appConfig.From[0]},
}
fmt.Println(criteria)
searchData, err := Client.Search(criteria, nil).Wait()
fmt.Println(searchData)

Output:

&{[] [] 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC [{FROM aleksandr.bondar.1985@gmail.com}] [] [] [\Seen] [] 0 0 [] [] <nil>}
&{ false 0 0 0 0}

In version 1.2.1 everything works just fine

Can you share debug logs?

I don't have access to server side (I use "imap.ukr.net"). What do you mean by debug logs?

I mean ClientOptions.DebugWriter set to os.Stderr or something

Sure, here it is:

INFO: 2023/10/17 19:05:45 imapWorker.go:44: Connecting to IMAP server imap.ukr.net:993...
INFO: 2023/10/17 19:05:45 imapWorker.go:49: Connected successfully
INFO: 2023/10/17 19:05:45 imapWorker.go:52: Logging in...
T1 LOGIN "dvr.my.home@ukr.net" "***"
* OK IMAP4 ready
T2 CAPABILITY
T1 OK LOGIN completed
INFO: 2023/10/17 19:05:45 imapWorker.go:56: Logged in successfully
T3 CAPABILITY
INFO: 2023/10/17 19:05:45 imapWorker.go:59: Selecting inbox folder...
T4 SELECT INBOX
* CAPABILITY IMAP4rev1 AUTH=PLAIN IDLE ID LITERAL+ UNSELECT NAMESPACE XLIST UIDPLUS MOVE LIST-STATUS APPENDLIMIT=31457280
T2 OK CAPABILITY completed
* CAPABILITY IMAP4rev1 AUTH=PLAIN IDLE ID LITERAL+ UNSELECT NAMESPACE XLIST UIDPLUS MOVE LIST-STATUS APPENDLIMIT=31457280
T3 OK CAPABILITY completed
* 322 EXISTS
* 1 RECENT
* FLAGS (\Answered \Deleted \Seen \Flagged $Forwarded)
* OK [UNSEEN 9]
* OK [UIDVALIDITY 1]
* OK [UIDNEXT 31593]
* OK [PERMANENTFLAGS (\Answered \Deleted \Seen \Flagged $Forwarded)]
T4 OK [READ-WRITE] SELECT completed
INFO: 2023/10/17 19:05:45 imapWorker.go:64: Selected inbox folder "INBOX" successfully
INFO: 2023/10/17 19:05:45 imapWorker.go:65: amount of messages in INBOX folder: 322
INFO: 2023/10/17 19:05:45 imapWorker.go:77: Searching messages...

//this is output from fmt.Println(criteria)
&{[] [] 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC [{FROM aleksandr.bondar.1985@gmail.com}] [] [] [\Seen] [] 0 0 [] [] <nil>}
T5 SEARCH (FROM "aleksandr.bondar.1985@gmail.com" SEEN)
* SEARCH
T5 OK SEARCH completed
//this is output from fmt.Println(searchData)
&{ false 0 0 0 0}
INFO: 2023/10/17 19:05:45 imapWorker.go:119: No new messages found.
T6 LOGOUT
* BYE server terminating connection

Hm, the server returns 0 results, and the command looks good to me. Can you try getting go-imap v1 debug logs as well so that we can compare?

I found the problem. First of all, there were no messages corresponding to search criteria in mailbox. As soon as I fixed that, I saw the search result. What guided me wrong, was the fact that I counted on .Count field in SearchData struct in search.go

type SearchData struct {
	All SeqSet

	// requires IMAP4rev2 or ESEARCH
	UID   bool
	Min   uint32
	Max   uint32
	Count uint32

	// requires CONDSTORE
	ModSeq uint64
}

But .Count doesn't changes regardless of how many items are in .All

Sorry for that :(

Yeah, the count requires additional extensions as hinted in the comment above.