2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-10 15:00:27 -06:00
|
|
|
class IncomingEmailDetailsSerializer < ApplicationSerializer
|
2016-03-07 09:56:17 -06:00
|
|
|
attributes :error, :error_description, :rejection_message, :headers, :subject, :body
|
2016-02-10 15:00:27 -06:00
|
|
|
|
|
|
|
def initialize(incoming_email, opts)
|
|
|
|
super
|
|
|
|
@error_string = incoming_email.error
|
|
|
|
@mail = Mail.new(incoming_email.raw)
|
|
|
|
end
|
|
|
|
|
2020-04-30 01:48:34 -05:00
|
|
|
EMAIL_RECEIVER_ERROR_PREFIX = "Email::Receiver::"
|
2016-02-10 15:00:27 -06:00
|
|
|
|
|
|
|
def error
|
2017-08-04 13:04:26 -05:00
|
|
|
@error_string.presence || I18n.t("emails.incoming.unrecognized_error")
|
2016-02-10 15:00:27 -06: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 11:59:56 -05:00
|
|
|
@error_string && @error_string[EMAIL_RECEIVER_ERROR_PREFIX]
|
2016-02-10 15:00:27 -06:00
|
|
|
end
|
|
|
|
|
2016-03-30 12:05:42 -05:00
|
|
|
def headers
|
|
|
|
@mail.header.to_s
|
2016-02-10 15:00:27 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
2018-01-30 16:47:58 -06:00
|
|
|
@mail.subject.presence || I18n.t("emails.incoming.no_subject")
|
2016-02-10 15:00:27 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
body =
|
2023-01-09 06:20:10 -06:00
|
|
|
begin
|
2016-02-10 15:00:27 -06:00
|
|
|
@mail.text_part.decoded
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2016-02-10 15:00:27 -06:00
|
|
|
body ||=
|
2023-01-09 06:20:10 -06:00
|
|
|
begin
|
2016-02-10 15:00:27 -06:00
|
|
|
@mail.html_part.decoded
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2016-02-10 15:00:27 -06:00
|
|
|
body ||=
|
2023-01-09 06:20:10 -06:00
|
|
|
begin
|
2016-02-10 15:00:27 -06:00
|
|
|
@mail.body.decoded
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2018-01-30 16:47:58 -06: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 15:00:27 -06:00
|
|
|
end
|
|
|
|
end
|