ez-email 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +5 -0
- data/README +1 -1
- data/ez-email.gemspec +4 -2
- data/lib/ez/email.rb +128 -130
- data/test/test_ez_email.rb +137 -67
- metadata +39 -40
data/CHANGES
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
== 0.1.3 - 9-Jan-2013
|
2
|
+
* Fixed a bug where the default 'from' value was not actually being set
|
3
|
+
if it wasn't explicitly specified.
|
4
|
+
* Refactored the tests and use test-unit 2 instead.
|
5
|
+
|
1
6
|
== 0.1.2 - 31-Aug-2011
|
2
7
|
* Refactored the Rakefile. Removed the old install task, added a clean
|
3
8
|
task, added a default task, and reorganized the gem tasks.
|
data/README
CHANGED
data/ez-email.gemspec
CHANGED
@@ -2,16 +2,18 @@ require 'rubygems'
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'ez-email'
|
5
|
-
s.version = '0.1.
|
5
|
+
s.version = '0.1.3'
|
6
6
|
s.license = 'Artistic 2.0'
|
7
7
|
s.summary = 'Really easy emails'
|
8
8
|
s.description = 'A very simple interface for sending email'
|
9
9
|
s.author = 'Daniel Berger'
|
10
10
|
s.email = 'djberg96@gmail.com'
|
11
|
-
s.homepage = '
|
11
|
+
s.homepage = 'https://github.com/djberg96/ez-email'
|
12
12
|
s.files = Dir['**/*'].reject{ |f| f.include?('git') }
|
13
13
|
s.test_file = 'test/test_ez_email.rb'
|
14
14
|
|
15
15
|
s.rubyforge_project = 'shards'
|
16
16
|
s.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
|
17
|
+
|
18
|
+
s.add_development_dependency('test-unit')
|
17
19
|
end
|
data/lib/ez/email.rb
CHANGED
@@ -5,138 +5,136 @@ require 'net/smtp'
|
|
5
5
|
|
6
6
|
# The EZ module serves as a namespace only.
|
7
7
|
module EZ
|
8
|
-
|
9
|
-
# The Email class encapsulates certain SMTP attributes, which are then
|
10
|
-
# used to send simple emails.
|
11
|
-
class Email
|
12
|
-
# The version of the ez-email library
|
13
|
-
VERSION = '0.1.2'
|
14
|
-
|
15
|
-
begin
|
16
|
-
@@mail_host = Resolv.getaddress('mailhost')
|
17
|
-
rescue Resolv::ResolvError
|
18
|
-
@@mail_host = 'localhost'
|
19
|
-
end
|
20
8
|
|
9
|
+
# The Email class encapsulates certain SMTP attributes, which are then
|
10
|
+
# used to send simple emails.
|
11
|
+
class Email
|
12
|
+
# The version of the ez-email library
|
13
|
+
VERSION = '0.1.3'
|
14
|
+
|
15
|
+
begin
|
16
|
+
@@mail_host = Resolv.getaddress('mailhost')
|
17
|
+
rescue Resolv::ResolvError
|
18
|
+
@@mail_host = 'localhost'
|
19
|
+
end
|
20
|
+
|
21
|
+
@@mail_port = 25
|
22
|
+
|
23
|
+
# The name of the mail host to use when sending email. The default
|
24
|
+
# is whatever the address of your system's 'mailhost' resolves to.
|
25
|
+
# If that cannot be determined, your localhost is used.
|
26
|
+
#
|
27
|
+
def self.mail_host
|
28
|
+
@@mail_host
|
29
|
+
end
|
30
|
+
|
31
|
+
# Sets the mail host.
|
32
|
+
def self.mail_host=(host)
|
33
|
+
@@mail_host = host
|
34
|
+
end
|
35
|
+
|
36
|
+
# The port to use when sending email. The default is 25.
|
37
|
+
def self.mail_port
|
38
|
+
@@mail_port
|
39
|
+
end
|
40
|
+
|
41
|
+
# Sets the mail port.
|
42
|
+
def self.mail_port=(port)
|
21
43
|
@@mail_port = 25
|
22
|
-
|
23
|
-
# The name of the mail host to use when sending email. The default
|
24
|
-
# is whatever the address of your system's 'mailhost' resolves to.
|
25
|
-
# If that cannot be determined, your localhost is used.
|
26
|
-
#
|
27
|
-
def self.mail_host
|
28
|
-
@@mail_host
|
29
|
-
end
|
30
|
-
|
31
|
-
# Sets the mail host.
|
32
|
-
def self.mail_host=(host)
|
33
|
-
@@mail_host = host
|
34
|
-
end
|
35
|
-
|
36
|
-
# The port to use when sending email. The default is 25.
|
37
|
-
def self.mail_port
|
38
|
-
@@mail_port
|
39
|
-
end
|
40
|
-
|
41
|
-
# Sets the mail port.
|
42
|
-
def self.mail_port=(port)
|
43
|
-
@@mail_port = 25
|
44
|
-
end
|
45
|
-
|
46
|
-
# A single email address or an array of email addresses to whom
|
47
|
-
# the email should be sent. Mandatory.
|
48
|
-
attr_accessor :to
|
49
|
-
|
50
|
-
# A single email address from whom the email is being delivered. The
|
51
|
-
# default is your login + '@' + your host name, though it is recommended
|
52
|
-
# that you specify it explicitly.
|
53
|
-
attr_accessor :from
|
54
|
-
|
55
|
-
# The subject of the email. Mandatory.
|
56
|
-
attr_accessor :subject
|
57
|
-
|
58
|
-
# The body of the email. Mandatory.
|
59
|
-
attr_accessor :body
|
60
|
-
|
61
|
-
# Creates a new EZ::Email object. As a general rule you won't use
|
62
|
-
# this method, but should use EZ::Email.deliver instead.
|
63
|
-
#
|
64
|
-
def initialize(options={})
|
65
|
-
raise TypeError unless options.is_a?(Hash)
|
66
|
-
validate_options(options)
|
67
|
-
@options = options
|
68
|
-
end
|
69
|
-
|
70
|
-
# Sends the email based on the options passed to the constructor.
|
71
|
-
# As a general rule you won't use this method directly, but should
|
72
|
-
# use EZ::Email.deliver instead.
|
73
|
-
#
|
74
|
-
def deliver
|
75
|
-
host = EZ::Email.mail_host
|
76
|
-
port = EZ::Email.mail_port
|
77
|
-
|
78
|
-
Net::SMTP.start(host, port, host){ |smtp|
|
79
|
-
smtp.open_message_stream(self.from, self.to){ |stream|
|
80
|
-
stream.puts "From: #{self.from}"
|
81
|
-
stream.puts "To: " + self.to.to_a.join(', ')
|
82
|
-
stream.puts "Subject: #{self.subject}"
|
83
|
-
stream.puts
|
84
|
-
stream.puts self.body
|
85
|
-
}
|
86
|
-
}
|
87
|
-
end
|
44
|
+
end
|
88
45
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
46
|
+
# A single email address or an array of email addresses to whom
|
47
|
+
# the email should be sent. Mandatory.
|
48
|
+
attr_accessor :to
|
49
|
+
|
50
|
+
# A single email address from whom the email is being delivered. The
|
51
|
+
# default is your login + '@' + your host name, though it is recommended
|
52
|
+
# that you specify it explicitly.
|
53
|
+
attr_accessor :from
|
54
|
+
|
55
|
+
# The subject of the email. Mandatory.
|
56
|
+
attr_accessor :subject
|
57
|
+
|
58
|
+
# The body of the email. Mandatory.
|
59
|
+
attr_accessor :body
|
60
|
+
|
61
|
+
# Creates a new EZ::Email object. As a general rule you won't use
|
62
|
+
# this method, but should use EZ::Email.deliver instead.
|
63
|
+
#
|
64
|
+
def initialize(options={})
|
65
|
+
raise TypeError unless options.is_a?(Hash)
|
66
|
+
options[:from] ||= Etc.getlogin + '@' + Socket.gethostname
|
67
|
+
validate_options(options)
|
68
|
+
@options = options
|
69
|
+
end
|
70
|
+
|
71
|
+
# Sends the email based on the options passed to the constructor.
|
72
|
+
# As a general rule you won't use this method directly, but should
|
73
|
+
# use EZ::Email.deliver instead.
|
74
|
+
#
|
75
|
+
def deliver
|
76
|
+
host = EZ::Email.mail_host
|
77
|
+
port = EZ::Email.mail_port
|
78
|
+
|
79
|
+
Net::SMTP.start(host, port, host){ |smtp|
|
80
|
+
smtp.open_message_stream(self.from, self.to){ |stream|
|
81
|
+
stream.puts "From: #{self.from}"
|
82
|
+
stream.puts "To: " + self.to.to_a.join(', ')
|
83
|
+
stream.puts "Subject: #{self.subject}"
|
84
|
+
stream.puts
|
85
|
+
stream.puts self.body
|
86
|
+
}
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
# Delivers a simple text email message using four options:
|
91
|
+
#
|
92
|
+
# * to
|
93
|
+
# * from
|
94
|
+
# * subject
|
95
|
+
# * body
|
96
|
+
#
|
97
|
+
# Examples:
|
98
|
+
#
|
99
|
+
# # Send an email to a single user
|
100
|
+
# EZ::Email.deliver(
|
101
|
+
# :to => 'some_user@hotmail.com',
|
102
|
+
# :from => 'me@hotmail.com',
|
103
|
+
# :subject => 'Hi',
|
104
|
+
# :body => 'How are you?'
|
105
|
+
# )
|
106
|
+
#
|
107
|
+
# # Send an email to a multiple users
|
108
|
+
# EZ::Email.deliver(
|
109
|
+
# :to => ['jon@hotmail.com', 'mary@hotmail.com'],
|
110
|
+
# :from => 'me@hotmail.com',
|
111
|
+
# :subject => 'Hi',
|
112
|
+
# :body => 'How are you?'
|
113
|
+
# )
|
114
|
+
#
|
115
|
+
# This is a shortcut for EZ::Email.new + EZ::Email#deliver.
|
116
|
+
#
|
117
|
+
def self.deliver(options)
|
118
|
+
new(options).deliver
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
# Private method that both validates the hash options, and sets
|
124
|
+
# the attribute values.
|
125
|
+
#
|
126
|
+
def validate_options(hash)
|
127
|
+
valid = %w[to from subject body]
|
128
|
+
|
129
|
+
hash.each{ |key, value|
|
130
|
+
key = key.to_s.downcase
|
131
|
+
raise ArgumentError unless valid.include?(key)
|
132
|
+
send("#{key}=", value)
|
133
|
+
}
|
134
|
+
|
135
|
+
if to.nil? || subject.nil? || body.nil?
|
136
|
+
raise ArgumentError, "Missing :to, :subject or :body"
|
140
137
|
end
|
141
|
-
|
138
|
+
end
|
139
|
+
end
|
142
140
|
end
|
data/test/test_ez_email.rb
CHANGED
@@ -3,74 +3,144 @@
|
|
3
3
|
#
|
4
4
|
# Test suite for the EZ::Email library.
|
5
5
|
########################################################################
|
6
|
-
require 'test
|
6
|
+
require 'test-unit'
|
7
7
|
require 'ez/email'
|
8
|
+
require 'socket'
|
9
|
+
require 'etc'
|
8
10
|
|
9
11
|
class TC_EZ_Email < Test::Unit::TestCase
|
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
|
-
|
12
|
+
def self.startup
|
13
|
+
@@host = Socket.gethostname
|
14
|
+
@@login = Etc.getlogin
|
15
|
+
end
|
16
|
+
|
17
|
+
def setup
|
18
|
+
@to = 'foo@some_mail_service.com'
|
19
|
+
@from = 'bar@some_mail_service.com'
|
20
|
+
@subj = 'This is a test'
|
21
|
+
@body = 'How are you?'
|
22
|
+
|
23
|
+
@opts = {:to => @to, :from => @from, :subject => @subj, :body => @body }
|
24
|
+
@email = EZ::Email.new(@opts)
|
25
|
+
end
|
26
|
+
|
27
|
+
test "version is set to expected value" do
|
28
|
+
assert_equal('0.1.3', EZ::Email::VERSION)
|
29
|
+
end
|
30
|
+
|
31
|
+
test "to getter method basic functionality" do
|
32
|
+
assert_respond_to(@email, :to)
|
33
|
+
assert_nothing_raised{ @email.to }
|
34
|
+
assert_not_nil(@email.to)
|
35
|
+
end
|
36
|
+
|
37
|
+
test "to getter method returns expected value" do
|
38
|
+
assert_equal(@to, @email.to)
|
39
|
+
end
|
40
|
+
|
41
|
+
test "to setter method basic functionality" do
|
42
|
+
assert_respond_to(@email, :to=)
|
43
|
+
assert_nothing_raised{ @email.to = 'bogus@some_bogus.com' }
|
44
|
+
end
|
45
|
+
|
46
|
+
test "to setter actually sets value" do
|
47
|
+
@email.to = 'bogus@some_bogus.com'
|
48
|
+
assert_equal(@email.to, 'bogus@some_bogus.com')
|
49
|
+
end
|
50
|
+
|
51
|
+
test "from getter basic functionality" do
|
52
|
+
assert_respond_to(@email, :from)
|
53
|
+
assert_nothing_raised{ @email.from }
|
54
|
+
assert_not_nil(@email.from)
|
55
|
+
end
|
56
|
+
|
57
|
+
test "from setter basic functionality" do
|
58
|
+
assert_respond_to(@email, :from=)
|
59
|
+
assert_nothing_raised{ @email.from = 'bogus@some_bogus.com' }
|
60
|
+
end
|
61
|
+
|
62
|
+
test "from method returns expected value" do
|
63
|
+
assert_equal(@from, @email.from)
|
64
|
+
end
|
65
|
+
|
66
|
+
test "from defaults to username@host if not set in constructor" do
|
67
|
+
@email = EZ::Email.new(:to => 'x', :subject => 'x', :body => 'x')
|
68
|
+
expected = @@login << '@' << @@host
|
69
|
+
assert_equal(expected, @email.from)
|
70
|
+
end
|
71
|
+
|
72
|
+
test "subject getter basic functionality" do
|
73
|
+
assert_respond_to(@email, :subject)
|
74
|
+
assert_nothing_raised{ @email.subject }
|
75
|
+
assert_not_nil(@email.subject)
|
76
|
+
end
|
77
|
+
|
78
|
+
test "subject setter basic functionality" do
|
79
|
+
assert_respond_to(@email, :subject=)
|
80
|
+
assert_nothing_raised{ @email.subject = 'bogus@bogus.com' }
|
81
|
+
end
|
82
|
+
|
83
|
+
test "subject method returns expected value" do
|
84
|
+
assert_equal(@subj, @email.subject)
|
85
|
+
end
|
86
|
+
|
87
|
+
test "body getter basic functionality" do
|
88
|
+
assert_respond_to(@email, :body)
|
89
|
+
assert_nothing_raised{ @email.body }
|
90
|
+
assert_not_nil(@email.body)
|
91
|
+
end
|
92
|
+
|
93
|
+
test "body setter basic functionality" do
|
94
|
+
assert_respond_to(@email, :body=)
|
95
|
+
assert_nothing_raised{ @email.body = "Test" }
|
96
|
+
end
|
97
|
+
|
98
|
+
test "body method returns the expected value" do
|
99
|
+
assert_equal(@body, @email.body)
|
100
|
+
end
|
101
|
+
|
102
|
+
test "mail_host getter basic functionality" do
|
103
|
+
assert_respond_to(EZ::Email, :mail_host)
|
104
|
+
assert_nothing_raised{ EZ::Email.mail_host }
|
105
|
+
assert_not_nil(EZ::Email.mail_host)
|
106
|
+
end
|
107
|
+
|
108
|
+
test "mail_host setter basic functionality" do
|
109
|
+
assert_respond_to(EZ::Email, :mail_host=)
|
110
|
+
assert_nothing_raised{ EZ::Email.mail_host = "Test" }
|
111
|
+
end
|
112
|
+
|
113
|
+
test "mail_port singleton getter basic functionality" do
|
114
|
+
assert_respond_to(EZ::Email, :mail_port)
|
115
|
+
end
|
116
|
+
|
117
|
+
test "mail_port singleton setter basic functionality" do
|
118
|
+
assert_respond_to(EZ::Email, :mail_port=)
|
119
|
+
end
|
120
|
+
|
121
|
+
test "mail_port method returns the expected value" do
|
122
|
+
assert_equal(25, EZ::Email.mail_port)
|
123
|
+
end
|
124
|
+
|
125
|
+
test "deliver singleton method basic functionality" do
|
126
|
+
assert_respond_to(EZ::Email, :deliver)
|
127
|
+
end
|
128
|
+
|
129
|
+
test "passing an invalid option to the constructor raises an error" do
|
130
|
+
assert_raise(ArgumentError){ EZ::Email.send(:new, {:bogus => 77}) }
|
131
|
+
end
|
132
|
+
|
133
|
+
def teardown
|
134
|
+
@to = nil
|
135
|
+
@from = nil
|
136
|
+
@subj = nil
|
137
|
+
@body = nil
|
138
|
+
@opts = nil
|
139
|
+
@email = nil
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.shutdown
|
143
|
+
@@host = nil
|
144
|
+
@@login = nil
|
145
|
+
end
|
76
146
|
end
|
metadata
CHANGED
@@ -1,34 +1,41 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: ez-email
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 2
|
10
|
-
version: 0.1.2
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Daniel Berger
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
date: 2013-01-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: test-unit
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
21
30
|
description: A very simple interface for sending email
|
22
31
|
email: djberg96@gmail.com
|
23
32
|
executables: []
|
24
|
-
|
25
33
|
extensions: []
|
26
|
-
|
27
|
-
extra_rdoc_files:
|
34
|
+
extra_rdoc_files:
|
28
35
|
- README
|
29
36
|
- CHANGES
|
30
37
|
- MANIFEST
|
31
|
-
files:
|
38
|
+
files:
|
32
39
|
- CHANGES
|
33
40
|
- ez-email.gemspec
|
34
41
|
- lib/ez/email.rb
|
@@ -36,38 +43,30 @@ files:
|
|
36
43
|
- Rakefile
|
37
44
|
- README
|
38
45
|
- test/test_ez_email.rb
|
39
|
-
homepage:
|
40
|
-
licenses:
|
46
|
+
homepage: https://github.com/djberg96/ez-email
|
47
|
+
licenses:
|
41
48
|
- Artistic 2.0
|
42
49
|
post_install_message:
|
43
50
|
rdoc_options: []
|
44
|
-
|
45
|
-
require_paths:
|
51
|
+
require_paths:
|
46
52
|
- lib
|
47
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
54
|
none: false
|
49
|
-
requirements:
|
50
|
-
- -
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
|
53
|
-
|
54
|
-
- 0
|
55
|
-
version: "0"
|
56
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
60
|
none: false
|
58
|
-
requirements:
|
59
|
-
- -
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
|
62
|
-
segments:
|
63
|
-
- 0
|
64
|
-
version: "0"
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
65
|
requirements: []
|
66
|
-
|
67
66
|
rubyforge_project: shards
|
68
|
-
rubygems_version: 1.8.
|
67
|
+
rubygems_version: 1.8.24
|
69
68
|
signing_key:
|
70
69
|
specification_version: 3
|
71
70
|
summary: Really easy emails
|
72
|
-
test_files:
|
71
|
+
test_files:
|
73
72
|
- test/test_ez_email.rb
|