Class: MailDaemon::EmailHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_daemon/email_handler.rb

Instance Method Summary collapse

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(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

Instance Method Details

#bodyObject



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_typeObject



89
90
91
# File 'lib/mail_daemon/email_handler.rb', line 89

def content_type
  @html_body.nil? ? "text/plain" : "text/html"
end

#fetch_fingerprintsObject



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_metadataObject



47
48
49
50
51
# File 'lib/mail_daemon/email_handler.rb', line 47

def 
  fetch_fingerprints.merge({
    "references" => references
  })
end

#generate_message_hashObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# 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" => 
  }
  message_hash.delete(:inbound_message)
  message_hash
end

#prepare_attachmentsObject



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 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!Object



101
102
103
# File 'lib/mail_daemon/email_handler.rb', line 101

def queue!
  $redis
end

#referencesObject



53
54
55
# File 'lib/mail_daemon/email_handler.rb', line 53

def references
  @email.header["References"] ? @email.header["References"].field.element.message_ids : []
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