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

message: DecodeTextAttachments option (FR #182) #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,29 @@ func decodeHeader(s string) (string, error) {
func encodeHeader(s string) string {
return mime.QEncoding.Encode("utf-8", s)
}

// DecodeTextAttachments turns on charset decoding to UTF-8 for all multipart
// `text/*` parts. It is `true` by default for backward compatibility. All
// `text/*` parts either `inline` or `attachment` Content-Disposition will be
// decoded to UTF-8. This may cause data corruption for incorrect or invalid
// emails, for example when defined charset doesn't match actual charset.
//
// When set to `false`, the `text/*` parts with Content-Disposition set to
// `attachment` will NOT be decoded and will be returned "as is".
//
// https://github.com/emersion/go-message/issues/182
var DecodeTextAttachments = true

// needDecodeTextPart returns whether we need to decode text/* part to UTF-8
//
// https://github.com/emersion/go-message/issues/182
func needDecodeTextPart(header Header) bool {
if DecodeTextAttachments {
return true
}
disposition, _, err := header.ContentDisposition()
if err == nil && disposition == "attachment" {
return false
}
return true
}
2 changes: 1 addition & 1 deletion entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func New(header Header, body io.Reader) (*Entity, error) {
}

// RFC 2046 section 4.1.2: charset only applies to text/*
if strings.HasPrefix(mediaType, "text/") {
if strings.HasPrefix(mediaType, "text/") && needDecodeTextPart(header) {
if ch, ok := mediaParams["charset"]; ok {
if converted, charsetErr := charsetReader(ch, body); charsetErr != nil {
err = UnknownCharsetError{charsetErr}
Expand Down