Search code examples
goimapflags

How to mark message as unread in go?


I wanna to mark message by IMAP as unread in go. I don't know, what parameters give to "Replace" function. I'm using http://godoc.org/github.com/mxk/go-imap/imap

It's my code:

set, _ := imap.NewSeqSet("")
set.AddNum(45364) // 45364 is message's UID
_, err = imap.Wait(c.UIDStore(set, "+FLAGS", imap.Replace()))

Solution

  • Taking a look at RFC3501 and the documentation for Replace, it looks a bit messy. Investigating the source for Replace, it just wants a []string with the keywords from RFC3501. So, for example

    flags := []string{}
    // ....
    _, err = imap.Wait(c.UIDStore(set, "+FLAGS", imap.Replace(flags)))
    // Since the "\Seen" is not in splice, the message will be unseen
    

    Please notice that Replace really removes all the flags. You have to handle (add to the splice as string values) what you want to preserve:

    • \Seen
    • \Answered
    • \Flagged
    • \Deleted
    • \Draft
    • \Recent

    You can get the previous values from the MessageInfo struct / Flags:

    type MessageInfo struct {
        Attrs        FieldMap  // All returned attributes
        Seq          uint32    // Message sequence number
        UID          uint32    // Unique identifier (optional in non-UID FETCH)
        Flags        FlagSet   // Flags that are set for this message (optional)
        InternalDate time.Time // Internal to the server message timestamp (optional)
        Size         uint32    // Message size in bytes (optional)
    }