emersion / go-message

✉️ A streaming Go library for the Internet Message Format and mail messages
MIT License
373 stars 108 forks source link

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

Open swagftw opened 7 months ago

swagftw commented 7 months ago

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 commented 7 months ago

What is the code reading this message?

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

swagftw commented 7 months ago
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 https://github.com/emersion/go-message/blob/8138ff9fec34e40fc969e98c7e6f5b76baff30d7/textproto/multipart.go#L262

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 commented 6 months ago

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