Class: Baconmail
- Inherits:
-
Object
- Object
- Baconmail
- Defined in:
- lib/baconmail.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
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 |
# File 'lib/baconmail.rb', line 40 def self.daily_digest(email_address, password, target_email, configs) 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}] Bacon Mail 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
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 |
# File 'lib/baconmail.rb', line 76 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: #DC6582;'>Bacon Mail for #{(Date.today - 1)}</h1>" response += "<h3 style='color: #F59FAC; margin-left: 40px; margin-top: -10px; margin-bottom: 30px;'>for <a href='http://#{account}' style='color: #F59FAC;'>#{account}</a></h3>" response += "<ul style='width: 90%;'>" .sort_by{|x| x[0]}.each do |m| response += "<li style='margin-bottom: 10px; list-style: none; color: #DC6582; 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' style='color: #000;'>#{m[1]}</a>" else response += m[1] end response += "</li>" end response += "</ul>" return response end |
.generate_previews(new_messages, configs, email_address) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/baconmail.rb', line 108 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 # baconmail 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
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 38 |
# File 'lib/baconmail.rb', line 10 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].detect{|mail| mail.host == email_address.split("@").last}.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 = "-------------------------- BACONMAIL ----------------------------------<br/> You have received a message from : #{mailbox}<br/> We have created a new label : #{mailbox}<br/> -------------------------- BACONMAIL ----------------------------------<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 |