A
A
Anton2021-02-05 20:06:46
go
Anton, 2021-02-05 20:06:46

How to delete email via imap in golang?

Can you suggest how you can delete a specific letter. For example, there is a
Message-ID: [<[email protected]>]

I work through this extension:
github.com/emersion/go-imap, but I could not find anything there.

Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-02-06
@anton11122211

You must first mark the messages as deleted (set the flag), then call Expunge.
The documentation says that if you call Close, then messages marked as deleted will be deleted, but I did not check :)
There is an example here: https://godocs.io/github.com/emersion/go-imap/clie. ..

var c *client.Client

mbox, err := c.Select("INBOX", false)
if err != nil {
    log.Fatal(err)
}

if mbox.Messages == 0 {
    log.Fatal("No message in mailbox")
}

seqset := new(imap.SeqSet)
for _, message := range mbox.Messages {
     // если сообщение должно быть удалено - тогда вызываете AddNum
     // используем именно message.Uid а не message.SeqNum потому, что ниже мы будем вызывать UidStore, а не Store, так будет явно надёжнее
     seqset.AddNum(message.Uid)
}

// отмечаем сообщения как удалённые
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.DeletedFlag}
if err := c.UidStore(seqset, item, flags, nil); err != nil {
    log.Fatal(err)
}

// удаляем сообщение
if err := c.Expunge(nil); err != nil {
    log.Fatal(err)
}

log.Println("Last message has been deleted")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question