Mapping sur un Email depuis ActiveMailbox

This commit is contained in:
Jérémy Lecour 2020-12-27 11:45:53 +01:00
parent 62224f5727
commit e8ec3c8a66

View file

@ -1,24 +1,33 @@
class InMailbox < ApplicationMailbox class InMailbox < ApplicationMailbox
def process def process
email = Email.new(
message_id: mail.message_id,
subject: mail.subject,
date: mail.date,
to: mail.to,
delivered_to: delivered_to(mail),
from: mail.from,
plain_body: text_plain_body(mail),
headers: hashed_headers(mail),
cron: sent_by_cron?(mail),
mailing_list: mailing_list?(mail),
clients: clients(mail),
servers: servers(mail),
tickets: tickets(mail)
)
binding.pry binding.pry
rescue => ex
binding.pry
end
# email = Email.new( def delivered_to(mail)
# message_id: mail.message_id, if mail.header["Delivered-To"].present?
# subject: mail.subject, mail.header["Delivered-To"].value
# date: mail.date, else
# to: mail.to, mail.to
# from: mail.from, end
# plain_body: text_plain_body(mail),
# raw_headers: mail.header.raw_source,
# cron: sent_by_cron?(mail),
# mailing_list: mailing_list?(mail),
# clients: clients(mail),
# servers: servers(mail),
# tickets: tickets(mail)
# )
#
# binding.pry
end end
def text_plain_body(mail) def text_plain_body(mail)
@ -31,6 +40,15 @@ class InMailbox < ApplicationMailbox
end end
end end
def hashed_headers(mail)
mail.header.map { |h|
{
name: h.name,
value: h.value
}
}
end
def sent_by_cron?(mail) def sent_by_cron?(mail)
mail.subject.match?(/cron/i) \ mail.subject.match?(/cron/i) \
|| mail.header["X-Cron-Env"].present? || mail.header["X-Cron-Env"].present?
@ -40,26 +58,22 @@ class InMailbox < ApplicationMailbox
end end
def clients(mail) def clients(mail)
if mail.header["X-Client-ID"].present? Array(mail.header["X-Client-ID"].value) if mail.header["X-Client-ID"].present?
mail.header["X-Client-ID"].value
end
end end
def servers(mail) def servers(mail)
if mail.header["X-Server-Name"].present? if mail.header["X-Server-Name"].present?
mail.header["X-Server-Name"].value Array(mail.header["X-Server-Name"].value)
else else
matching_header = ["To", "Delivered-To", "From", "Subject"].detect { |header_name| matching_header = ["To", "Delivered-To", "From", "Subject"].detect { |header_name|
address_match_evolix_net?(mail.header[header_name].value) address_match_evolix_net?(mail.header[header_name].value) if mail.header[header_name].present?
} }
extract_server_name_from_address(mail.header[matching_header].value) if matching_header Array(extract_server_name_from_address(mail.header[matching_header].value)) if matching_header
end end
end end
def tickets(mail) def tickets(mail)
if mail.header["X-Ticket-ID"].present? Array(mail.header["X-Ticket-ID"].value) if mail.header["X-Ticket-ID"].present?
mail.header["X-Ticket-ID"].value
end
end end
def address_match_evolix_net?(address) def address_match_evolix_net?(address)