class InMailbox < ApplicationMailbox def process binding.pry # 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 end def text_plain_body(mail) if mail.parts.present? mail.parts.detect(-> { mail.parts[0] }) { |part| part.content_type =~ /text\/plain/ }.body.decoded else mail.decoded end end def sent_by_cron?(mail) mail.subject.match?(/cron/i) \ || mail.header["X-Cron-Env"].present? end def mailing_list?(mail) mail.header["List-Unsubscribe"].present? end def clients(mail) if mail.header["X-Client-ID"].present? mail.header["X-Client-ID"].value end end def servers(mail) if mail.header["X-Server-Name"].present? 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) } 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 end def address_match_evolix_net?(address) address.match?(/@(.+)\.evolix\.net/i) end def extract_server_name_from_address(address) address.match(/@(.+)\.evolix\.net/i)[1] end end