go-imap icon indicating copy to clipboard operation
go-imap copied to clipboard

imapclient: Fetching BodyStructure fails with "imapwire: expected '(', got "N"

Open wheezyturbo opened this issue 9 months ago • 2 comments

Bug Report

Description of the Issue:

I am encountering an error when trying to fetch email messages using the emersion/go-imap/v2 library. The error occurs specifically when including the BodyStructure fetch option.

The error message I receive is:

2025/03/18 10:22:25 failed to fetch messages: in response-data: in body-type-mpart: in body-type-1part: imapwire: expected '(', got "N"
exit status 1

Here's the part of the program thats causing the problem


	fetchOpts := &imap.FetchOptions{
		Flags:    true,
		Envelope: true,
		UID:      true,
		// this is the part thats causing this exception
		//  :: 2025/02/28 04:57:15 failed to fetch messages: in response-data: in body-type-mpart: in body-type-1part: imapwire: expected '(', got "N"
		// 	   exit status 1
		BodyStructure: &imap.FetchItemBodyStructure{
			Extended: true,
		},
	}

	messages, err := c.Fetch(fetchSeqSet, fetchOpts).Collect()
	if err != nil {
		log.Fatalf("failed to fetch messages: %v", err)
	}

Here's the result when I get when i do it manually

? fetch 477 (bodystructure)
* 477 FETCH (BODYSTRUCTURE ("text" "html" ("charset" "utf-8") NIL NIL "quoted-printable" 5606 133 NIL NIL NIL NIL))
? OK Fetch completed (0.001 + 0.000 secs).

Additional Information

Go version:

  • go1.21.11 linux/amd64
  • go1.22.3 windows/amd64

Library Version:

  • github.com/emersion/go-imap/v2 v2.0.0-beta.5

Imap Server

  • Dovecot

wheezyturbo avatar Mar 18 '25 10:03 wheezyturbo

Indeed, sounds like the RFC allows NILs here and the parser is too strict.

emersion avatar Mar 18 '25 20:03 emersion

The example given in the original post seems to parse correctly for me, but I get this error when the message has a message/global attachment, because Dovecot doesn't send the message/rfc822 data when the content type is message/global.

The bodystructure data that gives me the error is:

(((("text" "plain" ("charset" "UTF-8") NIL NIL "quoted-printable" 576 31 NIL NIL NIL NIL)("text" "html" ("charset" "UTF-8") NIL NIL "quoted-printable" 4691 112 NIL NIL NIL NIL) "alternative" ("boundary" "----=_NextPart_002_0063_01D85E63.43189F50") NIL NIL NIL)("image" "png" ("name" "image001.png") "<[email protected]>" NIL "base64" 3832 NIL NIL NIL NIL) "related" ("boundary" "----=_NextPart_001_0062_01D85E63.43189F50") NIL NIL NIL)("message" "delivery-status" ("name" "details.txt") NIL NIL "7bit" 594 NIL ("attachment" ("filename" "details.txt")) NIL NIL)("message" "global" ("name" "Untitled attachment 00019.dat") NIL NIL "7bit" 6726 NIL ("attachment" ("filename" "Untitled attachment 00019.dat")) NIL NIL) "mixed" ("boundary" "----=_NextPart_000_0061_01D85E63.43189F50") NIL ("en-us") NIL)

andybalholm avatar Aug 02 '25 21:08 andybalholm