Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions imap/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,28 @@ func singlePartStructure(section *rfc822.Section, fields *paramList, writer *dua
return err
}

// Per RFC2045, if Content-Transfer-Encoding is missing, then we should assuume
// that the content is `7BIT`.
contentTransferEncoding := header.Get("Content-Transfer-Encoding")
if contentTransferEncoding == "" {
contentTransferEncoding = "7BIT"
}

fields.
addString(writer, mimeType).
addString(writer, mimeSubType).
addMap(writer, mimeParams).
addString(writer, mimeSubType)

// Per RFC3501 if `body-field-params` should be NIL if empty
if len(mimeParams) == 0 {
fields.addString(writer, "NIL")
} else {
fields.addMap(writer, mimeParams)
}

fields.
addString(writer, header.Get("Content-Id")).
addString(writer, header.Get("Content-Description")).
addString(writer, header.Get("Content-Transfer-Encoding")).
addString(writer, contentTransferEncoding).
addNumber(writer, len(section.Body()))

if mimeType == "message" && mimeSubType == "rfc822" {
Expand Down
2 changes: 1 addition & 1 deletion imap/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ hey there bro
require.NoError(t, err)
require.NotNil(t, parsed)

expected := "((\"text\" \"plain\" (\"charset\" \"utf-8\") NIL NIL \"quoted-printable\" 6 2)(\"message\" \"rfc822\" (\"name\" \"ISO-8859-1.eml\") NIL NIL NIL 127 (NIL \"ISO-8859-1\" ((NIL NIL \"random-mail\" \"pm.me\")) ((NIL NIL \"random-mail\" \"pm.me\")) ((NIL NIL \"random-mail\" \"pm.me\")) ((NIL NIL \"random-mail2\" \"pm.me\")) NIL NIL NIL NIL)(\"text\" \"plain\" (\"charset\" \"iso-8859-1\") NIL NIL NIL 14 1) 6) \"mixed\")"
expected := "((\"text\" \"plain\" (\"charset\" \"utf-8\") NIL NIL \"quoted-printable\" 6 2)(\"message\" \"rfc822\" (\"name\" \"ISO-8859-1.eml\") NIL NIL \"7BIT\" 127 (NIL \"ISO-8859-1\" ((NIL NIL \"random-mail\" \"pm.me\")) ((NIL NIL \"random-mail\" \"pm.me\")) ((NIL NIL \"random-mail\" \"pm.me\")) ((NIL NIL \"random-mail2\" \"pm.me\")) NIL NIL NIL NIL)(\"text\" \"plain\" (\"charset\" \"iso-8859-1\") NIL NIL \"7BIT\" 14 1) 6) \"mixed\")"
require.Equal(t, expected, parsed.Body)
}

Expand Down
13 changes: 12 additions & 1 deletion tests/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,17 @@ func newEmptyBodyStructureValidator() *bodyStructureValidator {
require.Empty(tb, s)
}

validEncoding := func(tb testing.TB, s string) {
validValues := []string{"7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE", ""}
tb.Helper()
for _, value := range validValues {
if strings.EqualFold(value, s) {
return
}
}
require.Failf(tb, "string not in allowed values", "got %q, want one of %v", s, validValues)
}

return &bodyStructureValidator{
validateMIMEType: emptyString,
validateMimeSubType: emptyString,
Expand All @@ -888,7 +899,7 @@ func newEmptyBodyStructureValidator() *bodyStructureValidator {
},
validateID: emptyString,
validateDescription: emptyString,
validateEncoding: emptyString,
validateEncoding: validEncoding,
validateSize: func(tb testing.TB, u uint32) {
require.Zero(tb, u)
},
Expand Down
Loading