diff --git a/app/mailboxes/in_mailbox.rb b/app/mailboxes/in_mailbox.rb index 8c4aaad..2d268cf 100644 --- a/app/mailboxes/in_mailbox.rb +++ b/app/mailboxes/in_mailbox.rb @@ -1,24 +1,33 @@ class InMailbox < ApplicationMailbox 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 + rescue => ex + binding.pry + end - # email = Email.new( - # message_id: mail.message_id, - # subject: mail.subject, - # date: mail.date, - # to: mail.to, - # from: mail.from, - # 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 - + def delivered_to(mail) + if mail.header["Delivered-To"].present? + mail.header["Delivered-To"].value + else + mail.to + end end def text_plain_body(mail) @@ -31,6 +40,15 @@ class InMailbox < ApplicationMailbox end end + def hashed_headers(mail) + mail.header.map { |h| + { + name: h.name, + value: h.value + } + } + end + def sent_by_cron?(mail) mail.subject.match?(/cron/i) \ || mail.header["X-Cron-Env"].present? @@ -40,26 +58,22 @@ class InMailbox < ApplicationMailbox end def clients(mail) - if mail.header["X-Client-ID"].present? - mail.header["X-Client-ID"].value - end + Array(mail.header["X-Client-ID"].value) if mail.header["X-Client-ID"].present? end def servers(mail) if mail.header["X-Server-Name"].present? - mail.header["X-Server-Name"].value + Array(mail.header["X-Server-Name"].value) else 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 def tickets(mail) - if mail.header["X-Ticket-ID"].present? - mail.header["X-Ticket-ID"].value - end + Array(mail.header["X-Ticket-ID"].value) if mail.header["X-Ticket-ID"].present? end def address_match_evolix_net?(address)