Fix: Email Parsing for html fix (#3659)

This commit is contained in:
Tejaswini Chile
2022-01-21 05:25:35 +05:30
committed by GitHub
parent a7c947aeae
commit e997aaceb7
3 changed files with 85 additions and 28 deletions

View File

@@ -8,30 +8,49 @@ class MailPresenter < SimpleDelegator
end
def subject
encode_to_unicode(@mail.subject || '')
encode_to_unicode(@mail.subject)
end
def text_content
@decoded_text_content ||= encode_to_unicode(text_part&.decoded || decoded_message || '')
@decoded_text_content = select_body(text_part) || ''
encoding = @decoded_text_content.encoding
body = EmailReplyTrimmer.trim(@decoded_text_content)
return {} if @decoded_text_content.blank?
@text_content ||= {
full: @decoded_text_content,
reply: extract_reply(@decoded_text_content)[:reply],
quoted: extract_reply(@decoded_text_content)[:quoted_text]
full: select_body(text_part),
reply: @decoded_text_content,
quoted: body.force_encoding(encoding).encode('UTF-8')
}
end
# returns encoded mail body text_part if available.
# else returns parsed the html body if contains text/html content.
def select_body(mail_part)
return '' unless mail_part
decoded = encode_to_unicode(mail_part.decoded)
if mail.text_part
decoded
elsif (mail.content_type || '').include? 'text/html'
::HtmlParser.parse_reply(decoded)
end
end
def html_content
@decoded_html_content ||= encode_to_unicode(html_part&.decoded)
@decoded_html_content = select_body(html_part) || ''
return {} if @decoded_html_content.blank?
body = EmailReplyTrimmer.trim(@decoded_html_content)
@html_content ||= {
full: @decoded_html_content,
reply: extract_reply(@decoded_html_content)[:reply],
quoted: extract_reply(@decoded_html_content)[:quoted_text]
full: select_body(html_part),
reply: @decoded_html_content,
quoted: body
}
end
@@ -47,14 +66,6 @@ class MailPresenter < SimpleDelegator
end
end
def decoded_message
if mail.multipart?
return mail.text_part ? mail.text_part.decoded : nil
end
mail.decoded
end
def number_of_attachments
mail.attachments.count
end
@@ -114,13 +125,7 @@ class MailPresenter < SimpleDelegator
return str if current_encoding == 'UTF-8'
str.encode(current_encoding, 'UTF-8', invalid: :replace, undef: :replace, replace: '?')
end
def extract_reply(content)
# NOTE: implement the reply parser over here
{
reply: content.strip,
quoted_text: content.strip
}
rescue StandardError
''
end
end