gpt4 book ai didi

How to download email attachment using go-imap v2?(如何使用Go-IMAP v2下载电子邮件附件?)

转载 作者:bug小助手 更新时间:2023-10-25 15:28:12 24 4
gpt4 key购买 nike



I'm trying to implement this workflow using Go (with go-imap v2):

我正在尝试使用Go(使用Go-IMAP v2)实现此工作流:



  1. Scan inbox, look for email from a certain sender, e.g [email protected]

  2. Download the Excel attachment

  3. Parse the Excel, feed it to another API


This is my code. Part 1 is roughly done.

这是我的密码。第一部分大致完成了。


package main

import (
"fmt"

"github.com/emersion/go-imap/v2"
"github.com/emersion/go-imap/v2/imapclient"
)

func generateIdRange(startIdx, endIdx uint32) []uint32 {
var x uint32
var theSize = endIdx - startIdx
var theSlice = make([]uint32, theSize)
for x = 0; x < theSize; x++ {
theSlice[x] = startIdx + x
}
return theSlice
}


func main() {

client, client_err := imapclient.DialTLS("xxx.xxx.xxx:993", nil)
if client_err != nil {
fmt.Printf("Failed to dial IMAP server: %v", client_err)
} else {
fmt.Printf("Succesfully connected to IMAP server...\n")
}
defer client.Close()

if client_err := client.Login("xxxxx", "xxxxx").Wait(); client_err != nil {
fmt.Printf("Failed to login: %v", client_err)
} else {
fmt.Printf("Succesfully logged in...\n")
}

mailboxes, client_err := client.List("", "%", nil).Collect()
if client_err != nil {
fmt.Printf("Failed to list mailboxes: %v\n", client_err)
} else {
fmt.Printf("Found %v mailboxes\n", len(mailboxes))
for _, mbox := range mailboxes {
fmt.Printf(" - %v\n", mbox.Mailbox)
}
}

selectedMbox, client_err := client.Select("INBOX", nil).Wait()
if client_err != nil {
fmt.Printf("Failed to select INBOX: %v\n", client_err)
} else {
fmt.Printf("INBOX contains %v messages\n", selectedMbox.NumMessages)
}

if selectedMbox.NumMessages > 0 {

theArr := generateIdRange(1, selectedMbox.NumMessages)
seqSet := imap.SeqSetNum(theArr...)

fetchOptions := &imap.FetchOptions{Envelope: true}
messages, client_err := client.Fetch(seqSet, fetchOptions).Collect()
if client_err != nil {
fmt.Printf("Failed to fetch first message in INBOX: %v\n", client_err)
} else {
var x uint32
for x = 0; x < selectedMbox.NumMessages-1; x++ {
var sender = messages[x].Envelope.Sender[0].Mailbox + "@" + messages[x].Envelope.Sender[0].Host
if sender == "[email protected]" {
// OK this is email from [email protected]
// Next step is grab the Excel attachment.....
}
}
}
}

if err := client.Logout().Wait(); err != nil {
fmt.Printf("Failed to logout: %v\n", err)
}
}

Now my question is how to grab the Excel attachment? I found this example: Fetching messages. Copy pasting it causes errors like:

现在我的问题是如何获取Excel附件?我发现了这个例子:获取消息。复制粘贴会导致如下错误:



undefined: imap.BodySectionName


undefined: imap.FetchItem



This is my modified code, btw:
https://gist.github.com/anta40/6c04227c7a28b781e6210da3f2757d57

这是我修改后的代码,btw:https://gist.github.com/anta40/6c04227c7a28b781e6210da3f2757d57


更多回答
优秀答案推荐
更多回答

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