gpt4 book ai didi

go - 如何使用mxk/go-imap查找附件并下载?

转载 作者:IT王子 更新时间:2023-10-29 01:43:48 27 4
gpt4 key购买 nike

这几天一直在努力解决这个问题。我正在尝试下载邮箱中所有电子邮件的图像附件。

在这里和那里调查发现这个 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>

最佳答案

你应该选择一个更新的库,你选择的库已经 4 年没有任何贡献了。这里有一个例子 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")
}

关于go - 如何使用mxk/go-imap查找附件并下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55203878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com