gmail 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +22 -0
- data/CHANGELOG.md +60 -0
- data/LICENSE +21 -0
- data/README.md +247 -0
- data/Rakefile +52 -0
- data/TODO.md +5 -0
- data/VERSION +1 -0
- data/lib/gmail.rb +47 -0
- data/lib/gmail/client.rb +221 -0
- data/lib/gmail/labels.rb +42 -0
- data/lib/gmail/mailbox.rb +92 -0
- data/lib/gmail/message.rb +137 -0
- data/lib/gmail/version.rb +12 -0
- data/spec/client_spec.rb +173 -0
- data/spec/gmail_spec.rb +38 -0
- data/spec/mailbox_spec.rb +48 -0
- data/spec/message_spec.rb +51 -0
- data/spec/spec_helper.rb +13 -0
- metadata +150 -0
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Gmail gem changelog
|
2
|
+
|
3
|
+
## 0.3.0 / Unreleased
|
4
|
+
|
5
|
+
* Refactoring
|
6
|
+
* Fixed bugs
|
7
|
+
* API improvements
|
8
|
+
* Better documentation
|
9
|
+
* Code cleanup
|
10
|
+
* RSpec for everything
|
11
|
+
|
12
|
+
## 0.1.1 / 2010-05-11
|
13
|
+
|
14
|
+
* Added explicit tmail dependency in gemspec
|
15
|
+
* Added better README tutorial content
|
16
|
+
|
17
|
+
## 0.0.9 / 2010-04-17
|
18
|
+
|
19
|
+
* Fixed content-transfer-encoding when sending email
|
20
|
+
|
21
|
+
## 0.0.8 / 2009-12-23
|
22
|
+
|
23
|
+
* Fixed attaching a file to an empty message
|
24
|
+
|
25
|
+
## 0.0.7 / 2009-12-23
|
26
|
+
|
27
|
+
* Improved multipart message parsing reliability
|
28
|
+
|
29
|
+
## 0.0.6 / 2009-12-21
|
30
|
+
|
31
|
+
* Fixed multipart parsing for when the boundary is marked in quotes.
|
32
|
+
|
33
|
+
## 0.0.5 / 2009-12-16
|
34
|
+
|
35
|
+
* Fixed IMAP initializer to work with Ruby 1.9's net/imap
|
36
|
+
* Better logout depending on the IMAP connection itself
|
37
|
+
* Added MIME::Message#text and MIME::Message#html for easier access to an email body
|
38
|
+
* Improved the MIME-parsing API slightly
|
39
|
+
* Added some tests
|
40
|
+
|
41
|
+
## 0.0.4 / 2009-11-30
|
42
|
+
|
43
|
+
* Added label creation (thanks to Justin Perkins / http://github.com/justinperkins)
|
44
|
+
* Made the gem login automatically when first needed
|
45
|
+
* Added an optional block on the Gmail.new object that will login and logout for you
|
46
|
+
* Added several search options (thanks to Mikkel Malmberg / http://github.com/mikker)
|
47
|
+
|
48
|
+
## 0.0.3 / 2009-11-19
|
49
|
+
|
50
|
+
* Fixed MIME::Message#content= for messages without an encoding
|
51
|
+
* Added Gmail#new_message
|
52
|
+
|
53
|
+
## 0.0.2 / 2009-11-18
|
54
|
+
|
55
|
+
* Made all of the examples in the README possible
|
56
|
+
|
57
|
+
## 0.0.1 / 2009-11-18
|
58
|
+
|
59
|
+
* Birthday!
|
60
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyrignt (c) 2010 Kriss 'nu7hatch' Kowalik
|
2
|
+
Copyright (c) 2009-2010 BehindLogic
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,247 @@
|
|
1
|
+
# GMail for Ruby
|
2
|
+
|
3
|
+
A Rubyesque interface to Google's GMail, with all the tools you'll need. Search,
|
4
|
+
read and send multipart emails, archive, mark as read/unread, delete emails,
|
5
|
+
and manage labels.
|
6
|
+
|
7
|
+
It's based on Daniel Parker's ruby-gmail gem. This version has more friendy
|
8
|
+
API, is well tested, better documented and have many other improvements.
|
9
|
+
|
10
|
+
## Author(s)
|
11
|
+
|
12
|
+
* Kriss 'nu7hatch' Kowalik
|
13
|
+
* [Daniel Parker of BehindLogic.com](http://github.com/dcparker)
|
14
|
+
|
15
|
+
Extra thanks for specific feature contributions from:
|
16
|
+
|
17
|
+
* [Justin Perkins](http://github.com/justinperkins)
|
18
|
+
* [Mikkel Malmberg](http://github.com/mikker)
|
19
|
+
* [Julien Blanchard](http://github.com/julienXX)
|
20
|
+
* [Federico Galassi](http://github.com/fgalassi)
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
You can install it easy using rubygems:
|
25
|
+
|
26
|
+
sudo gem install gmail
|
27
|
+
|
28
|
+
Or install it manualy:
|
29
|
+
|
30
|
+
git clone git://github.com/nu7hatch/gmail.git
|
31
|
+
cd gmail
|
32
|
+
rake install
|
33
|
+
|
34
|
+
To install gmail gem you have to met following requirements (with rubygems all
|
35
|
+
will be installed automatically):
|
36
|
+
|
37
|
+
* mail
|
38
|
+
* mime
|
39
|
+
* smpt_tls (Ruby < 1.8.7)
|
40
|
+
|
41
|
+
## Features
|
42
|
+
|
43
|
+
* Search emails
|
44
|
+
* Read emails (handles attachments)
|
45
|
+
* Emails: label, archive, delete, mark as read/unread/spam, star
|
46
|
+
* Manage labels
|
47
|
+
* Create and send multipart email messages in plaintext and/or html, with inline
|
48
|
+
images and attachments
|
49
|
+
* Utilizes Gmail's IMAP & SMTP, MIME-type detection and parses and generates
|
50
|
+
MIME properly.
|
51
|
+
|
52
|
+
## Basic usage
|
53
|
+
|
54
|
+
First of all require the `gmail` library.
|
55
|
+
|
56
|
+
require 'gmail'
|
57
|
+
|
58
|
+
### Authenticating gmail sessions
|
59
|
+
|
60
|
+
This will you automatically log in to your account.
|
61
|
+
|
62
|
+
gmail = Gmail.connect(username, password)
|
63
|
+
# play with your gmail...
|
64
|
+
gmail.logout
|
65
|
+
|
66
|
+
If you pass a block, the session will be passed into the block, and the session
|
67
|
+
will be logged out after the block is executed.
|
68
|
+
|
69
|
+
Gmail.connect(username, password) do |gmail|
|
70
|
+
# play with your gmail...
|
71
|
+
end
|
72
|
+
|
73
|
+
Examples above are "quiet", it means that it will not raise any errors when
|
74
|
+
session couldn't be started (eg. because of connection error or invalid
|
75
|
+
authorization data). You can use connection which handles errors raising:
|
76
|
+
|
77
|
+
Gmail.connect!(username, password)
|
78
|
+
Gmail.connect!(username, password) {|gmail| ... play with gmail ... }
|
79
|
+
|
80
|
+
You can also check if you are logged in at any time:
|
81
|
+
|
82
|
+
Gmail.connect(username, password) do |gmail|
|
83
|
+
gmail.logged_in?
|
84
|
+
end
|
85
|
+
|
86
|
+
### Counting and gathering emails
|
87
|
+
|
88
|
+
Get counts for messages in the inbox:
|
89
|
+
|
90
|
+
gmail.inbox.count
|
91
|
+
gmail.inbox.count(:unread)
|
92
|
+
gmail.inbox.count(:read)
|
93
|
+
|
94
|
+
Count with some criteria:
|
95
|
+
|
96
|
+
gmail.inbox.count(:after => Date.parse("2010-02-20"), :before => Date.parse("2010-03-20"))
|
97
|
+
gmail.inbox.count(:on => Date.parse("2010-04-15"))
|
98
|
+
gmail.inbox.count(:from => "myfriend@gmail.com")
|
99
|
+
gmail.inbox.count(:to => "directlytome@gmail.com")
|
100
|
+
|
101
|
+
Combine flags and options:
|
102
|
+
|
103
|
+
gmail.inbox.count(:unread, :from => "myboss@gmail.com")
|
104
|
+
|
105
|
+
Browsing labeled emails is similar to work with inbox.
|
106
|
+
|
107
|
+
gmail.mailbox('Urgent').count
|
108
|
+
|
109
|
+
Getting messages works the same way as counting: Remember that every message in a
|
110
|
+
conversation/thread will come as a separate message.
|
111
|
+
|
112
|
+
gmail.inbox.emails(:unread, :before => Date.parse("2010-04-20"), :from => "myboss@gmail.com")
|
113
|
+
|
114
|
+
You can use also one of aliases:
|
115
|
+
|
116
|
+
gmail.inbox.find(...)
|
117
|
+
gmail.inbox.search(...)
|
118
|
+
gmail.inbox.mails(...)
|
119
|
+
|
120
|
+
Also you can manipulate each message using block style:
|
121
|
+
|
122
|
+
gmail.inbox.find(:unread) do |email|
|
123
|
+
email.read!
|
124
|
+
end
|
125
|
+
|
126
|
+
### Working with emails!
|
127
|
+
|
128
|
+
Any news older than 4-20, mark as read and archive it:
|
129
|
+
|
130
|
+
gmail.inbox.find(:before => Date.parse("2010-04-20"), :from => "news@nbcnews.com") do |email|
|
131
|
+
email.read! # can also unread!, spam! or star!
|
132
|
+
email.archive!
|
133
|
+
end
|
134
|
+
|
135
|
+
Delete emails from X:
|
136
|
+
|
137
|
+
gmail.inbox.find(:from => "x-fiance@gmail.com").each do |email|
|
138
|
+
email.delete!
|
139
|
+
end
|
140
|
+
|
141
|
+
Save all attachments in the "Faxes" label to a local folder:
|
142
|
+
|
143
|
+
folder = "/where/ever"
|
144
|
+
gmail.mailbox("Faxes").emails do |email|
|
145
|
+
if !email.message.attachments.empty?
|
146
|
+
email.message.save_attachments_to(folder)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
You can use also `#label` method instead of `#mailbox`:
|
151
|
+
|
152
|
+
gmail.label("Faxes").emails {|email| ... }
|
153
|
+
|
154
|
+
Save just the first attachment from the newest unread email (assuming pdf):
|
155
|
+
|
156
|
+
email = gmail.inbox.find(:unread).first
|
157
|
+
email.attachments[0].save_to_file("/path/to/location")
|
158
|
+
|
159
|
+
Add a label to a message:
|
160
|
+
|
161
|
+
email.label("Faxes")
|
162
|
+
|
163
|
+
Example above will raise error when you don't have the `Faxes` label. You can
|
164
|
+
avoid this using:
|
165
|
+
|
166
|
+
email.label!("Faxes") # The `Faxes` label will be automatically created now
|
167
|
+
|
168
|
+
You can also move message to a label/mailbox:
|
169
|
+
|
170
|
+
email.move_to("Faxes")
|
171
|
+
email.move_to!("NewLabel")
|
172
|
+
|
173
|
+
There is also few shortcuts to mark messages quickly:
|
174
|
+
|
175
|
+
email.read!
|
176
|
+
email.unread!
|
177
|
+
email.spam!
|
178
|
+
email.star!
|
179
|
+
email.unstar!
|
180
|
+
|
181
|
+
### Managing labels
|
182
|
+
|
183
|
+
With Gmail gem you can also manage your labels. You can get list of defined
|
184
|
+
labels:
|
185
|
+
|
186
|
+
gmail.labels.all
|
187
|
+
|
188
|
+
Create new label:
|
189
|
+
|
190
|
+
gmail.labels.new("Uregent")
|
191
|
+
gmail.labels.add("AnotherOne")
|
192
|
+
|
193
|
+
Remove labels:
|
194
|
+
|
195
|
+
gmail.labels.delete("Uregent")
|
196
|
+
|
197
|
+
Or check if given label exists:
|
198
|
+
|
199
|
+
gmail.labels.exists?("Uregent") # => false
|
200
|
+
gmail.labels.exists?("AnotherOne") # => true
|
201
|
+
|
202
|
+
### Composing and sending emails
|
203
|
+
|
204
|
+
Creating emails now uses the amazing [Mail](http://rubygems.org/gems/mail) rubygem.
|
205
|
+
See its [documentation here](http://github.com/mikel/mail). The Ruby Gmail will
|
206
|
+
automatically configure your Mail emails to be sent via your Gmail account's SMTP,
|
207
|
+
so they will be in your Gmail's "Sent" folder. Also, no need to specify the "From"
|
208
|
+
email either, because ruby-gmail will set it for you.
|
209
|
+
|
210
|
+
gmail.deliver do
|
211
|
+
to "email@example.com"
|
212
|
+
subject "Having fun in Puerto Rico!"
|
213
|
+
text_part do
|
214
|
+
body "Text of plaintext message."
|
215
|
+
end
|
216
|
+
html_part do
|
217
|
+
body "<p>Text of <em>html</em> message.</p>"
|
218
|
+
end
|
219
|
+
add_file "/path/to/some_image.jpg"
|
220
|
+
end
|
221
|
+
|
222
|
+
Or, generate the message first and send it later
|
223
|
+
|
224
|
+
email = gmail.generate_message do
|
225
|
+
to "email@example.com"
|
226
|
+
subject "Having fun in Puerto Rico!"
|
227
|
+
body "Spent the day on the road..."
|
228
|
+
end
|
229
|
+
email.deliver! # or: gmail.deliver(email)
|
230
|
+
|
231
|
+
## Note on Patches/Pull Requests
|
232
|
+
|
233
|
+
* Fork the project.
|
234
|
+
* Make your feature addition or bug fix.
|
235
|
+
* Add tests for it. This is important so I don't break it in a
|
236
|
+
future version unintentionally.
|
237
|
+
* Commit, do not mess with rakefile, version, or history.
|
238
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
239
|
+
* Send me a pull request. Bonus points for topic branches.
|
240
|
+
|
241
|
+
## Copyright
|
242
|
+
|
243
|
+
Copyrignt (c) 2010 Kriss 'nu7hatch' Kowalik
|
244
|
+
Copyright (c) 2009-2010 BehindLogic
|
245
|
+
|
246
|
+
See LICENSE for details.
|
247
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "gmail"
|
10
|
+
gem.summary = %Q{A Rubyesque interface to Gmail, with all the tools you'll need.}
|
11
|
+
gem.description = <<-DESCR
|
12
|
+
A Rubyesque interface to Gmail, with all the tools you'll need. Search,
|
13
|
+
read and send multipart emails; archive, mark as read/unread, delete emails;
|
14
|
+
and manage labels.
|
15
|
+
DESCR
|
16
|
+
gem.email = "kriss.kowalik@gmail.com"
|
17
|
+
gem.homepage = "http://github.com/nu7hatch/gmail"
|
18
|
+
gem.authors = ["BehindLogic", "Kriss 'nu7hatch' Kowalik"]
|
19
|
+
gem.add_dependency 'mime', '>= 0.1'
|
20
|
+
gem.add_dependency 'mail', '>= 2.2.1'
|
21
|
+
gem.add_development_dependency 'rspec', '~> 2.0'
|
22
|
+
gem.add_development_dependency 'mocha', '>= 0.9'
|
23
|
+
end
|
24
|
+
Jeweler::GemcutterTasks.new
|
25
|
+
rescue LoadError
|
26
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
31
|
+
t.pattern = 'spec/**/*_spec.rb'
|
32
|
+
t.rspec_opts = %q[--colour --backtrace]
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |t|
|
36
|
+
t.rcov = true
|
37
|
+
t.rspec_opts = %q[--colour --backtrace]
|
38
|
+
t.rcov_opts = %q[--exclude "spec" --text-report]
|
39
|
+
end
|
40
|
+
|
41
|
+
task :spec => :check_dependencies
|
42
|
+
task :rcov => :check_dependencies
|
43
|
+
task :default => :spec
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "Ruby GMail #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/TODO.md
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0
|
data/lib/gmail.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'net/imap'
|
2
|
+
require 'net/smtp'
|
3
|
+
require 'mail'
|
4
|
+
require 'date'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
if RUBY_VERSION < "1.8.7"
|
8
|
+
require "smtp_tls"
|
9
|
+
end
|
10
|
+
|
11
|
+
class Object
|
12
|
+
def to_imap_date
|
13
|
+
Date.parse(to_s).strftime("%d-%B-%Y")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module Gmail
|
18
|
+
autoload :Version, "gmail/version"
|
19
|
+
autoload :Client, "gmail/client"
|
20
|
+
autoload :Labels, "gmail/labels"
|
21
|
+
autoload :Mailbox, "gmail/mailbox"
|
22
|
+
autoload :Message, "gmail/message"
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def new(username, password, options={}, &block)
|
26
|
+
client = Client.new(username, password, options)
|
27
|
+
client.connect and client.login
|
28
|
+
if block_given?
|
29
|
+
yield client
|
30
|
+
client.logout
|
31
|
+
end
|
32
|
+
client
|
33
|
+
end
|
34
|
+
alias :connect :new
|
35
|
+
|
36
|
+
def new!(username, password, options={}, &block)
|
37
|
+
client = Client.new(username, password, options)
|
38
|
+
client.connect! and client.login!
|
39
|
+
if block_given?
|
40
|
+
yield client
|
41
|
+
client.logout
|
42
|
+
end
|
43
|
+
client
|
44
|
+
end
|
45
|
+
alias :connect! :new!
|
46
|
+
end # << self
|
47
|
+
end # Gmail
|