使用GOLANG从电子邮件文件中提取附件。

huangapple go评论164阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2017年6月12日 17:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/44496434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定