class MailDaemon::EmailHandler
Public Class Methods
new(options)
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 9 def initialize(options) @options = options @email = Mail.read_from_string(options[:inbound_message]) puts "Handling inbound email from #{@email.from}" if @options[:debug] if @email.multipart? @email.parts.each do |part| content_type = part.content_type.split(";")[0] if content_type[0..3] == "text" if content_type == "text/html" @html_body = part.body.decoded.force_encoding("ASCII-8BIT").encode('UTF-8', undef: :replace, replace: '') elsif content_type == "text/plain" @text_body = part.body.decoded.force_encoding("ASCII-8BIT").encode('UTF-8', undef: :replace, replace: '') end end end else @text_body = @email.body.decoded.force_encoding("ASCII-8BIT").encode('UTF-8', undef: :replace, replace: '') end @body = @html_body || @text_body end
Public Instance Methods
body()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 105 def body @body end
clean_replies!()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 97 def clean_replies! @body = MailDaemon::EmailBodyParser.parse(@body, content_type) end
content_type()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 89 def content_type @html_body.nil? ? "text/plain" : "text/html" end
fetch_fingerprints()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 57 def fetch_fingerprints previous_id = references.last match = /(.*?)\.(.*?)@emergeadapt.com/.match(previous_id) if match if match[2] == "case" {"case_id" => match[1]} else {"message_id" => match[1]} end else {} end end
fetch_metadata()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 47 def fetch_metadata fetch_fingerprints.merge({ "references" => references }) end
generate_message_hash()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 33 def generate_message_hash message_hash = { "from" => @email.from, "to" => @email.to, "subject" => @email.subject, "body" => @body, "attachements" => prepare_attachments, "metadata" => fetch_metadata } message_hash.delete(:inbound_message) message_hash end
prepare_attachments()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 71 def prepare_attachments attachments = [] @email.attachments.each do | attachment | filename = attachment.filename random_tag = Random.rand(100000000000) attachment_folder = File.join(["/", "tmp", "caseblocks", @options[:mailbox][:account_code], "attachments"]) FileUtils::mkdir_p(attachment_folder) file_path = File.join([attachment_folder, "#{random_tag}.#{filename}"]) File.open(file_path, "w+b", 0644) {|f| f.write attachment.body.decoded} attachments << { "filename" => attachment.filename, "filepath" => file_path } end attachments end
queue!()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 101 def queue! $redis end
references()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 53 def references @email.header["References"] ? @email.header["References"].field.element.message_ids : [] end
sanitize!()
click to toggle source
# File lib/mail_daemon/email_handler.rb, line 93 def sanitize! @body = Sanitize.fragment(@body, Sanitize::Config::RELAXED) end