Class: Anotherinbox
- Inherits:
-
Object
- Object
- Anotherinbox
- Defined in:
- lib/anotherinbox.rb
Class Method Summary collapse
- .daily_digest(email_address, password, target_email, configs) ⇒ Object
- .email_template(new_messages, account, configs, email_address) ⇒ Object
- .generate_previews(new_messages, configs, email_address) ⇒ Object
- .process_inbox!(email_address, password, target_email) ⇒ Object
Class Method Details
.daily_digest(email_address, password, target_email, configs) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/anotherinbox.rb', line 39 def self.daily_digest(email_address, password, target_email, configs) def self.email_template(, account, configs, email_address) # we all know, inline styles sucks. sadly, it's the # only way to get them into gmail. response = "" response += "<h1 style='margin-left: 40px; color: #000000;'>Daily Digest for #{(Date.today - 1)}</h1>" response += "<h3 style='color: #aaaaaa; margin-left: 40px; margin-top: -10px; margin-bottom: 30px;'>for #{account}</h3>" response += "<ul style='width: 90%;'>" .sort_by{|x| x[0]}.each do |m| response += "<li style='margin-bottom: 10px; list-style: none; color: #3485ae; border-bottom: 1px dotted #ccc; padding-bottom: 10px; font-weight: bold;'>" response += m[0] response += ": <strong style='color: #000000; font-weight: normal;'>" if configs["use_preview"] response += "<a href='http://" response += configs["bucket"] response += ".s3.amazonaws.com/" response += email_address.gsub("@", "_").gsub(".", "_") response += "_" response += Digest::SHA1.hexdigest(m[1]) response += ".html'>#{m[1]}</a>" else response += m[1] end response += "</strong> </li>" end response += "</ul>" return response end def self.generate_previews(, configs, email_address) AWS::S3::Base.establish_connection!( :access_key_id => configs["aws_key"], :secret_access_key => configs["aws_secret"] ) # set file prefix. useful when we have more than one # anotherinbox account running in the same script instance prefix = email_address.gsub("@", "_").gsub(".", "_") #binding.pry # cleanup the bucket while (AWS::S3::Bucket.objects(configs["bucket"], :prefix => prefix).size > 0) do AWS::S3::Bucket.objects(configs["bucket"]).each do |file| file.delete if file.key.match(prefix) end end .sort_by{|x| x[0]}.each do |m| filename = Digest::SHA1.hexdigest(m[1]) AWS::S3::S3Object.store("#{prefix}_#{filename}.html", m[2].to_s, configs["bucket"], :access => :public_read) end end log = Logger.new(STDOUT) emails = [] aib_domain = email_address.split("@")[1] log.info("Account: #{email_address}") Gmail.new(email_address, password) do |gmail| gmail.mailbox('[Gmail]/All Mail').emails(:on => (Date.today - 1)).each do |email| next if email.subject.match("Daily Digest for") rescue nil next if "#{email.from[0]['mailbox']}@#{email.from[0]['host']}" == email_address mailbox = email[:to][0].mailbox.downcase rescue "unknown" body = email.html_part.nil? ? email.body : email.html_part.body emails << [mailbox, email.subject, body] end if emails.size > 0 if configs["use_preview"] log.info("Generating previews...") self.generate_previews(emails, configs, email_address) end log.info("Sending daily digest with #{emails.size} entries..") digest = gmail. digest.to = target_email digest.subject = "[#{aib_domain}] Daily Digest for #{(Date.today - 1)}" digest.content_type = "text/html" digest.body = self.email_template(emails, aib_domain, configs, email_address) digest.deliver! end end log.info("Process finished") end |
.email_template(new_messages, account, configs, email_address) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/anotherinbox.rb', line 41 def self.email_template(, account, configs, email_address) # we all know, inline styles sucks. sadly, it's the # only way to get them into gmail. response = "" response += "<h1 style='margin-left: 40px; color: #000000;'>Daily Digest for #{(Date.today - 1)}</h1>" response += "<h3 style='color: #aaaaaa; margin-left: 40px; margin-top: -10px; margin-bottom: 30px;'>for #{account}</h3>" response += "<ul style='width: 90%;'>" .sort_by{|x| x[0]}.each do |m| response += "<li style='margin-bottom: 10px; list-style: none; color: #3485ae; border-bottom: 1px dotted #ccc; padding-bottom: 10px; font-weight: bold;'>" response += m[0] response += ": <strong style='color: #000000; font-weight: normal;'>" if configs["use_preview"] response += "<a href='http://" response += configs["bucket"] response += ".s3.amazonaws.com/" response += email_address.gsub("@", "_").gsub(".", "_") response += "_" response += Digest::SHA1.hexdigest(m[1]) response += ".html'>#{m[1]}</a>" else response += m[1] end response += "</strong> </li>" end response += "</ul>" return response end |
.generate_previews(new_messages, configs, email_address) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/anotherinbox.rb', line 73 def self.generate_previews(, configs, email_address) AWS::S3::Base.establish_connection!( :access_key_id => configs["aws_key"], :secret_access_key => configs["aws_secret"] ) # set file prefix. useful when we have more than one # anotherinbox account running in the same script instance prefix = email_address.gsub("@", "_").gsub(".", "_") #binding.pry # cleanup the bucket while (AWS::S3::Bucket.objects(configs["bucket"], :prefix => prefix).size > 0) do AWS::S3::Bucket.objects(configs["bucket"]).each do |file| file.delete if file.key.match(prefix) end end .sort_by{|x| x[0]}.each do |m| filename = Digest::SHA1.hexdigest(m[1]) AWS::S3::S3Object.store("#{prefix}_#{filename}.html", m[2].to_s, configs["bucket"], :access => :public_read) end end |
.process_inbox!(email_address, password, target_email) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/anotherinbox.rb', line 9 def self.process_inbox!(email_address, password, target_email) log = Logger.new(STDOUT) log.info("Account: #{email_address}") Gmail.new(email_address, password) do |gmail| gmail.inbox.emails(:unread).each do |email| mailbox = email[:to][0].mailbox.downcase rescue "unknown" gmail.labels.new(mailbox) if gmail.mailbox(mailbox).count == 0 log.info("First email for [#{gmail.mailbox(mailbox)}], forwarding..") fwd = gmail. fwd.to = target_email fwd.subject = "New Sender: #{email.subject}" fwd.content_type = "text/html" body = email.parts.last.body.to_s rescue nil body ||= email.body.to_s fwd.body = "-------------------------- ANOTHERINBOX ----------------------------------<br/> You have received a message from : #{mailbox}<br/> We have created a new label : #{mailbox}<br/> -------------------------- ANOTHERINBOX ----------------------------------<br/><br/>" + body fwd.deliver! end email.label(mailbox) email.unread! email.archive! log.info("\tProcessing #{email[:subject]}") end end log.info("Process finished") end |