gmail-mailer 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,21 @@
1
- = gmail-mailer Changelog
1
+ # gmail-mailer Changelog
2
2
 
3
- === 0.3.0
3
+ ### 0.4.0
4
+ * Added further validation on the `to` field
5
+ * Added ability to pass an array into the `to` field which will convert the `to` to a semi-colon delimited string
6
+ * Added getther and setter to `to` field for validation purposes
7
+ * Added support for multiple attachments
8
+ * Added support for multiple receivers
9
+ * Added further validation for email credentials/reduced number of credentials needed by making some values default
10
+ * Added some primitive unit tests for sending email
11
+
12
+ ### 0.3.0
4
13
  * Removed the `from` field from `GmailMailer::Message`
5
14
  * Added accessors for `to`, `subject`, `body` fields
6
15
  * put validation on `to` field.
7
- === 0.2.0
16
+
17
+ ### 0.2.0
8
18
  * Removed attachments from the constructor and added `add_attachment` method
9
- === 0.1.0
19
+
20
+ ### 0.1.0
10
21
  * Initial version
data/Rakefile CHANGED
@@ -19,8 +19,40 @@ Jeweler::Tasks.new do |gem|
19
19
  gem.description = %Q{Programatically send emails using a given gmail account. No username/passwords needed, just use your OAUTH credentials}
20
20
  gem.email = "djharperuk@gmail.com"
21
21
  gem.authors = ["Daniel Harper"]
22
+ gem.required_ruby_version = ">= 1.9.2"
22
23
  gem.add_dependency "mail","~> 2.2.14"
23
24
  gem.add_dependency "gmail_xoauth", "~> 0.3.0"
24
25
  end
25
26
  Jeweler::RubygemsDotOrgTasks.new
26
27
 
28
+ require 'rake/testtask'
29
+ PRIVATE_CREDENTIALS = "files/private_key_conf_file"
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+
35
+ #tests sending a mail using user credentials (if provided)
36
+ root = File.expand_path(File.dirname(__FILE__))
37
+ root = File.expand_path("./test", root)
38
+ file = File.join(root, "/#{PRIVATE_CREDENTIALS}")
39
+
40
+ if File.exist?(file)
41
+ puts
42
+ puts "Will test sending a mail using your user credentials found in #{file}"
43
+ puts
44
+ test.test_files = ['test/mail_test_send.rb']
45
+ else
46
+ puts
47
+ puts "WILL NOT BE TESTING MAIL SEND FUNCTIONALITY. IF YOU WISH TO TEST THIS, CREATE A FILE WITH THE FOLLOWING: - "
48
+ puts
49
+ puts " smtp_oauth_token: <your outh token>"
50
+ puts " smtp_oauth_token_secret: <your oauth token secret>"
51
+ puts " email: <your email>"
52
+ puts
53
+ puts "place this information in test/files/private_key_conf_file"
54
+ puts
55
+ end
56
+ end
57
+
58
+ task :default => :test
data/TODO.md ADDED
@@ -0,0 +1,9 @@
1
+ #gmail-mailer TODO LIST
2
+
3
+ * Better validation for email configuration
4
+ * Add better handling for SMTP errors
5
+ * unit testing- STARTED
6
+ ** Need some sort of mock testing framework for SMTP messages
7
+ * add support for adding directories
8
+ * add size limits/checking for attachments
9
+ * reconfigure the way mailer sends message, queue messages up THEN send
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{gmail-mailer}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
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-09}
12
+ s.date = %q{2011-01-14}
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 = [
@@ -24,16 +24,26 @@ Gem::Specification.new do |s|
24
24
  "LICENSE.txt",
25
25
  "README.md",
26
26
  "Rakefile",
27
- "TODO.txt",
27
+ "TODO.md",
28
28
  "VERSION",
29
29
  "gmail-mailer.gemspec",
30
- "lib/gmail-mailer.rb"
30
+ "lib/gmail-mailer.rb",
31
+ "test/files/email_conf",
32
+ "test/mail_test_send.rb",
33
+ "test/test_mailer.rb",
34
+ "test/test_message.rb"
31
35
  ]
32
36
  s.homepage = %q{http://github.com/djhworld/gmail-mailer}
33
37
  s.licenses = ["MIT"]
34
38
  s.require_paths = ["lib"]
39
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9.2")
35
40
  s.rubygems_version = %q{1.3.7}
36
41
  s.summary = %q{Send emails using a your gmail account (via OAUTH)}
42
+ s.test_files = [
43
+ "test/mail_test_send.rb",
44
+ "test/test_mailer.rb",
45
+ "test/test_message.rb"
46
+ ]
37
47
 
38
48
  if s.respond_to? :specification_version then
39
49
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -1,9 +1,15 @@
1
1
  require 'mail'
2
2
  require 'gmail_xoauth'
3
3
  module GmailMailer
4
+ SMTP_SERVER = "smtp.gmail.com"
5
+ SMTP_PORT = "587"
6
+ SMTP_HOST = "gmail.com"
7
+ SMTP_CONSUMER_KEY = "anonymous"
8
+ SMTP_CONSUMER_SECRET = "anonymous"
4
9
  class Mailer
5
10
  def initialize(credentials)
6
- raise ArgumentError if !validate_credentials(credentials)
11
+ result = validate_credentials(credentials)
12
+ raise ArgumentError, result if result.nil? == false
7
13
  @email_credentials = credentials
8
14
  end
9
15
 
@@ -12,8 +18,10 @@ module GmailMailer
12
18
  to message.to
13
19
  subject message.subject
14
20
  body message.body
15
- if(!message.attachments.empty?)
16
- add_file message.attachments.first
21
+ end
22
+ if(!message.attachments.empty?)
23
+ message.attachments.each do |attachment|
24
+ mail.add_file(attachment)
17
25
  end
18
26
  end
19
27
  sendSMTP(mail)
@@ -21,15 +29,15 @@ module GmailMailer
21
29
 
22
30
  # Use gmail_xoauth to send email
23
31
  def sendSMTP(mail)
24
- smtp = Net::SMTP.new(@email_credentials[:smtp_server], @email_credentials[:smtp_port])
32
+ smtp = Net::SMTP.new(SMTP_SERVER, SMTP_PORT)
25
33
  smtp.enable_starttls_auto
26
34
  secret = {
27
- :consumer_key => @email_credentials[:smtp_consumer_key],
28
- :consumer_secret => @email_credentials[:smtp_consumer_secret],
35
+ :consumer_key => SMTP_CONSUMER_KEY,
36
+ :consumer_secret => SMTP_CONSUMER_SECRET,
29
37
  :token => @email_credentials[:smtp_oauth_token],
30
38
  :token_secret => @email_credentials[:smtp_oauth_token_secret]
31
39
  }
32
- smtp.start(@email_credentials[:host], @email_credentials[:email], secret, :xoauth) do |session|
40
+ smtp.start(SMTP_HOST, @email_credentials[:email], secret, :xoauth) do |session|
33
41
  print "Sending message..."
34
42
  session.send_message(mail.encoded, mail.from_addrs.first, mail.destinations)
35
43
  puts ".sent!"
@@ -37,32 +45,35 @@ module GmailMailer
37
45
  end
38
46
 
39
47
  def validate_credentials(creds)
40
- false if creds.nil?
41
- false if !creds.key?:smtp_server or creds[:smtp_server].nil?
42
- false if !creds.key?:smtp_port or creds[:smtp_port].nil?
43
- false if !creds.key?:smtp_consumer_key or creds[:smtp_consumer_key].nil?
44
- false if !creds.key?:smtp_consumer_secret or creds[:smtp_consumer_secret].nil?
45
- false if !creds.key?:smtp_oauth_token or creds[:smtp_oauth_token].nil?
46
- false if !creds.key?:smtp_oauth_token_secret or creds[:smtp_oauth_token_secret].nil?
47
- false if !creds.key?:host or creds[:host].nil?
48
- false if !creds.key?:email or creds[:email].nil?
49
- return true
48
+ return "The credentials you have posted are nil" if creds.nil?
49
+ return "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?
50
+ return "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?
51
+ return "You must provide an email value" if !creds.key?:email or creds[:email].nil? or creds[:email].empty?
52
+ return nil
50
53
  end
51
54
  end
52
55
 
53
56
  class Message
54
- attr_accessor :to, :subject, :body
55
- attr_reader :attachments
57
+ attr_accessor :subject, :body
58
+ attr_reader :to, :attachments
56
59
  def initialize(to, subject="", body="")
57
- raise ArgumentError, "You must specify an email address to send the message to!" if(to.nil? or to.empty?)
58
- @to = to
60
+ self.to=(to)
59
61
  @subject = subject
60
62
  @body = body
61
63
  @attachments = []
62
64
  end
63
65
 
64
66
  def add_attachment(filepath)
65
- @attachments << filepath if !filepath.nil? or !filepath.empty?
67
+ raise ArgumentError, "You must specify a file to send" if filepath.nil? or filepath.empty?
68
+ raise ArgumentError, "File #{filepath} does not exist" if !File.exist?(filepath)
69
+ raise ArgumentError, "#{filepath} file is a directory, this is not supported" if File.directory?(filepath)
70
+ @attachments << filepath
71
+ end
72
+
73
+ def to=(to)
74
+ raise ArgumentError, "You must specify an email address to send the message to!" if(to.nil? or to.empty?)
75
+ @to = to.join(";") if to.is_a?Array
76
+ @to = to if to.is_a?String
66
77
  end
67
78
  end
68
79
  end
@@ -0,0 +1,3 @@
1
+ smtp_oauth_token: 123456789
2
+ smtp_oauth_token_secret: 123456789
3
+ email: d@gmail.com
@@ -0,0 +1,24 @@
1
+ require 'gmail-mailer.rb'
2
+ require 'yaml'
3
+ require 'test/unit'
4
+
5
+ class TestMailSender < Test::Unit::TestCase
6
+ PRIVATE_CREDENTIALS = "files/private_key_conf_file"
7
+ def setup
8
+ root = File.expand_path(File.dirname(__FILE__))
9
+ root = File.expand_path("../test", root)
10
+ file = File.join(root, "/#{PRIVATE_CREDENTIALS}")
11
+ raise Exception, "You have not setup a credentials file for your gmail account to test with" if !File.exists?(file)
12
+ @config = YAML.load_file(file).inject({}){|memo,(k,v)| memo[k.to_sym] = v.to_s; memo}
13
+ end
14
+
15
+ def test_send_message
16
+ assert_nothing_raised {
17
+ mailer = GmailMailer::Mailer.new(@config)
18
+ message = GmailMailer::Message.new(@config[:email], "Hello Subject", "Hello Body")
19
+ mailer.send(message)
20
+ }
21
+ end
22
+ end
23
+
24
+
@@ -0,0 +1,88 @@
1
+ require "gmail-mailer.rb"
2
+ require 'yaml'
3
+ require 'test/unit'
4
+ class TestMailer < Test::Unit::TestCase
5
+ CREDS_FILE = "files/email_conf"
6
+ def setup
7
+ root = File.expand_path(File.dirname(__FILE__))
8
+ root = File.expand_path("../test", root)
9
+ file = File.join(root, "/#{CREDS_FILE}")
10
+ @credentials = YAML.load_file(file).inject({}){|memo,(k,v)| memo[k.to_sym] = v.to_s; memo}
11
+ @mailer = GmailMailer::Mailer.new(@credentials)
12
+ end
13
+
14
+ def teardown
15
+ @credentials = nil
16
+ end
17
+
18
+ def test_constructor
19
+ assert_nothing_raised { @mailer = GmailMailer::Mailer.new(@credentials) }
20
+ end
21
+
22
+ def test_constructor_with_nil_credentials
23
+ assert_raise(ArgumentError) { @mailer = GmailMailer::Mailer.new(nil) }
24
+ end
25
+
26
+ def test_constructor_with_no_smtp_oauth_token
27
+ assert_raise(ArgumentError) {
28
+ @credentials.delete(:smtp_oauth_token)
29
+ @mailer = GmailMailer::Mailer.new(@credentials)
30
+ }
31
+ end
32
+
33
+ def test_constructor_with_nil_smtp_outh_token
34
+ assert_raise(ArgumentError) {
35
+ @credentials[:smtp_oauth_token] = nil
36
+ @mailer = GmailMailer::Mailer.new(@credentials)
37
+ }
38
+ end
39
+
40
+ def test_constructor_with_empty_smtp_oauth_token
41
+ assert_raise(ArgumentError) {
42
+ @credentials[:smtp_oauth_token] = ""
43
+ @mailer = GmailMailer::Mailer.new(@credentials)
44
+ }
45
+ end
46
+
47
+ def test_constructor_with_no_smtp_oauth_token_secret
48
+ assert_raise(ArgumentError) {
49
+ @credentials.delete(:smtp_oauth_token_secret)
50
+ @mailer = GmailMailer::Mailer.new(@credentials)
51
+ }
52
+ end
53
+
54
+ def test_constructor_with_nil_smtp_outh_token_secret
55
+ assert_raise(ArgumentError) {
56
+ @credentials[:smtp_oauth_token_secret] = nil
57
+ @mailer = GmailMailer::Mailer.new(@credentials)
58
+ }
59
+ end
60
+
61
+ def test_constructor_with_empty_smtp_oauth_token_secret
62
+ assert_raise(ArgumentError) {
63
+ @credentials[:smtp_oauth_token_secret] = ""
64
+ @mailer = GmailMailer::Mailer.new(@credentials)
65
+ }
66
+ end
67
+
68
+ def test_constructor_with_no_email
69
+ assert_raise(ArgumentError) {
70
+ @credentials.delete(:email)
71
+ @mailer = GmailMailer::Mailer.new(@credentials)
72
+ }
73
+ end
74
+
75
+ def test_constructor_with_nil_email
76
+ assert_raise(ArgumentError) {
77
+ @credentials[:email] = nil
78
+ @mailer = GmailMailer::Mailer.new(@credentials)
79
+ }
80
+ end
81
+
82
+ def test_constructor_with_empty_email
83
+ assert_raise(ArgumentError) {
84
+ @credentials[:email] = ""
85
+ @mailer = GmailMailer::Mailer.new(@credentials)
86
+ }
87
+ end
88
+ end
@@ -0,0 +1,102 @@
1
+ require "gmail-mailer.rb"
2
+ require 'fileutils'
3
+ require 'test/unit'
4
+ class TestMessage < Test::Unit::TestCase
5
+ def setup
6
+ @files = ['file1','file2','file3']
7
+ @dir = File.expand_path("./tmpdir")
8
+ Dir.mkdir(@dir)
9
+ createTmpFiles
10
+ @msg = GmailMailer::Message.new("to","subject","body")
11
+ end
12
+
13
+ def teardown
14
+ @files.each do |file|
15
+ FileUtils.rm(file)
16
+ end
17
+ Dir.rmdir(@dir)
18
+ @msg = nil
19
+ end
20
+
21
+ def createTmpFiles
22
+ @files.each do |file|
23
+ FileUtils.touch(file)
24
+ end
25
+ end
26
+
27
+
28
+ def test_constructor
29
+ @msg = GmailMailer::Message.new("djharperuk@gmail.com","Subject","Body")
30
+ assert_not_nil(@msg, "Item did not appear to construct correctly")
31
+ end
32
+
33
+ def test_constructor_for_attributes
34
+ to, subject, body = "a@b.c", "subject", "body"
35
+ @msg = GmailMailer::Message.new(to, subject, body)
36
+ assert_equal(to, @msg.to)
37
+ assert_equal(subject, @msg.subject)
38
+ assert_equal(body, @msg.body)
39
+ assert_equal([], @msg.attachments)
40
+ end
41
+
42
+ def test_constructor_throws_exc_on_empty_to_field
43
+ to, subject, body = "", "subject", "body"
44
+ assert_raise(ArgumentError) { @msg = GmailMailer::Message.new(to, subject, body) }
45
+ end
46
+
47
+ def test_constructor_multiple_receivers
48
+ first = "a@b.c"
49
+ second = "b@c.d"
50
+ to = [first,second]
51
+ @msg = GmailMailer::Message.new(to, "", "")
52
+ expected = to.join(";")
53
+ assert_equal(expected, @msg.to, "Multiple receivers not detected")
54
+ end
55
+
56
+ def test_to_=
57
+ to = "a@b.c"
58
+ @msg.to= to
59
+ assert_equal(to, @msg.to)
60
+ end
61
+
62
+ def test_to_with_array
63
+ first = "a@b.c"
64
+ second = "b@c.d"
65
+ to = [first,second]
66
+ @msg.to= to
67
+ expected = to.join(";")
68
+ assert_equal(expected, @msg.to, "Multiple receivers not detected")
69
+ end
70
+
71
+
72
+ def test_add_attachment
73
+ @msg.add_attachment(@files.first)
74
+ assert_equal(1,@msg.attachments.count, "There should be 1 item in the list!")
75
+ end
76
+
77
+ def test_add_attachments
78
+ @msg.add_attachment(@files.pop)
79
+ @msg.add_attachment(@files.pop)
80
+ assert_equal(2,@msg.attachments.count, "There should be 2 items in the list!")
81
+ end
82
+
83
+ def test_add_attachment_with_empty_string
84
+ assert_raise(ArgumentError) { @msg.add_attachment("") }
85
+ assert_equal(0, @msg.attachments.count, "There should be no items in the list!")
86
+ end
87
+
88
+ def test_add_attachment_with_nil_string
89
+ assert_raise(ArgumentError) { @msg.add_attachment("") }
90
+ assert_equal(0, @msg.attachments.count, "There should be no items in the list!")
91
+ end
92
+
93
+ def test_add_file_that_doesnt_exist
94
+ assert_raise(ArgumentError) { @msg.add_attachment("asjdasjdhasdjhaksjdh") }
95
+ end
96
+
97
+ def test_add_file_that_is_actually_a_dir
98
+ assert_raise(ArgumentError) { @msg.add_attachment(@dir) }
99
+ end
100
+
101
+ end
102
+
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
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-09 00:00:00 +00:00
17
+ date: 2011-01-14 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -124,10 +124,14 @@ files:
124
124
  - LICENSE.txt
125
125
  - README.md
126
126
  - Rakefile
127
- - TODO.txt
127
+ - TODO.md
128
128
  - VERSION
129
129
  - gmail-mailer.gemspec
130
130
  - lib/gmail-mailer.rb
131
+ - test/files/email_conf
132
+ - test/mail_test_send.rb
133
+ - test/test_mailer.rb
134
+ - test/test_message.rb
131
135
  has_rdoc: true
132
136
  homepage: http://github.com/djhworld/gmail-mailer
133
137
  licenses:
@@ -142,10 +146,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
146
  requirements:
143
147
  - - ">="
144
148
  - !ruby/object:Gem::Version
145
- hash: -3791998660236781509
146
149
  segments:
147
- - 0
148
- version: "0"
150
+ - 1
151
+ - 9
152
+ - 2
153
+ version: 1.9.2
149
154
  required_rubygems_version: !ruby/object:Gem::Requirement
150
155
  none: false
151
156
  requirements:
@@ -161,5 +166,7 @@ rubygems_version: 1.3.7
161
166
  signing_key:
162
167
  specification_version: 3
163
168
  summary: Send emails using a your gmail account (via OAUTH)
164
- test_files: []
165
-
169
+ test_files:
170
+ - test/mail_test_send.rb
171
+ - test/test_mailer.rb
172
+ - test/test_message.rb
data/TODO.txt DELETED
@@ -1,4 +0,0 @@
1
- changes needed for next version: -
2
- - Better validation for email configuration
3
- - Add better handling for SMTP errors
4
- - unit testing