Class: MailDaemon::EmailHandler
- Inherits:
-
Object
- Object
- MailDaemon::EmailHandler
- Defined in:
- lib/mail_daemon/email_handler.rb
Instance Method Summary collapse
- #body ⇒ Object
- #clean_replies! ⇒ Object
- #content_type ⇒ Object
- #fetch_fingerprints ⇒ Object
- #fetch_metadata ⇒ Object
- #generate_message_hash ⇒ Object
-
#initialize(options) ⇒ EmailHandler
constructor
A new instance of EmailHandler.
- #prepare_attachments ⇒ Object
- #queue! ⇒ Object
- #references ⇒ Object
- #sanitize! ⇒ Object
Constructor Details
#initialize(options) ⇒ EmailHandler
Returns a new instance of EmailHandler.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mail_daemon/email_handler.rb', line 9 def initialize() = @email = Mail.read_from_string([:inbound_message]) puts "Handling inbound email from #{@email.from}" if [: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 |
Instance Method Details
#body ⇒ Object
105 106 107 |
# File 'lib/mail_daemon/email_handler.rb', line 105 def body @body end |
#clean_replies! ⇒ Object
97 98 99 |
# File 'lib/mail_daemon/email_handler.rb', line 97 def clean_replies! @body = MailDaemon::EmailBodyParser.parse(@body, content_type) end |
#content_type ⇒ Object
89 90 91 |
# File 'lib/mail_daemon/email_handler.rb', line 89 def content_type @html_body.nil? ? "text/plain" : "text/html" end |
#fetch_fingerprints ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 ⇒ Object
47 48 49 50 51 |
# File 'lib/mail_daemon/email_handler.rb', line 47 def fetch_fingerprints.merge({ "references" => references }) end |
#generate_message_hash ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mail_daemon/email_handler.rb', line 33 def = { "from" => @email.from, "to" => @email.to, "subject" => @email.subject, "body" => @body, "attachements" => , "metadata" => } .delete(:inbound_message) end |
#prepare_attachments ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mail_daemon/email_handler.rb', line 71 def = [] @email..each do | | filename = .filename random_tag = Random.rand(100000000000) = File.join(["/", "tmp", "caseblocks", [:mailbox][:account_code], "attachments"]) FileUtils::mkdir_p() file_path = File.join([, "#{random_tag}.#{filename}"]) File.open(file_path, "w+b", 0644) {|f| f.write .body.decoded} << { "filename" => .filename, "filepath" => file_path } end end |
#queue! ⇒ Object
101 102 103 |
# File 'lib/mail_daemon/email_handler.rb', line 101 def queue! $redis end |
#references ⇒ Object
53 54 55 |
# File 'lib/mail_daemon/email_handler.rb', line 53 def references @email.header["References"] ? @email.header["References"].field.element. : [] end |
#sanitize! ⇒ Object
93 94 95 |
# File 'lib/mail_daemon/email_handler.rb', line 93 def sanitize! @body = Sanitize.fragment(@body, Sanitize::Config::RELAXED) end |