gmail-mailer 0.4.2 → 0.4.4
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/Changelog.md +5 -0
- data/Rakefile +6 -1
- data/VERSION +1 -1
- data/gmail-mailer.gemspec +2 -2
- data/lib/gmail-mailer.rb +43 -9
- data/test/mail_test_send.rb +2 -2
- data/test/test_message.rb +61 -2
- metadata +3 -3
data/Changelog.md
CHANGED
data/Rakefile
CHANGED
@@ -31,6 +31,12 @@ Rake::TestTask.new(:test) do |test|
|
|
31
31
|
test.libs << 'lib' << 'test'
|
32
32
|
test.pattern = 'test/**/test_*.rb'
|
33
33
|
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
Rake::TestTask.new(:test_mail) do |test|
|
37
|
+
test.libs << 'lib' << 'test'
|
38
|
+
test.verbose = true
|
39
|
+
test.test_files = ['test/mail_test_send.rb']
|
34
40
|
|
35
41
|
#tests sending a mail using user credentials (if provided)
|
36
42
|
root = File.expand_path(File.dirname(__FILE__))
|
@@ -41,7 +47,6 @@ Rake::TestTask.new(:test) do |test|
|
|
41
47
|
puts
|
42
48
|
puts "Will test sending a mail using your user credentials found in #{file}"
|
43
49
|
puts
|
44
|
-
test.test_files = ['test/mail_test_send.rb']
|
45
50
|
else
|
46
51
|
puts
|
47
52
|
puts "WILL NOT BE TESTING MAIL SEND FUNCTIONALITY. IF YOU WISH TO TEST THIS, CREATE A FILE WITH THE FOLLOWING: - "
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.4
|
data/gmail-mailer.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{gmail-mailer}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Daniel Harper"]
|
12
|
-
s.date = %q{2011-01-
|
12
|
+
s.date = %q{2011-01-16}
|
13
13
|
s.description = %q{Programatically send emails using a given gmail account. No username/passwords needed, just use your OAUTH credentials}
|
14
14
|
s.email = %q{djharperuk@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/gmail-mailer.rb
CHANGED
@@ -6,10 +6,16 @@ module GmailMailer
|
|
6
6
|
SMTP_HOST = "gmail.com"
|
7
7
|
SMTP_CONSUMER_KEY = "anonymous"
|
8
8
|
SMTP_CONSUMER_SECRET = "anonymous"
|
9
|
+
ATTACHMENTS_SIZE_LIMIT = (1024*1024)*25 #25mb attachment limit
|
10
|
+
RECIPIENT_LIMIT = 500
|
11
|
+
MAX_RETRY = 2
|
9
12
|
class Mailer
|
10
13
|
def initialize(credentials)
|
11
|
-
|
12
|
-
|
14
|
+
begin
|
15
|
+
validate_credentials(credentials)
|
16
|
+
rescue
|
17
|
+
raise
|
18
|
+
end
|
13
19
|
@email_credentials = credentials
|
14
20
|
end
|
15
21
|
|
@@ -24,11 +30,23 @@ module GmailMailer
|
|
24
30
|
mail.add_file(attachment)
|
25
31
|
end
|
26
32
|
end
|
27
|
-
|
33
|
+
|
34
|
+
retry_attempts = 0
|
35
|
+
|
36
|
+
begin
|
37
|
+
send_smtp(mail)
|
38
|
+
rescue => message
|
39
|
+
puts "Error occured attempting to send mail => #{message}"
|
40
|
+
|
41
|
+
raise message if(retry_attempts > MAX_RETRY)
|
42
|
+
puts "Retry: #{retry_attempts+1}/#{MAX_RETRY+1}"
|
43
|
+
retry_attempts = retry_attempts.succ
|
44
|
+
retry
|
45
|
+
end
|
28
46
|
end
|
29
47
|
|
30
48
|
# Use gmail_xoauth to send email
|
31
|
-
def
|
49
|
+
def send_smtp(mail)
|
32
50
|
smtp = Net::SMTP.new(SMTP_SERVER, SMTP_PORT)
|
33
51
|
smtp.enable_starttls_auto
|
34
52
|
secret = {
|
@@ -45,10 +63,11 @@ module GmailMailer
|
|
45
63
|
end
|
46
64
|
|
47
65
|
def validate_credentials(creds)
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
66
|
+
msg = "ERROR: Email credentials are invalid:"
|
67
|
+
raise ArgumentError, "#{msg} The credentials you have posted are nil" if creds.nil?
|
68
|
+
raise ArgumentError, "#{msg} You must provide a smtp_oauth_token value!" if !creds.key?:smtp_oauth_token or creds[:smtp_oauth_token].nil? or creds[:smtp_oauth_token].empty?
|
69
|
+
raise ArgumentError, "#{msg} You must provide a smtp_oauth_token_secret value!" if !creds.key?:smtp_oauth_token_secret or creds[:smtp_oauth_token_secret].nil? or creds[:smtp_oauth_token_secret].empty?
|
70
|
+
raise ArgumentError, "#{msg} You must provide an email value" if !creds.key?:email or creds[:email].nil? or creds[:email].empty?
|
52
71
|
return nil
|
53
72
|
end
|
54
73
|
end
|
@@ -67,13 +86,28 @@ module GmailMailer
|
|
67
86
|
raise ArgumentError, "You must specify a file to send" if filepath.nil? or filepath.empty?
|
68
87
|
raise ArgumentError, "File #{filepath} does not exist" if !File.exist?(filepath)
|
69
88
|
raise ArgumentError, "#{filepath} file is a directory, this is not supported" if File.directory?(filepath)
|
89
|
+
|
90
|
+
size = File.size(filepath)
|
91
|
+
print "Adding attachment: #{filepath} "
|
92
|
+
printf("(%5.4f kb)",size.to_f/1024.0)
|
93
|
+
puts
|
94
|
+
raise ArgumentError, "There is a #{ATTACHMENTS_SIZE_LIMIT/1024/1024}mb limit on attachments}" if (get_attachments_size + size) > ATTACHMENTS_SIZE_LIMIT
|
70
95
|
@attachments << filepath
|
71
96
|
end
|
72
97
|
|
73
98
|
def to=(to)
|
74
99
|
raise ArgumentError, "You must specify an email address to send the message to!" if(to.nil? or to.empty?)
|
75
|
-
|
100
|
+
raise ArgumentError, "You cannot send a message to more than #{RECIPIENT_LIMIT} recipients" if to.is_a?Array and to.count > RECIPIENT_LIMIT
|
101
|
+
@to = to.join(";") if to.is_a?Array
|
76
102
|
@to = to if to.is_a?String
|
77
103
|
end
|
104
|
+
|
105
|
+
def get_attachments_size
|
106
|
+
attachments_size = 0
|
107
|
+
@attachments.each do |attachment|
|
108
|
+
attachments_size += File.size(attachment)
|
109
|
+
end
|
110
|
+
return attachments_size
|
111
|
+
end
|
78
112
|
end
|
79
113
|
end
|
data/test/mail_test_send.rb
CHANGED
@@ -13,11 +13,11 @@ class TestMailSender < Test::Unit::TestCase
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_send_message
|
16
|
-
|
16
|
+
assert_nothing_raised {
|
17
17
|
mailer = GmailMailer::Mailer.new(@config)
|
18
18
|
message = GmailMailer::Message.new(@config[:email], "Hello Subject", "Hello Body")
|
19
19
|
mailer.send(message)
|
20
|
-
|
20
|
+
}
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
data/test/test_message.rb
CHANGED
@@ -2,6 +2,12 @@ require "gmail-mailer.rb"
|
|
2
2
|
require 'fileutils'
|
3
3
|
require 'test/unit'
|
4
4
|
class TestMessage < Test::Unit::TestCase
|
5
|
+
def create_file(filename, size)
|
6
|
+
File.open(filename,'w') do |f|
|
7
|
+
f.puts "-"*size
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
5
11
|
def setup
|
6
12
|
@files = ['file1','file2','file3']
|
7
13
|
@dir = File.expand_path("./tmpdir")
|
@@ -20,11 +26,10 @@ class TestMessage < Test::Unit::TestCase
|
|
20
26
|
|
21
27
|
def createTmpFiles
|
22
28
|
@files.each do |file|
|
23
|
-
|
29
|
+
create_file(file, rand(1024))
|
24
30
|
end
|
25
31
|
end
|
26
32
|
|
27
|
-
|
28
33
|
def test_constructor
|
29
34
|
@msg = GmailMailer::Message.new("djharperuk@gmail.com","Subject","Body")
|
30
35
|
assert_not_nil(@msg, "Item did not appear to construct correctly")
|
@@ -67,6 +72,13 @@ class TestMessage < Test::Unit::TestCase
|
|
67
72
|
expected = to.join(";")
|
68
73
|
assert_equal(expected, @msg.to, "Multiple receivers not detected")
|
69
74
|
end
|
75
|
+
|
76
|
+
def test_to_with_array_of_more_than_acceptable_recipients
|
77
|
+
to = ("a@b.c,"*501).split(',')
|
78
|
+
assert_raise(ArgumentError) {
|
79
|
+
@msg.to= to
|
80
|
+
}
|
81
|
+
end
|
70
82
|
|
71
83
|
|
72
84
|
def test_add_attachment
|
@@ -98,5 +110,52 @@ class TestMessage < Test::Unit::TestCase
|
|
98
110
|
assert_raise(ArgumentError) { @msg.add_attachment(@dir) }
|
99
111
|
end
|
100
112
|
|
113
|
+
def test_get_attachment_size
|
114
|
+
begin
|
115
|
+
file = "file5"
|
116
|
+
create_file(file, 1024*1024)
|
117
|
+
size = File.size(file)
|
118
|
+
@msg.add_attachment(file)
|
119
|
+
assert_equal(size, @msg.get_attachments_size)
|
120
|
+
ensure
|
121
|
+
FileUtils.rm(file)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_get_attachment_size_with_multiple_attachments
|
126
|
+
begin
|
127
|
+
file = "file5"
|
128
|
+
file2 = "file6"
|
129
|
+
create_file(file, 1024*1024)
|
130
|
+
create_file(file2,(1024*1024)*5)
|
131
|
+
size = File.size(file)
|
132
|
+
size2 = File.size(file2)
|
133
|
+
@msg.add_attachment(file)
|
134
|
+
@msg.add_attachment(file2)
|
135
|
+
assert_equal(size+size2, @msg.get_attachments_size)
|
136
|
+
ensure
|
137
|
+
FileUtils.rm(file)
|
138
|
+
FileUtils.rm(file2)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_add_attachment_with_over_limit
|
143
|
+
begin
|
144
|
+
|
145
|
+
file = "file5"
|
146
|
+
file2 = "file6"
|
147
|
+
create_file(file, 1024*1024)
|
148
|
+
create_file(file2,(1024*1024)*25)
|
149
|
+
size = File.size(file)
|
150
|
+
size2 = File.size(file2)
|
151
|
+
assert_raise(ArgumentError) {
|
152
|
+
@msg.add_attachment(file)
|
153
|
+
@msg.add_attachment(file2)
|
154
|
+
}
|
155
|
+
ensure
|
156
|
+
FileUtils.rm(file)
|
157
|
+
FileUtils.rm(file2)
|
158
|
+
end
|
159
|
+
end
|
101
160
|
end
|
102
161
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 4
|
9
|
+
version: 0.4.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Daniel Harper
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-16 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|