2020-04-30 14:50:26 +00:00
|
|
|
class MailPresenter < SimpleDelegator
|
|
|
|
attr_accessor :mail
|
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
def initialize(mail, account = nil)
|
2020-04-30 14:50:26 +00:00
|
|
|
super(mail)
|
|
|
|
@mail = mail
|
2020-05-22 12:37:06 +00:00
|
|
|
@account = account
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
2022-01-20 23:55:35 +00:00
|
|
|
encode_to_unicode(@mail.subject)
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 12:34:30 +00:00
|
|
|
# encode decoded mail text_part or html_part if mail is multipart email
|
|
|
|
# encode decoded mail raw bodyt if mail is not multipart email but the body content is text/html
|
|
|
|
def mail_content(mail_part)
|
|
|
|
if multipart_mail_body?
|
|
|
|
decoded_multipart_mail(mail_part)
|
|
|
|
else
|
|
|
|
text_html_mail(mail_part)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# encodes mail if mail.parts is present
|
|
|
|
# encodes mail content type is multipart
|
|
|
|
def decoded_multipart_mail(mail_part)
|
|
|
|
encoded = encode_to_unicode(mail_part&.decoded)
|
|
|
|
|
|
|
|
encoded if text_mail_body? || html_mail_body?
|
|
|
|
end
|
|
|
|
|
|
|
|
# encodes mail raw body if mail.parts is empty
|
|
|
|
# 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)
|
2022-04-06 07:06:32 +00:00
|
|
|
decoded = mail_part&.decoded || @mail.decoded
|
2022-03-30 12:34:30 +00:00
|
|
|
encoded = encode_to_unicode(decoded)
|
|
|
|
|
|
|
|
encoded if html_mail_body? || text_mail_body?
|
|
|
|
end
|
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
def text_content
|
2022-03-30 12:34:30 +00:00
|
|
|
@decoded_text_content = mail_content(text_part) || ''
|
|
|
|
|
2022-01-20 23:55:35 +00:00
|
|
|
encoding = @decoded_text_content.encoding
|
|
|
|
|
|
|
|
body = EmailReplyTrimmer.trim(@decoded_text_content)
|
2021-07-15 14:36:43 +00:00
|
|
|
|
2022-03-30 12:34:30 +00:00
|
|
|
return {} if @decoded_text_content.blank? || !text_mail_body?
|
2021-07-15 14:36:43 +00:00
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
@text_content ||= {
|
2022-03-30 12:34:30 +00:00
|
|
|
full: mail_content(text_part),
|
2022-01-20 23:55:35 +00:00
|
|
|
reply: @decoded_text_content,
|
|
|
|
quoted: body.force_encoding(encoding).encode('UTF-8')
|
2020-05-22 12:37:06 +00:00
|
|
|
}
|
|
|
|
end
|
2020-04-30 14:50:26 +00:00
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
def html_content
|
2022-03-30 12:34:30 +00:00
|
|
|
encoded = mail_content(html_part) || ''
|
|
|
|
@decoded_html_content = ::HtmlParser.parse_reply(encoded)
|
2021-07-15 14:36:43 +00:00
|
|
|
|
2022-03-30 12:34:30 +00:00
|
|
|
return {} if @decoded_html_content.blank? || !html_mail_body?
|
2021-07-15 14:36:43 +00:00
|
|
|
|
2022-01-20 23:55:35 +00:00
|
|
|
body = EmailReplyTrimmer.trim(@decoded_html_content)
|
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
@html_content ||= {
|
2022-03-30 12:34:30 +00:00
|
|
|
full: mail_content(html_part),
|
2022-01-20 23:55:35 +00:00
|
|
|
reply: @decoded_html_content,
|
|
|
|
quoted: body
|
2020-05-22 12:37:06 +00:00
|
|
|
}
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def attachments
|
|
|
|
# ref : https://github.com/gorails-screencasts/action-mailbox-action-text/blob/master/app/mailboxes/posts_mailbox.rb
|
|
|
|
mail.attachments.map do |attachment|
|
2021-08-03 14:41:52 +00:00
|
|
|
blob = ActiveStorage::Blob.create_and_upload!(
|
2020-04-30 14:50:26 +00:00
|
|
|
io: StringIO.new(attachment.body.to_s),
|
|
|
|
filename: attachment.filename,
|
|
|
|
content_type: attachment.content_type
|
|
|
|
)
|
|
|
|
{ original: attachment, blob: blob }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def number_of_attachments
|
|
|
|
mail.attachments.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_data
|
|
|
|
{
|
2021-08-09 12:34:34 +00:00
|
|
|
bcc: bcc,
|
|
|
|
cc: cc,
|
|
|
|
content_type: content_type,
|
2020-04-30 14:50:26 +00:00
|
|
|
date: date,
|
|
|
|
from: from,
|
2021-08-09 12:34:34 +00:00
|
|
|
html_content: html_content,
|
2020-04-30 14:50:26 +00:00
|
|
|
in_reply_to: in_reply_to,
|
2021-08-09 12:34:34 +00:00
|
|
|
message_id: message_id,
|
|
|
|
multipart: multipart?,
|
|
|
|
number_of_attachments: number_of_attachments,
|
|
|
|
subject: subject,
|
|
|
|
text_content: text_content,
|
|
|
|
to: to
|
2020-04-30 14:50:26 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-08-11 19:58:07 +00:00
|
|
|
def from
|
|
|
|
# changing to downcase to avoid case mismatch while finding contact
|
2022-02-10 13:50:30 +00:00
|
|
|
(@mail.reply_to.presence || @mail.from).map(&:downcase)
|
2021-08-11 19:58:07 +00:00
|
|
|
end
|
|
|
|
|
2021-09-16 07:56:52 +00:00
|
|
|
def sender_name
|
2022-02-10 13:50:30 +00:00
|
|
|
Mail::Address.new((@mail[:reply_to] || @mail[:from]).value).name
|
2021-09-16 07:56:52 +00:00
|
|
|
end
|
|
|
|
|
2021-08-24 08:48:08 +00:00
|
|
|
def original_sender
|
2022-07-02 12:10:28 +00:00
|
|
|
from_email_address(@mail[:reply_to].try(:value)) || @mail['X-Original-Sender'].try(:value) || from_email_address(from.first)
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_email_address(email)
|
|
|
|
Mail::Address.new(email).address
|
2021-08-24 08:48:08 +00:00
|
|
|
end
|
|
|
|
|
2021-12-10 14:12:26 +00:00
|
|
|
def email_forwarded_for
|
|
|
|
@mail['X-Forwarded-For'].try(:value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mail_receiver
|
|
|
|
if @mail.to.blank?
|
|
|
|
return [email_forwarded_for] if email_forwarded_for.present?
|
|
|
|
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
@mail.to
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
# forcing the encoding of the content to UTF-8 so as to be compatible with database and serializers
|
|
|
|
def encode_to_unicode(str)
|
2021-07-15 14:36:43 +00:00
|
|
|
return '' if str.blank?
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
current_encoding = str.encoding.name
|
2020-09-22 05:56:41 +00:00
|
|
|
return str if current_encoding == 'UTF-8'
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
str.encode(current_encoding, 'UTF-8', invalid: :replace, undef: :replace, replace: '?')
|
2022-01-20 23:55:35 +00:00
|
|
|
rescue StandardError
|
|
|
|
''
|
2020-07-21 06:50:46 +00:00
|
|
|
end
|
2022-01-27 23:45:26 +00:00
|
|
|
|
|
|
|
def html_mail_body?
|
2022-03-30 12:34:30 +00:00
|
|
|
((mail.content_type || '').include? 'text/html') || @mail.html_part&.content_type&.include?('text/html')
|
2022-01-27 23:45:26 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 12:34:30 +00:00
|
|
|
def text_mail_body?
|
|
|
|
((mail.content_type || '').include? 'text/plain') || @mail.text_part&.content_type&.include?('text/plain')
|
|
|
|
end
|
2022-01-27 23:45:26 +00:00
|
|
|
|
2022-03-30 12:34:30 +00:00
|
|
|
def multipart_mail_body?
|
|
|
|
((mail.content_type || '').include? 'multipart') || @mail.parts.any?
|
2022-01-27 23:45:26 +00:00
|
|
|
end
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|