Jesse
Jesse

Reputation: 21

Extracting attachment from email file using 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.

Upvotes: 2

Views: 3879

Answers (2)

Sebastian
Sebastian

Reputation: 1853

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
    }
}

Upvotes: 2

kudo28
kudo28

Reputation: 26

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.

Upvotes: 0

Related Questions