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

multipart: NextPart: EOF error for multipart/mixed content type #174

Open
swagftw opened this issue Dec 22, 2023 · 9 comments
Open

multipart: NextPart: EOF error for multipart/mixed content type #174

swagftw opened this issue Dec 22, 2023 · 9 comments

Comments

@swagftw
Copy link

swagftw commented Dec 22, 2023

I was trying the read the mail using go-imap and go-message libraries, but for a particular mail I am getting
multipart: NextPart: EOF error on part, err := reader.NextPart() where reader is the mail.CreateReader.

content type of the email is as following. I got it from gmail client show original mail

Content-Type: multipart/mixed; boundary="-----SECBOUND"

-------SECBOUND
Content-Type: text/html
Content-Transfer-Encoding: 8bit

html content only

-------SECBOUND--
@emersion
Copy link
Owner

What is the code reading this message?

NextPart will return EOF when there are not more parts to read.

@swagftw
Copy link
Author

swagftw commented Dec 22, 2023

part, err := reader.NextPart()
if err == io.EOF {
	break
} else if err != nil && !message.IsUnknownCharset(err) {
	break
}

From so long I was thinking that this is a bug because I was already handling the io.EOF error, but as you said it should be just another EOF error, I dug deeper.

The code is pretty straight forward and I am not doing anything fancy here. The error is getting populated through

return nil, fmt.Errorf("multipart: NextPart: %v", err)

But I am not sure why it is not just another io.EOF even though the whole body itself is of multipart/mixed type. Also we don't have a concrete error type to check/match the error if this occurs.

How do you think is the right way to handle this error for now?

@emersion
Copy link
Owner

I've tried running your code with your source message, but I don't get the error: https://go.dev/play/p/cSU0T0XgRL7

@rexxDigital
Copy link

This still happens by the way, got me confused. when just returning an io.EOF would be sufficient.

@emersion
Copy link
Owner

Please post a playground link with a reproducer, if you can reproduce.

@rexxDigital
Copy link

rexxDigital commented Dec 19, 2024

Please post a playground link with a reproducer, if you can reproduce.

Cant post with the actual mail since its private info. But this happens rarely with mails received from certain platforms. Wouldnt returning an io.EOF be just fine here? or create an error type like MultipartEOF?

Can create a pr for this if you want me to

@emersion
Copy link
Owner

No, the error happens when EOF is unexpected in the middle of a multipart message, so it doesn't make sense to ignore it or treat it like a regular EOF.

@rexxDigital
Copy link

rexxDigital commented Dec 19, 2024

No, the error happens when EOF is unexpected in the middle of a multipart message, so it doesn't make sense to ignore it or treat it like a regular EOF.

I see, from my own testing when this happens we have already gotten the body in both html and plain text. Letting the loop run ends in it never breaking as it does not reach the io.EOF return. Having an error type for this would good so we can atleast check for it.

Edit: While debugging i see that it does indeed reach EOF, the mail is wrongly formatted and does not have the --boundary-- after the html part.

@emersion
Copy link
Owner

If you don't care about errors after the parts you're interested in, you can stop the loop as soon as you've seen these parts.

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

3 participants