2

I am using this IMAP Golang package to fetch recent emails from my gmail. I would like to only get the 50 most recent messages. My current approach is fetching every single email uid. I am using imap.gmail.com is there anyway to get the most recent messages, or the 50 highest UID's?

func getCode(findEmail string) (string, error) {
    uids, err := im.GetUIDs("1:*")
    if err != nil {
        imapLock.RUnlock()
        return "", err
    }

    if len(uids) > 50 {
        uids = uids[len(uids)-50:]
    }

    emails, err := im.GetEmails(uids...)
    if err != nil {
        return "", err
    }

    for _, email := range emails {
        // do something
    }
}

1 Answer 1

2

Modify your UID fetch range to "*" or use SORT if supported:

uids, err := im.GetUIDs("50:*") // Fetch last 50 UIDs

or

uids, err := im.GetSortedUIDs("REVERSE DATE", 50) // If sorting is supported
Sign up to request clarification or add additional context in comments.

1 Comment

Using the first route with the command im.GetUIDs("50:*") does not get the last 50 email uids. Instead this command gets all uids without the first 50. First 50 being the 50 oldest. And the Go package doesn't have im.GetSortedUIDs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.