simple_mailer 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README +13 -0
- data/lib/simple_mailer.rb +21 -1
- data/spec/simple_mailer.rb +83 -23
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a983756906bc274eb4c8a9f870af6cb17375ff4d
|
4
|
+
data.tar.gz: f0de651d59f07f38c9a3f885adc419f7a00bb2c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 682559a504a4594ad1771b9349378f7b4e1733b70adab4a95370daf22105e0b34a8cde75381ecb35fcc313ee530e2b9fa1584ce5b25185baf647ecc798625d10
|
7
|
+
data.tar.gz: 29d1c3d59bed320f8fb1055bf679892f1cbf6f407ed6d6c60601e20d77d12956682ed69557e410eeabcce6dfdda758c029ef8117e5f78bac00eae8b9e55f7f8d
|
data/README
CHANGED
@@ -55,6 +55,19 @@ There are four special headers that simple_mailer processes:
|
|
55
55
|
|
56
56
|
All other headers are used verbatim in the message.
|
57
57
|
|
58
|
+
== Configuration
|
59
|
+
|
60
|
+
You can pass in options just like with the Mail gem.
|
61
|
+
|
62
|
+
SimpleMailer.smtp_settings.update(
|
63
|
+
:address => "smtp.gmail.com",
|
64
|
+
:port => 587,
|
65
|
+
:domain => "localhost",
|
66
|
+
:user_name => "bob",
|
67
|
+
:password => "secret",
|
68
|
+
:authentication => :plain,
|
69
|
+
)
|
70
|
+
|
58
71
|
== Testing
|
59
72
|
|
60
73
|
Testing support is probably the main reason to use simple_mailer over
|
data/lib/simple_mailer.rb
CHANGED
@@ -15,6 +15,7 @@ module SimpleMailer
|
|
15
15
|
extend self
|
16
16
|
|
17
17
|
DEFAULT_SMTP_HOST = 'localhost'.freeze
|
18
|
+
DEFAULT_SMTP_DOMAIN = 'localhost'.freeze
|
18
19
|
|
19
20
|
# The array of emails sent
|
20
21
|
def self.emails_sent
|
@@ -37,6 +38,16 @@ module SimpleMailer
|
|
37
38
|
|
38
39
|
# The smtp server to sent email to
|
39
40
|
attr_accessor :smtp_server
|
41
|
+
|
42
|
+
# Custom SMTP configuration for any service (taken from the "mail" gem)
|
43
|
+
def smtp_settings
|
44
|
+
@smtp_settings ||= {
|
45
|
+
:address => smtp_server || DEFAULT_SMTP_HOST,
|
46
|
+
:port => 25,
|
47
|
+
:enable_starttls_auto => true,
|
48
|
+
:domain => DEFAULT_SMTP_DOMAIN,
|
49
|
+
}
|
50
|
+
end
|
40
51
|
|
41
52
|
# The emails sent in test mode. Is an array of arrays. Each
|
42
53
|
# element array is a array of three elements, the message, from address,
|
@@ -91,7 +102,9 @@ END_OF_MESSAGE
|
|
91
102
|
if SimpleMailer.test_mode?
|
92
103
|
test_mode_send_email(msg, from, to)
|
93
104
|
else
|
94
|
-
|
105
|
+
smtp.start(*smtp_settings.values_at(:domain, :user_name, :password, :authentication)) do |s|
|
106
|
+
s.send_message(msg, from, to)
|
107
|
+
end
|
95
108
|
end
|
96
109
|
end
|
97
110
|
|
@@ -100,4 +113,11 @@ END_OF_MESSAGE
|
|
100
113
|
def test_mode_send_email(msg, from, to)
|
101
114
|
emails_sent << [msg, from, to]
|
102
115
|
end
|
116
|
+
|
117
|
+
def smtp
|
118
|
+
Net::SMTP.new(*smtp_settings.values_at(:address, :port)).tap do |smtp|
|
119
|
+
smtp.enable_starttls_auto unless smtp_settings[:enable_starttls_auto] == false
|
120
|
+
smtp.enable_tls if smtp_settings[:tls] || smtp_settings[:ssl]
|
121
|
+
end
|
122
|
+
end
|
103
123
|
end
|
data/spec/simple_mailer.rb
CHANGED
@@ -1,32 +1,54 @@
|
|
1
|
-
#!/usr/bin/env spec
|
2
1
|
require File.join(File.dirname(File.dirname(__FILE__)), '/lib/simple_mailer')
|
3
2
|
Object.send(:remove_const, :Net)
|
4
3
|
|
4
|
+
gem 'minitest'
|
5
|
+
require 'minitest/autorun'
|
6
|
+
|
5
7
|
$message = [nil, nil, nil]
|
6
8
|
module Net
|
7
9
|
class SMTP
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
attr_reader :address, :port, :domain, :user_name, :password, :authentication,
|
11
|
+
:starttls_auto, :tls
|
12
|
+
|
13
|
+
def initialize(address, port = nil)
|
14
|
+
@address, @port = address, port
|
15
|
+
end
|
16
|
+
|
17
|
+
def enable_starttls_auto
|
18
|
+
@starttls_auto = true
|
19
|
+
end
|
20
|
+
|
21
|
+
def enable_tls
|
22
|
+
@tls = true
|
15
23
|
end
|
16
|
-
|
17
|
-
|
24
|
+
|
25
|
+
def start(domain = nil, user_name = nil, password = nil, authentication = nil)
|
26
|
+
@domain, @user_name, @password, @authentication = domain, user_name, password, authentication
|
27
|
+
yield self
|
28
|
+
end
|
29
|
+
|
30
|
+
def send_message(msg, from, to)
|
31
|
+
$message = [msg, from, to, @address]
|
32
|
+
self
|
18
33
|
end
|
19
34
|
end
|
20
35
|
end
|
21
36
|
|
22
|
-
|
37
|
+
module SimpleMailerSpecs
|
38
|
+
extend Minitest::Spec::DSL
|
39
|
+
|
23
40
|
before do
|
24
41
|
$message = [nil, nil, nil]
|
25
42
|
end
|
43
|
+
after do
|
44
|
+
@mailer.smtp_server = nil
|
45
|
+
@mailer.instance_variable_set(:@smtp_settings, nil)
|
46
|
+
SimpleMailer.instance_variable_set(:@test_mode, false)
|
47
|
+
end
|
26
48
|
|
27
|
-
it "should
|
49
|
+
it "should send email" do
|
28
50
|
@mailer.send_email('from1@from.com', 'to1@to.com', 'Test Subject 1', 'Test Body 1')
|
29
|
-
$message.
|
51
|
+
$message.must_equal [<<END_MESSAGE, 'from1@from.com', 'to1@to.com', 'localhost']
|
30
52
|
From: from1@from.com
|
31
53
|
To: to1@to.com
|
32
54
|
Subject: Test Subject 1
|
@@ -37,7 +59,7 @@ END_MESSAGE
|
|
37
59
|
|
38
60
|
it "should allow the setting of headers" do
|
39
61
|
@mailer.send_email('from2@from.com', 'to2@to.com', 'Test Subject 2', 'Test Body 2', 'HeaderKey2'=>'HeaderValue2')
|
40
|
-
$message.
|
62
|
+
$message.must_equal [<<END_MESSAGE, 'from2@from.com', 'to2@to.com', 'localhost']
|
41
63
|
From: from2@from.com
|
42
64
|
To: to2@to.com
|
43
65
|
Subject: Test Subject 2
|
@@ -49,7 +71,7 @@ END_MESSAGE
|
|
49
71
|
|
50
72
|
it "should recognize the special :cc header" do
|
51
73
|
@mailer.send_email('from3@from.com', 'to3@to.com', 'Test Subject 3', 'Test Body 3', :cc=>'cc3@to.com')
|
52
|
-
$message.
|
74
|
+
$message.must_equal [<<END_MESSAGE, 'from3@from.com', ['to3@to.com', 'cc3@to.com'], 'localhost']
|
53
75
|
From: from3@from.com
|
54
76
|
To: to3@to.com
|
55
77
|
Subject: Test Subject 3
|
@@ -61,7 +83,7 @@ END_MESSAGE
|
|
61
83
|
|
62
84
|
it "should recognize the special :bcc header" do
|
63
85
|
@mailer.send_email('from3@from.com', 'to3@to.com', 'Test Subject 3', 'Test Body 3', :bcc=>'cc3@to.com')
|
64
|
-
$message.
|
86
|
+
$message.must_equal [<<END_MESSAGE, 'from3@from.com', ['to3@to.com', 'cc3@to.com'], 'localhost']
|
65
87
|
From: from3@from.com
|
66
88
|
To: to3@to.com
|
67
89
|
Subject: Test Subject 3
|
@@ -72,7 +94,7 @@ END_MESSAGE
|
|
72
94
|
|
73
95
|
it "should recognize the special :smtp_from and :smtp_to headers" do
|
74
96
|
@mailer.send_email('from3@from.com', 'to3@to.com', 'Test Subject 3', 'Test Body 3', 'HeaderKey3'=>'HeaderValue3', :smtp_from=>'from@to.com', :smtp_to=>'to@from.com')
|
75
|
-
$message.
|
97
|
+
$message.must_equal [<<END_MESSAGE, 'from@to.com', 'to@from.com', 'localhost']
|
76
98
|
From: from3@from.com
|
77
99
|
To: to3@to.com
|
78
100
|
Subject: Test Subject 3
|
@@ -85,13 +107,13 @@ END_MESSAGE
|
|
85
107
|
it "should not modify input hash" do
|
86
108
|
h = {:smtp_to=>'to@to.com'}
|
87
109
|
@mailer.send_email('from3@from.com', 'to3@to.com', 'Test Subject 3', 'Test Body 3', h)
|
88
|
-
h.
|
110
|
+
h.must_equal(:smtp_to=>'to@to.com')
|
89
111
|
end
|
90
112
|
|
91
113
|
it "should allow the setting of smtp server" do
|
92
114
|
@mailer.smtp_server = 'blah.com'
|
93
115
|
@mailer.send_email('from1@from.com', 'to1@to.com', 'Test Subject 1', 'Test Body 1')
|
94
|
-
$message.
|
116
|
+
$message.must_equal [<<END_MESSAGE, 'from1@from.com', 'to1@to.com', 'blah.com']
|
95
117
|
From: from1@from.com
|
96
118
|
To: to1@to.com
|
97
119
|
Subject: Test Subject 1
|
@@ -103,13 +125,13 @@ END_MESSAGE
|
|
103
125
|
it "should not send emails in test mode" do
|
104
126
|
SimpleMailer.test_mode!
|
105
127
|
@mailer.send_email('from3@from.com', 'to3@to.com', 'Test Subject 3', 'Test Body 3', 'HeaderKey3'=>'HeaderValue3', :smtp_from=>'from@to.com', :smtp_to=>'to@from.com')
|
106
|
-
$message.
|
128
|
+
$message.must_equal [nil, nil, nil]
|
107
129
|
end
|
108
130
|
|
109
131
|
it "should record emails sent to emails_sent in test mode" do
|
110
132
|
SimpleMailer.test_mode!
|
111
133
|
@mailer.send_email('from3@from.com', 'to3@to.com', 'Test Subject 3', 'Test Body 3', 'HeaderKey3'=>'HeaderValue3', :smtp_from=>'from@to.com', :smtp_to=>'to@from.com')
|
112
|
-
@mailer.emails_sent.
|
134
|
+
@mailer.emails_sent.must_equal [[<<END_MESSAGE, 'from@to.com', 'to@from.com']]
|
113
135
|
From: from3@from.com
|
114
136
|
To: to3@to.com
|
115
137
|
Subject: Test Subject 3
|
@@ -119,14 +141,52 @@ Test Body 3
|
|
119
141
|
END_MESSAGE
|
120
142
|
SimpleMailer.instance_variable_set(:@test_mode, false)
|
121
143
|
end
|
144
|
+
|
145
|
+
it "should give proper default smtp settings" do
|
146
|
+
@mailer.send(:smtp).address.must_equal 'localhost'
|
147
|
+
@mailer.send(:smtp).port.must_equal 25
|
148
|
+
@mailer.send(:smtp).starttls_auto.must_equal true
|
149
|
+
|
150
|
+
smtp = @mailer.send_email('from@from.com', 'to@to.com', 'Test Subject', 'Test Body')
|
151
|
+
smtp.domain.must_equal 'localhost'
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should take smtp_server as the address" do
|
155
|
+
@mailer.smtp_server = 'blah.com'
|
156
|
+
@mailer.send(:smtp).address.must_equal 'blah.com'
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should have configurable smtp settings" do
|
160
|
+
@mailer.smtp_settings.update(
|
161
|
+
:address => 'smtp.gmail.com',
|
162
|
+
:port => 587,
|
163
|
+
:user_name => 'bob',
|
164
|
+
:password => 'secret',
|
165
|
+
:domain => 'mydomain.com',
|
166
|
+
:authentication => :plain,
|
167
|
+
:enable_starttls_auto => false,
|
168
|
+
:tls => true
|
169
|
+
)
|
170
|
+
|
171
|
+
@mailer.send(:smtp).address.must_equal 'smtp.gmail.com'
|
172
|
+
@mailer.send(:smtp).port.must_equal 587
|
173
|
+
@mailer.send(:smtp).starttls_auto.must_equal nil
|
174
|
+
@mailer.send(:smtp).tls.must_equal true
|
175
|
+
|
176
|
+
smtp = @mailer.send_email('from@from.com', 'to@to.com', 'Test Subject', 'Test Body')
|
177
|
+
smtp.domain.must_equal 'mydomain.com'
|
178
|
+
smtp.user_name.must_equal 'bob'
|
179
|
+
smtp.password.must_equal 'secret'
|
180
|
+
smtp.authentication.must_equal :plain
|
181
|
+
end
|
122
182
|
end
|
123
183
|
|
124
184
|
describe "SimpleMailer module itself" do
|
125
185
|
before{@mailer = SimpleMailer}
|
126
|
-
|
186
|
+
include SimpleMailerSpecs
|
127
187
|
end
|
128
188
|
|
129
189
|
describe "Class including SimpleMailer" do
|
130
190
|
before{@mailer = Class.new{include SimpleMailer}.new}
|
131
|
-
|
191
|
+
include SimpleMailerSpecs
|
132
192
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_mailer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Evans
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: code@jeremyevans.net
|
@@ -17,8 +17,8 @@ extensions: []
|
|
17
17
|
extra_rdoc_files:
|
18
18
|
- MIT-LICENSE
|
19
19
|
files:
|
20
|
-
- README
|
21
20
|
- MIT-LICENSE
|
21
|
+
- README
|
22
22
|
- lib/simple_mailer.rb
|
23
23
|
- spec/simple_mailer.rb
|
24
24
|
homepage:
|
@@ -26,25 +26,25 @@ licenses: []
|
|
26
26
|
metadata: {}
|
27
27
|
post_install_message:
|
28
28
|
rdoc_options:
|
29
|
-
- --inline-source
|
30
|
-
- --line-numbers
|
29
|
+
- "--inline-source"
|
30
|
+
- "--line-numbers"
|
31
31
|
- README
|
32
32
|
- lib
|
33
33
|
require_paths:
|
34
34
|
- lib
|
35
35
|
required_ruby_version: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- -
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '0'
|
40
40
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
41
|
requirements:
|
42
|
-
- -
|
42
|
+
- - ">="
|
43
43
|
- !ruby/object:Gem::Version
|
44
44
|
version: '0'
|
45
45
|
requirements: []
|
46
46
|
rubyforge_project:
|
47
|
-
rubygems_version: 2.
|
47
|
+
rubygems_version: 2.4.5
|
48
48
|
signing_key:
|
49
49
|
specification_version: 4
|
50
50
|
summary: Simple email library with testing support
|