mxk / go-imap

IMAP4rev1 Client for Go
BSD 3-Clause "New" or "Revised" License
212 stars 63 forks source link

Can't delete Message via UID #18

Closed paulm17 closed 9 years ago

paulm17 commented 9 years ago

Hi,

I've tried all day to delete messages. No matter what I try, it just doesn't work.

My code in its entirety that deals with looping through the mailbox.

I want to be able to select WHICH UIDs I delete. That's all.

Thanks

for cmd.InProgress() {
        // Wait for the next response (no timeout)
        c.Recv(-1)

        // Process command data
        for _, rsp = range cmd.Data {
            uid := imap.AsNumber(rsp.MessageInfo().UID)
            //msg_no := imap.AsNumber(rsp.MessageInfo().Seq)
            header := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.HEADER"])
            body := imap.AsBytes(rsp.MessageInfo().Attrs["RFC822.TEXT"])

            if msg, _ := mail.ReadMessage(bytes.NewReader(header)); msg != nil {
                xheaders := make(map[string]string)

                // Add X- Headers
                r := regexp.MustCompile("^X-(.*): (.*)")
                s_body := strings.Split(string(body), "\n")

                for _, value := range s_body {
                    res := r.FindAllString(value, -1)

                    if len(res) > 0 {
                        s_res := strings.Split(res[0], ": ")

                        h_key := s_res[0]
                        h_value := s_res[1]

                        // Fix up
                        h_key = strings.Replace(h_key, "[", "", -1)
                        h_value = strings.Replace(h_value, "\r", "", -1)

                        xheaders[h_key] = h_value
                    }
                }

                // Remove non Mailer-* headers
                for key, _ := range xheaders {
                    if !strings.Contains(key, "X-Mailer") {
                        delete(xheaders, key)
                    }
                }

                // Fix Date
                date := msg.Header.Get("Date")
                s_date := strings.Split(date, " (")

                mail := &Mail{
                    Uid:      uid,
                    Xheaders: xheaders,
                    From:     msg.Header.Get("FROM"),
                    Subject:  msg.Header.Get("Subject"),
                    Body:     string(body),
                    Time:     s_date[0],
                }

                res := process_message(mailbox_type, *mail)

                if res {
                    delset, _ := imap.NewSeqSet(string(uid))
                    c.UIDStore(delset, "+FLAGS.SILENT", imap.NewFlagSet(`\Deleted`))
                }

                fmt.Println(uid)
            }
        }

        c.Expunge(nil)

        cmd.Data = nil

        // Process unilateral server data
        /*for _, rsp = range c.Data {
            fmt.Println("Server data:", rsp)
        }*/
        c.Data = nil
    }
mxk commented 9 years ago

Are you sure that the mailbox is open in READ-WRITE mode? Enable raw message logging (imap.DefaultLogMask = imap.LogConn | imap.LogRaw) and your answer will probably be in there.

paulm17 commented 9 years ago

Thanks. I had:

c.Select("INBOX", true).

Also if this helps anyone else. I had to do:

seq, _ := imap.NewSeqSet("")
seq.AddNum(uid)
c.UIDStore(seq, "+FLAGS.SILENT", imap.NewFlagSet(`\Deleted`))
c.Expunge(nil)

To have deletions.