2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-10 16:00:27 -05:00
|
|
|
class IncomingEmailDetailsSerializer < ApplicationSerializer
|
2016-03-07 10:56:17 -05:00
|
|
|
attributes :error, :error_description, :rejection_message, :headers, :subject, :body
|
2016-02-10 16:00:27 -05:00
|
|
|
|
|
|
|
def initialize(incoming_email, opts)
|
|
|
|
super
|
|
|
|
@error_string = incoming_email.error
|
|
|
|
@mail = Mail.new(incoming_email.raw)
|
|
|
|
end
|
|
|
|
|
2020-04-30 02:48:34 -04:00
|
|
|
EMAIL_RECEIVER_ERROR_PREFIX = "Email::Receiver::"
|
2016-02-10 16:00:27 -05:00
|
|
|
|
|
|
|
def error
|
2017-08-04 14:04:26 -04:00
|
|
|
@error_string.presence || I18n.t("emails.incoming.unrecognized_error")
|
2016-02-10 16:00:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_description
|
|
|
|
error_name = @error_string.sub(EMAIL_RECEIVER_ERROR_PREFIX, "").underscore
|
|
|
|
I18n.t("emails.incoming.errors.#{error_name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_error_description?
|
2019-03-26 12:59:56 -04:00
|
|
|
@error_string && @error_string[EMAIL_RECEIVER_ERROR_PREFIX]
|
2016-02-10 16:00:27 -05:00
|
|
|
end
|
|
|
|
|
2016-03-30 13:05:42 -04:00
|
|
|
def headers
|
|
|
|
@mail.header.to_s
|
2016-02-10 16:00:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
2018-01-30 17:47:58 -05:00
|
|
|
@mail.subject.presence || I18n.t("emails.incoming.no_subject")
|
2016-02-10 16:00:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
body =
|
2023-01-09 07:20:10 -05:00
|
|
|
begin
|
2016-02-10 16:00:27 -05:00
|
|
|
@mail.text_part.decoded
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2016-02-10 16:00:27 -05:00
|
|
|
body ||=
|
2023-01-09 07:20:10 -05:00
|
|
|
begin
|
2016-02-10 16:00:27 -05:00
|
|
|
@mail.html_part.decoded
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2016-02-10 16:00:27 -05:00
|
|
|
body ||=
|
2023-01-09 07:20:10 -05:00
|
|
|
begin
|
2016-02-10 16:00:27 -05:00
|
|
|
@mail.body.decoded
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2018-01-30 17:47:58 -05:00
|
|
|
|
|
|
|
return I18n.t("emails.incoming.no_body") if body.blank?
|
|
|
|
|
|
|
|
body
|
|
|
|
.encode("utf-8", invalid: :replace, undef: :replace, replace: "")
|
|
|
|
.strip
|
|
|
|
.truncate_words(100, escape: false)
|
2016-02-10 16:00:27 -05:00
|
|
|
end
|
|
|
|
end
|