Class: Mail2cb::EmailHandler
- Inherits:
-
Object
- Object
- Mail2cb::EmailHandler
- Defined in:
- lib/mail2cb/email_handler.rb
Constant Summary collapse
- TWENTYFOUR_HOURS =
60 * 60 * 24
Instance Method Summary collapse
- #body ⇒ Object
- #clean_replies! ⇒ Object
- #content_type ⇒ Object
- #email ⇒ Object
- #fetch_metadata ⇒ Object
- #format_recipient(recipient) ⇒ Object
- #format_recipients(recipients) ⇒ Object
- #generate_message_hash ⇒ Object
-
#initialize(options) ⇒ EmailHandler
constructor
A new instance of EmailHandler.
- #prepare_attachments ⇒ Object
- #queue! ⇒ Object
- #sanitize! ⇒ Object
- #unique_email_code ⇒ Object
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 = raise "Require :inbound_message to be passed in" unless .has_key?(:inbound_message) raise "Require :mailbox to be passed in" unless .has_key?(:mailbox) @email = Mail.read_from_string([:inbound_message]) puts "Inbound email from #{@email.from.join(", ")}" @content = EmailContent.new(@email) end |
Instance Method Details
#body ⇒ Object
135 136 137 |
# File 'lib/mail2cb/email_handler.rb', line 135 def body @content.body end |
#clean_replies! ⇒ Object
122 123 124 |
# File 'lib/mail2cb/email_handler.rb', line 122 def clean_replies! @content.body = Mail2cb::EmailBodyCleanser.parse(@content.body, content_type) end |
#content_type ⇒ Object
114 115 116 |
# File 'lib/mail2cb/email_handler.rb', line 114 def content_type @content.html.nil? ? "text/plain" : "text/html" end |
#email ⇒ Object
26 27 28 |
# File 'lib/mail2cb/email_handler.rb', line 26 def email @email end |
#fetch_metadata ⇒ Object
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., "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_hash ⇒ Object
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 = { "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" => , "metadata" => } ["to"] = format_recipients(@email[:to].address_list.addresses) if @email[:to] ["cc"] = format_recipients(@email[:cc].address_list.addresses) if @email[:cc] .delete(:inbound_message) end |
#prepare_attachments ⇒ Object
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 |
# File 'lib/mail2cb/email_handler.rb', line 68 def = [] 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..each do || filename = .filename bucket_name = ENV["AWS_MAILROOM_BUCKET"] path = ["", "attachments", @options[:mailbox][:account_code], unique_email_code, filename].join("/") AWS::S3::S3Object.store(path, .body.decoded, bucket_name) url = AWS::S3::S3Object.url_for("#{path}", bucket_name, :expires_in => EmailHandler::TWENTYFOUR_HOURS) << { "storage_type" => "S3", "filename" => .filename, "filepath" => "/#{bucket_name}#{path}", "url" => url } end else @email..each do | | filename = .filename random_tag = Random.rand(100000000000) = File.join(["/", "tmp", "caseblocks", "attachments", @options[:mailbox][:account_code], unique_email_code]) FileUtils::mkdir_p() file_path = File.join([, "#{random_tag}.#{filename}"]) File.open(file_path, "w+b", 0644) {|f| f.write .body.decoded} << { "storage_type" => "FILE", "filename" => .filename, "filepath" => file_path } end end end |
#queue! ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/mail2cb/email_handler.rb', line 126 def queue! # puts "___________________________________________________________________________" puts "Queueing on redis #{redis_key(@options)}" ap # puts "___________________________________________________________________________" ap $redis ap $redis.lpush(redis_key(@options), .to_json) end |
#sanitize! ⇒ Object
118 119 120 |
# File 'lib/mail2cb/email_handler.rb', line 118 def sanitize! @content.body = Sanitize.fragment(@content.body, Sanitize::Config::RELAXED) end |
#unique_email_code ⇒ Object
107 108 109 110 111 112 |
# File 'lib/mail2cb/email_handler.rb', line 107 def unique_email_code md5 = Digest::MD5.new md5.update @email.raw_source md5.hexdigest end |