From 821b953ee9b02d68ab2152fb05ac7a437b0708e4 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Wed, 6 Apr 2022 12:36:32 +0530 Subject: [PATCH] Fix: handling UTF encoded mail (#4384) --- app/presenters/mail_presenter.rb | 2 +- spec/fixtures/files/non_utf_encoded_mail.eml | 15 +++++++++++++++ spec/presenters/mail_presenter_spec.rb | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/files/non_utf_encoded_mail.eml diff --git a/app/presenters/mail_presenter.rb b/app/presenters/mail_presenter.rb index f2d9ac92c..ff355c9f3 100644 --- a/app/presenters/mail_presenter.rb +++ b/app/presenters/mail_presenter.rb @@ -33,7 +33,7 @@ class MailPresenter < SimpleDelegator # encodes mail raw body if mail.content_type is plain/text # encodes mail raw body if mail.content_type is html/text def text_html_mail(mail_part) - decoded = mail_part&.decoded || @mail.body&.decoded + decoded = mail_part&.decoded || @mail.decoded encoded = encode_to_unicode(decoded) encoded if html_mail_body? || text_mail_body? diff --git a/spec/fixtures/files/non_utf_encoded_mail.eml b/spec/fixtures/files/non_utf_encoded_mail.eml new file mode 100644 index 000000000..78e6e55b1 --- /dev/null +++ b/spec/fixtures/files/non_utf_encoded_mail.eml @@ -0,0 +1,15 @@ +In-Reply-To: <4e6e35f5a38b4_479f13bb90078178@small-app-01.mail>, <4e6e35f5a38b4_479f13bb90078178@small-app-01.mail> +To: "Replies" +From: "=?UTF-8?B?2YXYqtis2LEg2LPZiNmCINmG2Ko=?=" +Date: Sun, 3 Apr 2022 11:48:20 -0700 +Message-ID: +Subject: =?UTF-8?Q?=D8=A3=D9=87=D9=84=D9=8A=D9=86_=D8=B9?= =?UTF-8?Q?=D9=85=D9=8A=D9=84=D9=86=D8=A7_=D8=A7=D9=84?= =?UTF-8?Q?=D9=83=D8=B1=D9=8A=D9=85_?= +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: base64 +Content-Disposition: inline +Precedence: bulk +X-Autoreply: yes +Auto-Submitted: auto-replied + +2KPZhti42LHZiNin2Iwg2KPZhtinINij2K3Yqtin2KzZh9inINmB2YLYtyDZhNiq2YLZiNmFINio2KfZhNiq2K/ZgtmK2YIg2YHZiiDZhdmC2KfZhNiq2Yog2KfZhNi02K7YtdmK2KkK diff --git a/spec/presenters/mail_presenter_spec.rb b/spec/presenters/mail_presenter_spec.rb index c1049e944..a39559cc4 100644 --- a/spec/presenters/mail_presenter_spec.rb +++ b/spec/presenters/mail_presenter_spec.rb @@ -5,6 +5,7 @@ RSpec.describe MailPresenter do describe 'parsed mail decorator' do let(:mail) { create_inbound_email_from_fixture('welcome.eml').mail } let(:html_mail) { create_inbound_email_from_fixture('welcome_html.eml').mail } + let(:ascii_mail) { create_inbound_email_from_fixture('non_utf_encoded_mail.eml').mail } let(:decorated_mail) { described_class.new(mail) } let(:mail_with_no_subject) { create_inbound_email_from_fixture('mail_with_no_subject.eml').mail } @@ -65,5 +66,13 @@ RSpec.describe MailPresenter do "I'm learning English as a first language for the past 13 years, but to " ) end + + it 'encodes email to UTF-8' do + decorated_html_mail = described_class.new(ascii_mail) + expect(decorated_html_mail.subject).to eq('أهلين عميلنا الكريم ') + expect(decorated_html_mail.text_content[:reply][0..70]).to eq( + 'أنظروا، أنا أحتاجها فقط لتقوم بالتدقيق في مقالتي الشخصية' + ) + end end end