2021-01-20 13:45:05 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-12-31 11:56:13 +01:00
|
|
|
class EmailImporter
|
2021-01-04 22:03:24 +01:00
|
|
|
attr_accessor :email_class
|
2021-01-04 22:05:42 +01:00
|
|
|
attr_accessor :html_to_text_class
|
2020-12-31 11:56:13 +01:00
|
|
|
|
2021-01-04 22:03:24 +01:00
|
|
|
def initialize(
|
|
|
|
email_class: Email,
|
2021-01-04 22:05:42 +01:00
|
|
|
html_to_text_class: Rails.configuration.html_to_text_class)
|
2021-01-04 22:03:24 +01:00
|
|
|
|
|
|
|
@email_class = email_class
|
2021-01-04 22:05:42 +01:00
|
|
|
@html_to_text_class = html_to_text_class
|
2020-12-31 11:56:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def import(mail)
|
2021-01-04 22:03:24 +01:00
|
|
|
email = email_class.new(
|
2020-12-31 15:32:07 +01:00
|
|
|
message_id: mail.message_id,
|
|
|
|
subject: mail.subject,
|
|
|
|
date: mail.date,
|
|
|
|
to: mail.to,
|
2020-12-31 11:56:13 +01:00
|
|
|
delivered_to: delivered_to(mail),
|
2020-12-31 15:32:07 +01:00
|
|
|
from: mail.from,
|
|
|
|
plain_body: text_plain_body(mail),
|
2021-01-20 13:46:45 +01:00
|
|
|
headers: hashed_headers(mail)
|
2020-12-31 11:56:13 +01:00
|
|
|
)
|
|
|
|
rescue => ex
|
|
|
|
binding.pry
|
|
|
|
end
|
|
|
|
|
|
|
|
def delivered_to(mail)
|
2021-01-04 22:05:42 +01:00
|
|
|
values_from_header(header: mail.header["Delivered-To"], default: Array(mail.to))
|
2020-12-31 11:56:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def text_plain_body(mail)
|
|
|
|
if mail.parts.present?
|
|
|
|
if mail.text_part.present?
|
|
|
|
mail.text_part.decoded
|
|
|
|
elsif mail.html_part.present?
|
2021-01-04 22:05:42 +01:00
|
|
|
html_to_text_class.new.convert(mail.html_part.decoded)
|
2020-12-31 11:56:13 +01:00
|
|
|
else
|
|
|
|
mail.parts[0].decoded
|
|
|
|
end
|
2021-01-04 22:05:42 +01:00
|
|
|
elsif mail.content_type && mail.content_type.match?(/\btext\/html\b/)
|
2021-01-04 23:06:28 +01:00
|
|
|
plain_text = html_to_text_class.new.convert(mail.decoded)
|
|
|
|
if mail.content_type.match?(/\butf-8\b/)
|
|
|
|
# force utf-8 charset, even if we have to remve invalid characters
|
|
|
|
plain_text.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
|
|
|
|
else
|
|
|
|
plain_text
|
|
|
|
end
|
2020-12-31 11:56:13 +01:00
|
|
|
else
|
2021-01-01 22:33:47 +01:00
|
|
|
mail.decoded
|
2020-12-31 11:56:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def hashed_headers(mail)
|
|
|
|
mail.header.map { |header|
|
|
|
|
{
|
2021-01-23 18:08:58 +01:00
|
|
|
"name" => header.name,
|
|
|
|
"value" => header.value
|
2020-12-31 11:56:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-01-04 22:03:24 +01:00
|
|
|
def values_from_header(header:, default: [])
|
2021-01-01 19:12:50 +01:00
|
|
|
if header.respond_to?(:map)
|
|
|
|
header.map(&:value)
|
|
|
|
elsif header.respond_to?(:value)
|
2021-01-04 22:03:24 +01:00
|
|
|
Array(header.value)
|
|
|
|
elsif block_given?
|
|
|
|
yield(header)
|
|
|
|
else
|
|
|
|
default
|
2021-01-01 19:12:50 +01:00
|
|
|
end
|
2020-12-31 11:56:13 +01:00
|
|
|
end
|
|
|
|
end
|