fix: html content is available and mail_part empty (#3964)

This commit is contained in:
Tejaswini Chile 2022-02-14 20:22:58 +05:30 committed by GitHub
parent f34a2e6115
commit de4e4c6f65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 183 additions and 9 deletions

View file

@ -27,17 +27,21 @@ class MailPresenter < SimpleDelegator
end end
# returns encoded mail body text_part if available. # 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. # else returns parsed the html body if contains text/html content.
def select_body(mail_part) 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 if mail.text_part
decoded encoded
elsif html_mail_body? elsif html_mail_body?
::HtmlParser.parse_reply(decoded) ::HtmlParser.parse_reply(encoded)
end end
end end
@ -131,12 +135,15 @@ class MailPresenter < SimpleDelegator
end end
def html_mail_body? 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 end
# returns mail body if mail content_type is text/plain def text_mail_body?
def encoded_mail_body ((mail.content_type || '').include? 'text') || @mail.text_part&.content_type&.include?('text')
return encode_to_unicode(@mail.body.decoded) if (@mail.content_type || '').include? 'text/plain' end
def raw_mail_body
return @mail.body.decoded if html_mail_body? || text_mail_body?
'' ''
end end

144
spec/fixtures/files/support_1.eml vendored Normal file
View file

@ -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
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8=
" />
<meta name=3D"viewport" content=3D"width=3Ddevice-width, initial-scale=
=3D1">
<meta http-equiv=3D"X-UA-Compatible" content=3D"IE=3Dedge" />
<style type=3D"text/css">
@media screen {
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 400;
src: local('Lato Regular'), local('Lato-Regular'), url(http=
s://fonts.gstatic.com/s/lato/v11/qIIYRU-oROkIk8vfvxw6QvesZW2xOQ-xsNqO47m55D=
A.woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: normal;
font-weight: 700;
src: local('Lato Bold'), local('Lato-Bold'), url(https://fo=
nts.gstatic.com/s/lato/v11/qdgUG4U09HnJwhYI-uK18wLUuEpTyoUstqEm5AMlJo4.woff=
) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: italic;
font-weight: 400;
src: local('Lato Italic'), local('Lato-Italic'), url(https:=
//fonts.gstatic.com/s/lato/v11/RYyZNoeFgb0l7W3Vu1aSWOvvDin1pK8aKteLpeZ5c0A.=
woff) format('woff');
}
@font-face {
font-family: 'Lato';
font-style: italic;
font-weight: 700;
src: local('Lato Bold Italic'), local('Lato-BoldItalic'), u=
rl(https://fonts.gstatic.com/s/lato/v11/HkF_qI1x_noxlxhrhMQYELO3LdcAZYWl9Si=
6vvxL-qU.woff) format('woff');
}
}
/* CLIENT-SPECIFIC STYLES */
body,
table,
td,
a {
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
table,
td {
mso-table-lspace: 0pt;
mso-table-rspace: 0pt;
}
td{
}
img {
-ms-interpolation-mode: bicubic;
}
/* RESET STYLES */
img {
border: 0;
height: auto;
line-height: 100%;
outline: none;
text-decoration: none;
}
table {
border-collapse: collapse !important;
}
body {
height: 100% !important;
margin: 0 !important;
padding: 0 !important;
width: 100% !important;
background-color: black;
}
/* iOS BLUE LINKS */
a[x-apple-data-detectors] {
color: inherit !important;
text-decoration: none !important;
font-size: inherit !important;
font-family: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
}
/* MOBILE STYLES */
@media screen and (max-width:600px) {
h1 {
font-size: 32px !important;
line-height: 32px !important;
}
}
/* ANDROID CENTER FIX */
div[style*=3D"margin: 16px 0;"] {
margin: 0 !important;
}
</style>
</head>
<body>
<div>
Hi,<br /><br />We are providing you platform from here you can sell=
paid posts on your website.
<div style=3D'width:100%'><p>Chatwoot | CS team | <a href=3D"https://d33wubrfki0l68.cloudfront.net/973467c532160fd8b940300a43fa85fa2d060307/dc9a0/static/brand-73f58cdefae282ae74cebfa74c1d7003.svg">C</hatwoota></p>
<p>Skype: live:.cid.something</p>
</d=
iv>
</div>
<img width=3D"1px" height=3D"1px" alt=3D"" src=3D"https://d33wubrfki0l68.cloudfront.net/973467c532160fd8b940300a43fa85fa2d060307/dc9a0/static/brand-73f58cdefae282ae74cebfa74c1d7003.svg"></=
body>
</html>

View file

@ -156,5 +156,28 @@ RSpec.describe SupportMailbox, type: :mailbox do
expect(conversation_1.messages.count).to eq(2) expect(conversation_1.messages.count).to eq(2)
end end
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
end end