-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmail-old.rb
45 lines (34 loc) · 1.28 KB
/
gmail-old.rb
1
2
3
4
5
6
7
8
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
38
39
40
41
42
43
44
45
# Playing around with Gmail gem
# https://github.com/nu7hatch/gmail
require 'gmail'
require 'date'
require 'active_support'
require 'active_support/core_ext'
require './gmail_config'
gmail = Gmail.connect(Gmail_Config::KOKO[:username], Gmail_Config::KOKO[:password])
inbox = gmail.inbox
expenses = gmail.label("Expenses")
last_month = Date.today.month - 1
prev_month_expenses = expenses.find(:before => Date.today.beginning_of_month, :after => Date.today.beginning_of_month.last_month)
puts "Total number of emails in inbox: #{inbox.count}"
puts "Total number of unread emails: #{inbox.count(:unread)}"
puts "Total number of 'Expenses' emails last month: #{prev_month_expenses.count}"
prev_month_expenses.each {|email| puts "#{email.date} - #{email.subject}"}
#puts "All labels: #{gmail.labels.all}"
#puts "Total number of 'Expenses' emails: #{expenses.count}"
i = 0
prev_month_expenses.each do |email|
new_email = gmail.compose do
to "#{Gmail_Config::KOKO[:recipient]}"
subject "Josh Expenses: #{email.subject}"
content_type 'text/html; charset=UTF-8'
body "#{email.body}"
email.attachments.each do |attachment|
add_file email.attachment
end
end
gmail.deliver(new_email)
i += 1
puts "Sent email number #{i} of #{prev_month_expenses.count}"
end
gmail.logout