-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.rb
More file actions
86 lines (68 loc) · 1.52 KB
/
Copy pathemail.rb
File metadata and controls
86 lines (68 loc) · 1.52 KB
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
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
class Email
include Gmail
FILENAME = 'email_config.yaml'
def initialize(user=nil, password=nil)
if user.nil? and password.nil?
data = YAML::load(File.open(FILENAME))
user=data['email']
password=data['password']
@folder=data['folder']
end
begin
@gmail = Gmail.connect!(user,password)
rescue Net::IMAP::BadResponseError
puts "CANNOT LOGIN TO GMAIL WITH GIVEN CREDENTIALS"
end
end
def connected?
@gmail.logged_in?
end
def logout
@gmail.logout
end
def last_inbox_email
@gmail.inbox.mails.last
end
def last_inbox_unread_email
@gmail.inbox.find(:unread).last
end
def last_email_from_label(label_name)
@gmail.label(label_name).emails.last
end
def last_email_from(from)
@gmail.inbox.find(:from => from).last
end
def mark_as_unread mail
mail.unread!
end
def mark_as_read mail
mail.read!
end
def mark_as_starred mail
mail.star!
end
def mark_as_unstarred mail
mail.unstar!
end
def download_attachment mail
if !mail.message.attachments.empty?
mail.message.save_attachments_to(@folder)
end
end
def delete_all_emails_with_label(label)
@gmail.emails(:label => label).each do |email|
email.delete!
end
end
def emails_with_subject(subject)
@gmail.emails(:subject => label)
end
def send_email(email_to,email_subject,email_body)
email = @gmail.compose do
to email_to
subject email_subject
body email_body
end
email.deliver!
end
end