jeremyevans-simple_mailer 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/lib/simple_mailer.rb +29 -19
- data/spec/simple_mailer.rb +3 -2
- metadata +1 -1
data/README
CHANGED
@@ -49,7 +49,7 @@ Testing support is probably the main reason to use simple_mailer over
|
|
49
49
|
using net/smtp directly. After you enter test mode, emails you send
|
50
50
|
are available via the emails_sent option:
|
51
51
|
|
52
|
-
SimpleMailer.
|
52
|
+
SimpleMailer.test_mode!
|
53
53
|
SimpleMailer.emails_sent # []
|
54
54
|
SimpleMailer.send_email('from@from.com', 'to@to.com', 'S, 'B')
|
55
55
|
SimpleMailer.emails_sent # [[message, 'from@from.com', 'to@to.com']]
|
data/lib/simple_mailer.rb
CHANGED
@@ -9,18 +9,42 @@ require 'net/smtp'
|
|
9
9
|
# it handles headers and it has a test mode that just records the number of
|
10
10
|
# emails sent instead of actually sending the messages.
|
11
11
|
module SimpleMailer
|
12
|
+
@test_mode = false
|
13
|
+
@emails_sent = []
|
14
|
+
|
12
15
|
extend self
|
13
16
|
|
14
17
|
DEFAULT_SMTP_HOST = 'localhost'.freeze
|
15
18
|
|
16
|
-
# The
|
17
|
-
|
18
|
-
|
19
|
-
|
19
|
+
# The array of emails sent
|
20
|
+
def self.emails_sent
|
21
|
+
@emails_sent
|
22
|
+
end
|
23
|
+
|
24
|
+
# Turn on the test mode. There is no method given to turn it off.
|
25
|
+
# While in test mode, messages will not be sent, but the messages
|
26
|
+
# that would have been sent are available via emails_sent.
|
27
|
+
# This method also resets the emails_sent variable to the empty array.
|
28
|
+
def self.test_mode!
|
29
|
+
@emails_sent.clear
|
30
|
+
@test_mode = true
|
31
|
+
end
|
32
|
+
|
33
|
+
# Whether we are in simple mailer's test mode
|
34
|
+
def self.test_mode?
|
35
|
+
@test_mode
|
36
|
+
end
|
20
37
|
|
21
38
|
# The smtp server to sent email to
|
22
39
|
attr_accessor :smtp_server
|
23
40
|
|
41
|
+
# The emails sent in test mode. Is an array of arrays. Each
|
42
|
+
# element array is a array of three elements, the message, from address,
|
43
|
+
# and to address.
|
44
|
+
def emails_sent
|
45
|
+
SimpleMailer.emails_sent
|
46
|
+
end
|
47
|
+
|
24
48
|
# Formats email message using from address, to address, subject, message,
|
25
49
|
# and header hash. Arguments:
|
26
50
|
# * from - From address for the message
|
@@ -49,21 +73,12 @@ Subject: #{subject}
|
|
49
73
|
END_OF_MESSAGE
|
50
74
|
end
|
51
75
|
|
52
|
-
# Turn on the test mode. There is no method given to turn it off.
|
53
|
-
# While in test mode, messages will not be sent, but the messages
|
54
|
-
# that would have been sent are available via emails_sent.
|
55
|
-
# This method also resets the emails_sent variable to the empty array.
|
56
|
-
def simple_mailer_test_mode!
|
57
|
-
@emails_sent = []
|
58
|
-
@simple_mailer_test_mode = true
|
59
|
-
end
|
60
|
-
|
61
76
|
private
|
62
77
|
|
63
78
|
# If in test mode, call test_mode_send_email with the arguments.
|
64
79
|
# Otherwise, use net/smtp to send the message to the smtp server.
|
65
80
|
def _send_email(msg, from, to)
|
66
|
-
if
|
81
|
+
if SimpleMailer.test_mode?
|
67
82
|
test_mode_send_email(msg, from, to)
|
68
83
|
else
|
69
84
|
Net::SMTP.start(smtp_server || DEFAULT_SMTP_HOST){|s| s.send_message(msg, from, to)}
|
@@ -75,9 +90,4 @@ END_OF_MESSAGE
|
|
75
90
|
def test_mode_send_email(msg, from, to)
|
76
91
|
emails_sent << [msg, from, to]
|
77
92
|
end
|
78
|
-
|
79
|
-
# Whether we are in simple mailer's test mode
|
80
|
-
def simple_mailer_test_mode?
|
81
|
-
@simple_mailer_test_mode
|
82
|
-
end
|
83
93
|
end
|
data/spec/simple_mailer.rb
CHANGED
@@ -72,13 +72,13 @@ END_MESSAGE
|
|
72
72
|
end
|
73
73
|
|
74
74
|
it "should not send emails in test mode" do
|
75
|
-
|
75
|
+
SimpleMailer.test_mode!
|
76
76
|
@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')
|
77
77
|
$message.should == [nil, nil, nil]
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should record emails sent to emails_sent in test mode" do
|
81
|
-
|
81
|
+
SimpleMailer.test_mode!
|
82
82
|
@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')
|
83
83
|
@mailer.emails_sent.should == [[<<END_MESSAGE, 'from@to.com', 'to@from.com']]
|
84
84
|
From: from3@from.com
|
@@ -88,6 +88,7 @@ HeaderKey3: HeaderValue3
|
|
88
88
|
|
89
89
|
Test Body 3
|
90
90
|
END_MESSAGE
|
91
|
+
SimpleMailer.instance_variable_set(:@test_mode, false)
|
91
92
|
end
|
92
93
|
end
|
93
94
|
|