jeremyevans-simple_mailer 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +19 -0
  2. data/README +59 -0
  3. data/lib/simple_mailer.rb +83 -0
  4. data/spec/simple_mailer.rb +102 -0
  5. metadata +59 -0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Jeremy Evans <code@jeremyevans.net>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,59 @@
1
+ = simple_mailer
2
+
3
+ simple_mailer is a very simple email library for ruby, with testing
4
+ support. It just uses ruby's standard net/smtp library to send out
5
+ the emails. Configuration is limited to setting the server that
6
+ the email is sent to (defaults to localhost). Testing support is
7
+ limited to appending the emails that would have been sent to an
8
+ array.
9
+
10
+ simple_mailer can be installed with:
11
+
12
+ sudo gem install jeremyevans-simple_mailer \
13
+ --source=http://gems.github.com
14
+
15
+ Source is available at github:
16
+ http://github.com/jeremyevans/simple_mailer
17
+
18
+ RDoc is available at: http://code.jeremyevans.net/doc/simple_mailer/
19
+
20
+ == Usage
21
+
22
+ There is no required configuration, you can use simple_mailer
23
+ immediately:
24
+
25
+ require 'simple_mailer'
26
+ SimpleMailer.send_email('from@from.com', 'to@to.com', 'Subject',
27
+ 'Body', 'HeaderKey'=>'HeaderValue')
28
+
29
+ SimpleMailer is a module that can be included in other classes:
30
+
31
+ class Mailer
32
+ include SimpleMailer
33
+
34
+ def initialize(subject, body)
35
+ @subject = subject
36
+ @body = body
37
+ self.smtp_server = 'smtp.example.com'
38
+ end
39
+
40
+ def email(from, to)
41
+ send_email(from, to, @subject, @body)
42
+ end
43
+ end
44
+ Mailer.new('Subject', 'Body').email('from@from.com', 'to@to.com')
45
+
46
+ == Testing
47
+
48
+ Testing support is probably the main reason to use simple_mailer over
49
+ using net/smtp directly. After you enter test mode, emails you send
50
+ are available via the emails_sent option:
51
+
52
+ SimpleMailer.simple_mailer_test_mode!
53
+ SimpleMailer.emails_sent # []
54
+ SimpleMailer.send_email('from@from.com', 'to@to.com', 'S, 'B')
55
+ SimpleMailer.emails_sent # [[message, 'from@from.com', 'to@to.com']]
56
+
57
+ == Author
58
+
59
+ Jeremy Evans (code@jeremyevans.net)
@@ -0,0 +1,83 @@
1
+ require 'net/smtp'
2
+
3
+ # SimpleMailer is a very small class that uses net/smtp to sent email messages
4
+ # to a given smtp server (localhost by default). Like the name implies, it
5
+ # is very simple, you provide a from address, to address, subject, body and
6
+ # an optional hash of headers.
7
+ #
8
+ # The main advantage of this over just using Net::SMTP directly is that
9
+ # it handles headers and it has a test mode that just records the number of
10
+ # emails sent instead of actually sending the messages.
11
+ module SimpleMailer
12
+ extend self
13
+
14
+ DEFAULT_SMTP_HOST = 'localhost'.freeze
15
+
16
+ # The emails sent in test mode. Is an array of arrays. Each
17
+ # element array is a array of three elements, the message, from address,
18
+ # and to address.
19
+ attr_reader :emails_sent
20
+
21
+ # The smtp server to sent email to
22
+ attr_accessor :smtp_server
23
+
24
+ # Formats email message using from address, to address, subject, message,
25
+ # and header hash. Arguments:
26
+ # * from - From address for the message
27
+ # * to - To address for the message
28
+ # * subject - Subject of the message
29
+ # * message - Body of the message
30
+ # * headers - Headers for the message. Also, handles the following keys
31
+ # specially:
32
+ # * :smtp_from - the SMTP MAIL FROM address to use. Uses the value of
33
+ # of the from argument by default.
34
+ # * :smtp_to - the SMTP RCPT TO address to use. Uses the value of the
35
+ # to argument by default.
36
+ #
37
+ # The caller is responsible for ensuring that the from, to, subject, and
38
+ # headers do not have an carriage returns or line endings. Otherwise,
39
+ # it's possible to inject arbitrary headers or body content.
40
+ def send_email(from, to, subject, message, headers={})
41
+ smtp_from = headers.delete(:smtp_from) || from
42
+ smtp_to = headers.delete(:smtp_to) || to
43
+ _send_email(<<END_OF_MESSAGE, smtp_from, smtp_to)
44
+ From: #{from}
45
+ To: #{to}
46
+ Subject: #{subject}
47
+ #{headers.sort.map{|k,v| "#{k}: #{v}"}.join("\n")}#{"\n" unless headers.empty?}
48
+ #{message}
49
+ END_OF_MESSAGE
50
+ end
51
+
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
+ private
62
+
63
+ # If in test mode, call test_mode_send_email with the arguments.
64
+ # Otherwise, use net/smtp to send the message to the smtp server.
65
+ def _send_email(msg, from, to)
66
+ if simple_mailer_test_mode?
67
+ test_mode_send_email(msg, from, to)
68
+ else
69
+ Net::SMTP.start(smtp_server || DEFAULT_SMTP_HOST){|s| s.send_message(msg, from, to)}
70
+ end
71
+ end
72
+
73
+ # Handle the message in test mode. By default just adds it to the emails_sent
74
+ # array.
75
+ def test_mode_send_email(msg, from, to)
76
+ emails_sent << [msg, from, to]
77
+ 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
+ end
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env spec
2
+ require File.join(File.dirname(File.dirname(__FILE__)), '/lib/simple_mailer')
3
+ Object.send(:remove_const, :Net)
4
+
5
+ $message = [nil, nil, nil]
6
+ module Net
7
+ class SMTP
8
+ class Mock
9
+ def initialize(host)
10
+ @host = host
11
+ end
12
+ def send_message(msg, from, to)
13
+ $message = [msg, from, to, @host]
14
+ end
15
+ end
16
+ def self.start(host, *args)
17
+ yield(Mock.new(host))
18
+ end
19
+ end
20
+ end
21
+
22
+ shared_examples_for "simple_mailer" do
23
+ before do
24
+ $message = [nil, nil, nil]
25
+ end
26
+
27
+ it "should sent email" do
28
+ @mailer.send_email('from1@from.com', 'to1@to.com', 'Test Subject 1', 'Test Body 1')
29
+ $message.should == [<<END_MESSAGE, 'from1@from.com', 'to1@to.com', 'localhost']
30
+ From: from1@from.com
31
+ To: to1@to.com
32
+ Subject: Test Subject 1
33
+
34
+ Test Body 1
35
+ END_MESSAGE
36
+ end
37
+
38
+ it "should allow the setting of headers" do
39
+ @mailer.send_email('from2@from.com', 'to2@to.com', 'Test Subject 2', 'Test Body 2', 'HeaderKey2'=>'HeaderValue2')
40
+ $message.should == [<<END_MESSAGE, 'from2@from.com', 'to2@to.com', 'localhost']
41
+ From: from2@from.com
42
+ To: to2@to.com
43
+ Subject: Test Subject 2
44
+ HeaderKey2: HeaderValue2
45
+
46
+ Test Body 2
47
+ END_MESSAGE
48
+ end
49
+
50
+ it "should recognize the special :smtp_from and :smtp_to headers" do
51
+ @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')
52
+ $message.should == [<<END_MESSAGE, 'from@to.com', 'to@from.com', 'localhost']
53
+ From: from3@from.com
54
+ To: to3@to.com
55
+ Subject: Test Subject 3
56
+ HeaderKey3: HeaderValue3
57
+
58
+ Test Body 3
59
+ END_MESSAGE
60
+ end
61
+
62
+ it "should allow the setting of smtp server" do
63
+ @mailer.smtp_server = 'blah.com'
64
+ @mailer.send_email('from1@from.com', 'to1@to.com', 'Test Subject 1', 'Test Body 1')
65
+ $message.should == [<<END_MESSAGE, 'from1@from.com', 'to1@to.com', 'blah.com']
66
+ From: from1@from.com
67
+ To: to1@to.com
68
+ Subject: Test Subject 1
69
+
70
+ Test Body 1
71
+ END_MESSAGE
72
+ end
73
+
74
+ it "should not send emails in test mode" do
75
+ @mailer.simple_mailer_test_mode!
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
+ $message.should == [nil, nil, nil]
78
+ end
79
+
80
+ it "should record emails sent to emails_sent in test mode" do
81
+ @mailer.simple_mailer_test_mode!
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
+ @mailer.emails_sent.should == [[<<END_MESSAGE, 'from@to.com', 'to@from.com']]
84
+ From: from3@from.com
85
+ To: to3@to.com
86
+ Subject: Test Subject 3
87
+ HeaderKey3: HeaderValue3
88
+
89
+ Test Body 3
90
+ END_MESSAGE
91
+ end
92
+ end
93
+
94
+ describe "SimpleMailer module itself" do
95
+ before{@mailer = SimpleMailer}
96
+ it_should_behave_like "simple_mailer"
97
+ end
98
+
99
+ describe "Class including SimpleMailer" do
100
+ before{@mailer = Class.new{include SimpleMailer}.new}
101
+ it_should_behave_like "simple_mailer"
102
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jeremyevans-simple_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: code@jeremyevans.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ files:
25
+ - README
26
+ - LICENSE
27
+ - lib/simple_mailer.rb
28
+ - spec/simple_mailer.rb
29
+ has_rdoc: true
30
+ homepage:
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --inline-source
34
+ - --line-numbers
35
+ - README
36
+ - lib
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.2.0
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: Simple email library with testing support
58
+ test_files: []
59
+