From de4e4c6f65e32fc3259686dbe3fdc18c52ef1555 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Mon, 14 Feb 2022 20:22:58 +0530 Subject: [PATCH] fix: html content is available and mail_part empty (#3964) --- app/presenters/mail_presenter.rb | 25 +++-- spec/fixtures/files/support_1.eml | 144 +++++++++++++++++++++++++ spec/mailboxes/support_mailbox_spec.rb | 23 ++++ 3 files changed, 183 insertions(+), 9 deletions(-) create mode 100644 spec/fixtures/files/support_1.eml diff --git a/app/presenters/mail_presenter.rb b/app/presenters/mail_presenter.rb index 4ad650db8..e42bbbf7b 100644 --- a/app/presenters/mail_presenter.rb +++ b/app/presenters/mail_presenter.rb @@ -27,17 +27,21 @@ class MailPresenter < SimpleDelegator end # returns encoded mail body text_part if available. - # returns encoded mail body as it is if mail_part not available. + # returns encoded raw mail body as it is if mail_part not available. # else returns parsed the html body if contains text/html content. def select_body(mail_part) - return encoded_mail_body unless mail_part + decoded = if mail_part + mail_part.decoded + else + raw_mail_body + end - decoded = encode_to_unicode(mail_part.decoded) + encoded = encode_to_unicode(decoded) if mail.text_part - decoded + encoded elsif html_mail_body? - ::HtmlParser.parse_reply(decoded) + ::HtmlParser.parse_reply(encoded) end end @@ -131,12 +135,15 @@ class MailPresenter < SimpleDelegator end def html_mail_body? - ((mail.content_type || '').include? 'text/html') || @mail.html_part || @mail.html_part.content_type.include?('text/html') + ((mail.content_type || '').include? 'text/html') || @mail.html_part&.content_type&.include?('text/html') end - # returns mail body if mail content_type is text/plain - def encoded_mail_body - return encode_to_unicode(@mail.body.decoded) if (@mail.content_type || '').include? 'text/plain' + def text_mail_body? + ((mail.content_type || '').include? 'text') || @mail.text_part&.content_type&.include?('text') + end + + def raw_mail_body + return @mail.body.decoded if html_mail_body? || text_mail_body? '' end diff --git a/spec/fixtures/files/support_1.eml b/spec/fixtures/files/support_1.eml new file mode 100644 index 000000000..c16f461cf --- /dev/null +++ b/spec/fixtures/files/support_1.eml @@ -0,0 +1,144 @@ +Date: Fri, 11 Feb 2022 05:15:51 +0000 +Mime-Version: 1.0 +Subject: Get Paid to post an article +From: sony@chatwoot.com +To: care@example.com +X-Mailgun-Tag: test1 +X-Mailgun-Track-Clicks: true +X-Mailgun-Track: true +X-Mailgun-Track-Opens: true +Message-Id: <20220211051551.0ac6490aa10da09b@chatwoot.com> +Content-Type: text/html; charset="ascii" +Content-Transfer-Encoding: quoted-printable + + + + + + + + + + + + + +
+Hi,

We are providing you platform from here you can sell= +paid posts on your website. + +

Chatwoot | CS team | C

+

Skype: live:.cid.something

+ +
+3D"" + + diff --git a/spec/mailboxes/support_mailbox_spec.rb b/spec/mailboxes/support_mailbox_spec.rb index 1dda7be31..e25cee6de 100644 --- a/spec/mailboxes/support_mailbox_spec.rb +++ b/spec/mailboxes/support_mailbox_spec.rb @@ -156,5 +156,28 @@ RSpec.describe SupportMailbox, type: :mailbox do expect(conversation_1.messages.count).to eq(2) end end + + describe 'when mail part is not present' do + let(:support_mail) { create_inbound_email_from_fixture('support_1.eml') } + let(:described_subject) { described_class.receive support_mail } + + it 'Considers raw html mail body' do + described_subject + expect(conversation.inbox.id).to eq(channel_email.inbox.id) + expect(conversation.messages.last.content).to include( + <<-BODY.strip_heredoc.chomp + Hi, + We are providing you platform from here you can sellpaid posts on your website. + + Chatwoot | CS team | [C](https://d33wubrfki0l68.cloudfront.net/973467c532160fd8b940300a43fa85fa2d060307/dc9a0/static/brand-73f58cdefae282ae74cebfa74c1d7003.svg) + + Skype: live:.cid.something + + [] + BODY + ) + expect(conversation.messages.last.content_attributes['email']['subject']).to eq('Get Paid to post an article') + end + end end end