class Mail2cb::EmailHandler

Constants

TWENTYFOUR_HOURS

Public Class Methods

new(options) click to toggle source
# 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

Public Instance Methods

body() click to toggle source
# File lib/mail2cb/email_handler.rb, line 137
def body
  @content.body
end
clean_replies!() click to toggle source
# File lib/mail2cb/email_handler.rb, line 124
def clean_replies!
  @content.body = Mail2cb::EmailBodyCleanser.parse(@content.body, content_type)
end
content_type() click to toggle source
# File lib/mail2cb/email_handler.rb, line 116
def content_type
  @content.html.nil? ? "text/plain" : "text/html"
end
email() click to toggle source
# File lib/mail2cb/email_handler.rb, line 26
def email
  @email
end
fetch_metadata() click to toggle source
# File lib/mail2cb/email_handler.rb, line 54
def fetch_metadata
  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) click to toggle source
# 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) click to toggle source
# File lib/mail2cb/email_handler.rb, line 47
def format_recipients(recipients)
  recipients.map{|recipient| format_recipient(recipient)}
end
generate_message_hash() click to toggle source
# 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" => fetch_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_attachments() click to toggle source
# 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!() click to toggle source
# 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!() click to toggle source
# File lib/mail2cb/email_handler.rb, line 120
def sanitize!
  @content.body = Sanitize.fragment(@content.body, Sanitize::Config::RELAXED)
end
unique_email_code() click to toggle source
# File lib/mail2cb/email_handler.rb, line 109
def unique_email_code
  md5 = Digest::MD5.new
  md5.update @email.raw_source

  md5.hexdigest
end

Private Instance Methods

redis_key(options) click to toggle source
# File lib/mail2cb/email_handler.rb, line 142
def redis_key(options)
  "cb:comms:#{options[:mailbox][:account_code]}:inbox"
end