go - Sort IMAP email uids based on data Golang - Stack Overflow

I am using this IMAP Golang package to fetch recent emails from my gmail. I would like to only get the

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 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
    }
}

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 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
    }
}
Share Improve this question asked Feb 21 at 5:07 Ahmed ZaidanAhmed Zaidan 711 gold badge9 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745166625a4614672.html

相关推荐

  • go - Sort IMAP email uids based on data Golang - Stack Overflow

    I am using this IMAP Golang package to fetch recent emails from my gmail. I would like to only get the

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信