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



137
138
139
# File 'lib/mail2cb/email_handler.rb', line 137

def body
  @content.body
end

#clean_replies!Object



124
125
126
# File 'lib/mail2cb/email_handler.rb', line 124

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

#content_typeObject



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

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
64
65
66
# 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],
    "mailbox_id" => @options[:mailbox][:mailbox_id],
    "mailbox_name" => @options[:mailbox][:mailbox_name],
    "mailbox_default" => @options[:mailbox][:mailbox_default],
    "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



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
103
104
105
106
107
# File 'lib/mail2cb/email_handler.rb', line 68

def prepare_attachments
  attachments = []
  if ENV["AWS_ACCESS_KEY_ID"] && ENV["AWS_SECRET_ACCESS_KEY"] && ENV["AWS_REGION"] && ENV["AWS_MAILROOM_BUCKET"]
    @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("/")[1..-1]
      # AWS::S3::S3Object.store(path, attachment.body.decoded, bucket_name)
      # url = AWS::S3::S3Object.url_for("#{path}", bucket_name, :expires_in => EmailHandler::TWENTYFOUR_HOURS)


      s3 = Aws::S3::Resource.new(region: 'us-east-1')
      obj = s3.bucket(bucket_name).object(path)
      obj.put(body: attachment.body.decoded, acl: 'private')
      url = obj.presigned_url(:get, 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



128
129
130
131
132
133
134
135
# File 'lib/mail2cb/email_handler.rb', line 128

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

#sanitize!Object



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

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

#unique_email_codeObject



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

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

  md5.hexdigest
end