package main
import (
"io"
"io/ioutil"
"log"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-message"
)
func main() {
log.Println("Connecting to server...")
c, err := client.DialTLS("xxxxxxxxx", nil)
if err != nil {
log.Fatal(err)
}
log.Println("Connected")
defer c.Logout()
if err := c.Login("xxxxxxxxxxxxx", "xxxxxxxxxx"); err != nil {
log.Fatal(err)
}
log.Println("Logged in")
mbox, err := c.Select("INBOX", false)
if err != nil {
log.Fatal(err)
}
seqset := new(imap.SeqSet)
seqset.AddRange(1, mbox.Messages)
messages := make(chan *imap.Message, 10)
done := make(chan error, 1)
go func() {
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchRFC822}, messages)
}()
for msg := range messages {
for _, r := range msg.Body {
entity, err := message.Read(r)
if err != nil {
log.Fatal(err)
}
multiPartReader := entity.MultipartReader()
for e, err := multiPartReader.NextPart(); err != io.EOF; e, err = multiPartReader.NextPart() {
kind, params, cErr := e.Header.ContentType()
if cErr != nil {
log.Fatal(cErr)
}
if kind != "image/png" && kind != "image/gif" {
continue
}
c, rErr := ioutil.ReadAll(e.Body)
if rErr != nil {
log.Fatal(rErr)
}
log.Printf("Dump file %s", params["name"])
if fErr := ioutil.WriteFile("/tmp/"+params["name"], c, 0777); ferr != nil {
log.Fatal(fErr)
}
}
}
}
if err := <-done; err != nil {
log.Fatal(err)
}
log.Println("Done")
}
Пример взял тут
https://stackoverflow.com/questions/55203878/how-t...