From 8e5eb140de7484ceab2a80456538965927fffeda Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Wed, 24 Dec 2025 05:11:18 +0900 Subject: [PATCH] Decode by Mail gem's decoded before `Kconv`ing body is now decoded by the hybrid approach as follows: body.decoded: handles Content-Transfer-Encoding (base64, quoted-printable) Kconv.toutf8: auto-detects charset (handles mislabeled charsets in legacy emails) --- app/models/message.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/message.rb b/app/models/message.rb index 62c9b30..3bf3ad7 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -84,9 +84,11 @@ def from_mail(mail, list, list_seq) file = StringIO.new(part.decoded) attachments.attach(io: file, filename: part.filename || 'noname', content_type: part.content_type) when /^text\/plain/, /text\/enriched;/, 'message/rfc822', nil - (self.body ||= ''.dup) << Kconv.toutf8(part.body.raw_source) + # body.decoded: handles Content-Transfer-Encoding (base64, quoted-printable) + # Kconv.toutf8: auto-detects charset (handles mislabeled charsets in legacy emails) + (self.body ||= ''.dup) << Kconv.toutf8(part.body.decoded) when /^text\/html/ - (self.html_body ||= ''.dup) << Kconv.toutf8(part.body.raw_source) + (self.html_body ||= ''.dup) << Kconv.toutf8(part.body.decoded) when 'application/octet-stream', 'image/gif', 'application/rtf', 'message/delivery-status' # there can be an attachment with nil part.filename (which is equivalent to part.attachment?). file = StringIO.new(part.decoded)