Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot parse part "multipart/mixed" with boundary #68

Open
bsc-computer opened this issue Jan 4, 2024 · 1 comment
Open

cannot parse part "multipart/mixed" with boundary #68

bsc-computer opened this issue Jan 4, 2024 · 1 comment

Comments

@bsc-computer
Copy link

Part of eml-file

`X-MS-Exchange-Organization-Processed-By-Gcc-Journaling: Journal Agent
Content-Type: multipart/mixed;
boundary="a5093c80-52fc-4ad9-bca7-3906fe47922d"

--a5093c80-52fc-4ad9-bca7-3906fe47922d
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable`

code

email, err := letters.ParseEmail(r) if err != nil { log.Fatal(err) }

output

letters.parsers.parsePart: cannot parse Content-Disposition: letters.parsers.parseContentDisposition: unknown Content-Disposition "attachfile"

Normaly letters works fine.
It seems a special structure of this emails
I have some eml-files to produce the error. (Only via private mail)

workaround

(with standard go libraries isit possible to extract attachments without errors)

`package main

import (
"encoding/base64"
"fmt"
"io"
"mime"
"mime/multipart"
"net/mail"
"os"
"strings"
)

func main() {

emlFile, err := os.Open(`test.eml`)
if err != nil {
	panic(err)
}
defer emlFile.Close()


msg, err := mail.ReadMessage(emlFile)
if err != nil {
	panic(err)
}

// Get MIME-Typ
mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
if err != nil {
	panic(err)
}

//  multipart-message?
if strings.HasPrefix(mediaType, "multipart/") {
	mr := multipart.NewReader(msg.Body, params["boundary"])
	for {
		p, err := mr.NextPart()
		if err == io.EOF {
			break
		}
		if err != nil {
			panic(err)
		}

		if cd := p.Header.Get("Content-Disposition"); strings.Contains(cd, "attachFile") {
			filename := p.FileName()

			//  Content-Transfer-Encoding-Header
			var reader io.Reader
			switch p.Header.Get("Content-Transfer-Encoding") {
			case "base64":
				reader = base64.NewDecoder(base64.StdEncoding, p)
			default:
				reader = p
			}

			data, err := io.ReadAll(reader)
			if err != nil {
				panic(err)
			}

			// save attachment to file
			err = os.WriteFile(filename, data, 0644)
			if err != nil {
				panic(err)
			}
			fmt.Printf("attachment %s saved\n", filename)
		}
	}
}

}
`

@manuraj17
Copy link

@bsc-computer Could you share the Content-Disposition header or a bit more elaborate sample eml file with which we can reproduce this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants