From 381c448627001986e835a5119718b9279eabef7d Mon Sep 17 00:00:00 2001 From: Sony Mathew Date: Tue, 22 Sep 2020 11:26:41 +0530 Subject: [PATCH] fix: Email encoding issue - utf8 (#1264) There were issues in parsing Arabic or UTF characters (emojis) correctly for the incoming emails. All these characters were converted to question marks which is teh fallback character when an encoding is enforced and there is a missing matching character. --- app/presenters/mail_presenter.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/presenters/mail_presenter.rb b/app/presenters/mail_presenter.rb index a320c1ce0..40070fd04 100644 --- a/app/presenters/mail_presenter.rb +++ b/app/presenters/mail_presenter.rb @@ -12,7 +12,7 @@ class MailPresenter < SimpleDelegator end def text_content - @decoded_text_content ||= encode_to_unicode(text_part&.body&.decoded || fallback_content) + @decoded_text_content ||= encode_to_unicode(text_part&.decoded || fallback_content) @text_content ||= { full: @decoded_text_content, reply: extract_reply(@decoded_text_content)[:reply], @@ -21,7 +21,7 @@ class MailPresenter < SimpleDelegator end def html_content - @decoded_html_content ||= encode_to_unicode(html_part&.body&.decoded || fallback_content) + @decoded_html_content ||= encode_to_unicode(html_part&.decoded || fallback_content) @html_content ||= { full: @decoded_html_content, reply: extract_reply(@decoded_html_content)[:reply], @@ -70,6 +70,8 @@ class MailPresenter < SimpleDelegator # forcing the encoding of the content to UTF-8 so as to be compatible with database and serializers def encode_to_unicode(str) current_encoding = str.encoding.name + return str if current_encoding == 'UTF-8' + str.encode(current_encoding, 'UTF-8', invalid: :replace, undef: :replace, replace: '?') end