过去几天试图弄清楚这个问题。我正在尝试下载邮箱中所有电子邮件的图像附件。
在这里到处进行调查,发现该mxk/go-imap/issues/17 BODYSTRUCTURE在其中,库的作者给出了一些提示,但是老实说,我不能将自己的想法缠在RFC 3501上,也不能使用该函数的As*
功能就像我看到的那样,他的库在导航和遍历字段一样没有什么不同,就好像它们是数组一样,当找到嵌入的“数组”时进行一些递归。
我已经能够将这段代码放在一起,使用TLS连接到IMAP服务器,并获取所有打印每个字段的电子邮件,请原谅代码中嵌入的西班牙语。
package main
import (
"crypto/tls"
"log"
"time"
"github.com/mxk/go-imap/imap"
)
func main() {
client, err := imap.DialTLS("imapserver.com:993", &tls.Config{
ServerName: "imapserver.com",
InsecureSkipVerify: false,
})
if client.State() == imap.Login {
_, err = client.Login("myemail@imapserver.com", "mypassword:)")
if err != nil {
log.Fatalf("no se pudo iniciar sesion: %s", err)
}
}
defer client.Logout(30 * time.Second)
log.Printf("Conectado")
// lista correo en la bandeja de entrada
client.Select("INBOX", true)
log.Printf("Seleccionado: %s", client.Mailbox)
set, _ := imap.NewSeqSet("")
set.Add("1:*")
cmd, err := client.Fetch(set, "FLAGS", "INTERNALDATE", "RFC822.SIZE", "ENVELOPE", "BODYSTRUCTURE")
if err != nil {
log.Fatalf("no se pudo hacer FETCH: %s", err)
}
log.Println("Mensajes mas recientes:")
for cmd.InProgress() {
// espera por la siguiente respuesta
client.Recv(-1)
for _, respuesta := range cmd.Data {
message := respuesta.MessageInfo()
log.Printf("= %d\n", message.Seq)
for _, k := range imap.AsList(message.Attrs["BODYSTRUCTURE"]) {
log.Printf("== %#v\n", k)
}
}
}
}
这位美女将打印以下内容:
2019/03/16 22:30:45 Conectado
2019/03/16 22:30:45 Seleccionado: --- "INBOX" ---
ReadOnly: true
Flags: (\Answered \Deleted \Draft \Flagged \Seen)
PermFlags: ()
Messages: 2
Recent: 0
Unseen: 1
UIDNext: 85
UIDValidity: 1542111171
UIDNotSticky: false
2019/03/16 22:30:45 Mensajes mas recientes:
2019/03/16 22:30:45 = 1
2019/03/16 22:30:45 == "\"text\""
2019/03/16 22:30:45 == "\"plain\""
2019/03/16 22:30:45 == []imap.Field{"\"charset\"", "\"utf-8\""}
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == "\"base64\""
2019/03/16 22:30:45 == 0x6a
2019/03/16 22:30:45 == 0x3
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 = 1
2019/03/16 22:30:45 == "\"text\""
2019/03/16 22:30:45 == "\"plain\""
2019/03/16 22:30:45 == []imap.Field{"\"charset\"", "\"utf-8\""}
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == "\"base64\""
2019/03/16 22:30:45 == 0x6a
2019/03/16 22:30:45 == 0x3
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 = 2
2019/03/16 22:30:45 == []imap.Field{"\"text\"", "\"plain\"", []imap.Field{"\"charset\"", "\"utf-8\""}, imap.Field(nil), imap.Field(nil), "\"base64\"", 0x66, 0x1, imap.Field(nil), imap.Field(nil), imap.Field(nil), imap.Field(nil)}
2019/03/16 22:30:45 == []imap.Field{"\"image\"", "\"jpeg\"", []imap.Field{"\"name\"", "\"Screenshot_20190315-090210.jpg\""}, imap.Field(nil), imap.Field(nil), "\"base64\"", 0xfd0b2, imap.Field(nil), []imap.Field{"\"attachment\"", []imap.Field{"\"filename\"", "\"Screenshot_20190315-090210.jpg\"", "\"size\"", "\"757415\""}}, imap.Field(nil), imap.Field(nil)}
2019/03/16 22:30:45 == "\"mixed\""
2019/03/16 22:30:45 == []imap.Field{"\"boundary\"", "\"--_com.android.email_70037410718460\""}
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 = 1
2019/03/16 22:30:45 == "\"text\""
2019/03/16 22:30:45 == "\"plain\""
2019/03/16 22:30:45 == []imap.Field{"\"charset\"", "\"utf-8\""}
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == "\"base64\""
2019/03/16 22:30:45 == 0x6a
2019/03/16 22:30:45 == 0x3
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 = 2
2019/03/16 22:30:45 == []imap.Field{"\"text\"", "\"plain\"", []imap.Field{"\"charset\"", "\"utf-8\""}, imap.Field(nil), imap.Field(nil), "\"base64\"", 0x66, 0x1, imap.Field(nil), imap.Field(nil), imap.Field(nil), imap.Field(nil)}
2019/03/16 22:30:45 == []imap.Field{"\"image\"", "\"jpeg\"", []imap.Field{"\"name\"", "\"Screenshot_20190315-090210.jpg\""}, imap.Field(nil), imap.Field(nil), "\"base64\"", 0xfd0b2, imap.Field(nil), []imap.Field{"\"attachment\"", []imap.Field{"\"filename\"", "\"Screenshot_20190315-090210.jpg\"", "\"size\"", "\"757415\""}}, imap.Field(nil), imap.Field(nil)}
2019/03/16 22:30:45 == "\"mixed\""
2019/03/16 22:30:45 == []imap.Field{"\"boundary\"", "\"--_com.android.email_70037410718460\""}
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
2019/03/16 22:30:45 == <nil>
答案 0 :(得分:1)
您应该选择一个最新的图书馆,您选择的图书馆四年来都没有任何贡献。这是一个https://github.com/emersion/go-imap库,用于png和gif的示例。
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")
}