Class: Mail2cb::EmailHandler

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

Constant Summary collapse

TWENTYFOUR_HOURS =
60 * 60 * 24

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EmailHandler

Returns a new instance of EmailHandler.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mail2cb/email_handler.rb', line 14

def initialize(options)
  @options = options

  raise "Require :inbound_message to be passed in" unless options.has_key?(:inbound_message)
  raise "Require :mailbox to be passed in" unless options.has_key?(:mailbox)

  @email = Mail.read_from_string(options[:inbound_message])

  puts "Inbound email from #{@email.from.join(", ")}"
  @content = EmailContent.new(@email)
end

Instance Method Details

#bodyObject



131
132
133
# File 'lib/mail2cb/email_handler.rb', line 131

def body
  @content.body
end

#clean_replies!Object



119
120
121
# File 'lib/mail2cb/email_handler.rb', line 119

def clean_replies!
  @content.body = Mail2cb::EmailBodyCleanser.parse(@content.body, content_type)
end

#content_typeObject



111
112
113
# File 'lib/mail2cb/email_handler.rb', line 111

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

#emailObject



26
27
28
# File 'lib/mail2cb/email_handler.rb', line 26

def email
  @email
end

#fetch_metadataObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/mail2cb/email_handler.rb', line 54

def 
  id_parser = MessageIdHandler.new(@email)
  {
    "has_bounced" => @email.bounced?,
    "in_reply_to" => @email.in_reply_to,
    "message_id" => @email.message_id,
    "account_code" => @options[:mailbox][:account_code],
    "references" => id_parser.references
  }.merge(id_parser.hash)
end

#format_recipient(recipient) ⇒ Object



50
51
52
# File 'lib/mail2cb/email_handler.rb', line 50

def format_recipient(recipient)
  {:email => recipient.address, :display_name => recipient.display_name, :account_code => @options[:mailbox][:account_code]}
end

#format_recipients(recipients) ⇒ Object



47
48
49
# File 'lib/mail2cb/email_handler.rb', line 47

def format_recipients(recipients)
  recipients.map{|recipient| format_recipient(recipient)}
end

#generate_message_hashObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mail2cb/email_handler.rb', line 30

def generate_message_hash
  message_hash = {
    "from" => format_recipient(Mail::Address.new(@email[:from].value)),
    "subject" => @email.subject,
    "sent_at" => @email.date.rfc3339,
    "received_at" => DateTime.now.rfc3339,
    "body" => @content.body,
    "attachments" => prepare_attachments,
    "metadata" => 
  }
  message_hash["to"] = format_recipients(@email[:to].address_list.addresses) if @email[:to]
  message_hash["cc"] = format_recipients(@email[:cc].address_list.addresses) if @email[:cc]

  message_hash.delete(:inbound_message)
  message_hash
end

#prepare_attachmentsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mail2cb/email_handler.rb', line 65

def prepare_attachments
  attachments = []
  if ENV["AWS_ACCESS_KEY"] && ENV["AWS_SECRET_KEY"] && ENV["AWS_MAILROOM_BUCKET"]
    AWS::S3::Base.establish_connection!(
      :access_key_id     => ENV["AWS_ACCESS_KEY"],
      :secret_access_key => ENV["AWS_SECRET_KEY"]
    )
    @email.attachments.each do |attachment|
      filename = attachment.filename
      bucket_name = ENV["AWS_MAILROOM_BUCKET"]
      path = ["", "attachments", @options[:mailbox][:account_code], unique_email_code, filename].join("/")
      AWS::S3::S3Object.store(path, attachment.body.decoded, bucket_name)
      url = AWS::S3::S3Object.url_for("#{path}", bucket_name, :expires_in => EmailHandler::TWENTYFOUR_HOURS)

      attachments << {
        "storage_type" => "S3",
        "filename" => attachment.filename,
        "filepath" => "/#{bucket_name}#{path}",
        "url" => url
      }
    end
  else
    @email.attachments.each do | attachment |
      filename = attachment.filename
      random_tag = Random.rand(100000000000)
      attachment_folder = File.join(["/", "tmp", "caseblocks", "attachments", @options[:mailbox][:account_code], unique_email_code])
      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 << {
        "storage_type" => "FILE",
        "filename" => attachment.filename,
        "filepath" => file_path
      }
    end
  end
  attachments
end

#queue!Object



123
124
125
126
127
128
129
# File 'lib/mail2cb/email_handler.rb', line 123

def queue!
  # puts "___________________________________________________________________________"
  puts "Queueing on redis #{redis_key(@options)}"
  ap generate_message_hash
  # puts "___________________________________________________________________________"
  $redis.lpush(redis_key(@options), generate_message_hash.to_json)
end

#sanitize!Object



115
116
117
# File 'lib/mail2cb/email_handler.rb', line 115

def sanitize!
  @content.body = Sanitize.fragment(@content.body, Sanitize::Config::RELAXED)
end

#unique_email_codeObject



104
105
106
107
108
109
# File 'lib/mail2cb/email_handler.rb', line 104

def unique_email_code
  md5 = Digest::MD5.new
  md5.update @email.raw_source

  md5.hexdigest
end