英文:
Extracting attachment from email file using GOLANG
问题
我一直在使用Golang解析电子邮件。现在我正在提取附件的部分。我已经查看了Golang的MIME和MIME/multipart库,但它们没有提供任何用于提取附件的方法或函数。
具体而言,我想要做的是:例如
我有一个带有附件file1.txt、file2.pdf和file3.png的电子邮件文件。我已经成功解析了邮件正文。现在我想要提取附件并将它们保存到一个单独的目录中。我已经搜索了Golang的所有部分,包括MIME和MIME/multipart,它们似乎没有这个功能。Golang能够实现这个吗?如果可以,有什么提示或线索吗?
英文:
I have been working on parsing email using golang. I am now in the part of extracting the attachments. I have looked into golang lib MIME and MIME/multipart. But it does not have any methods or function to do this.
What specifically I want to do is: Example
I have an email file with attachments file1.txt, file2.pdf, and file3.png. I have successfully parsed the email body. Now I want to extract the attachment and save them on a separate directory. I have searched all part of golang including MIME and MIME/multipart. They seem to not have this functionality. Can golang do this? if Yes any hint or clue please.
答案1
得分: 2
我找到了一个解决方案,它使用了DusanKasan的parsemail函数。
import (
"github.com/DusanKasan/parsemail"
)
func readEmail() error {
b := getYourEmail()
email, err := parsemail.Parse(bytes.NewBuffer(b))
if err != nil{
return err
}
for _, a := range email.Attachments{
// 处理附件
}
}
这段代码使用了DusanKasan的parsemail函数来解析电子邮件。它首先获取电子邮件的内容,然后使用parsemail.Parse函数将其解析为email对象。接下来,通过遍历email对象的Attachments属性,可以对附件进行处理。
英文:
I found a solution to this that uses the parsemail function from DusanKasan
import (
"github.com/DusanKasan/parsemail"
)
func readEmail() error {
b := getYourEmail()
email, err := parsemail.Parse(bytes.NewBuffer(b))
if err != nil{
return err
}
for _, a := range email.Attachments{
// do stuff with attachment
}
}
答案2
得分: 0
我认为首先你应该找到边界:
> Content-Type: multipart/mixed; boundary={sample-boundary}
然后你可以通过该sample-boundary
将邮件分割开。
最后,你可以获取附件的Base64编码部分。
我正在处理这个问题。完成后我会回来的。
英文:
I think firstly you should find the boundary of:
> Content-Type: multipart/mixed; boundary={sample-boundary}
Then you split the email by that sample-boundary
.
And finally you get the base64 encoding part of attachment.
I'm current working on this. I'll be back when I'm done.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论