simple_mailer 1.1.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 +93 -0
  4. data/spec/simple_mailer.rb +103 -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.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,93 @@
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
+ @test_mode = false
13
+ @emails_sent = []
14
+
15
+ extend self
16
+
17
+ DEFAULT_SMTP_HOST = 'localhost'.freeze
18
+
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
37
+
38
+ # The smtp server to sent email to
39
+ attr_accessor :smtp_server
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
+
48
+ # Formats email message using from address, to address, subject, message,
49
+ # and header hash. Arguments:
50
+ # * from - From address for the message
51
+ # * to - To address for the message
52
+ # * subject - Subject of the message
53
+ # * message - Body of the message
54
+ # * headers - Headers for the message. Also, handles the following keys
55
+ # specially:
56
+ # * :smtp_from - the SMTP MAIL FROM address to use. Uses the value of
57
+ # of the from argument by default.
58
+ # * :smtp_to - the SMTP RCPT TO address to use. Uses the value of the
59
+ # to argument by default.
60
+ #
61
+ # The caller is responsible for ensuring that the from, to, subject, and
62
+ # headers do not have an carriage returns or line endings. Otherwise,
63
+ # it's possible to inject arbitrary headers or body content.
64
+ def send_email(from, to, subject, message, headers={})
65
+ smtp_from = headers.delete(:smtp_from) || from
66
+ smtp_to = headers.delete(:smtp_to) || to
67
+ _send_email(<<END_OF_MESSAGE, smtp_from, smtp_to)
68
+ From: #{from}
69
+ To: #{to}
70
+ Subject: #{subject}
71
+ #{headers.sort.map{|k,v| "#{k}: #{v}"}.join("\n")}#{"\n" unless headers.empty?}
72
+ #{message}
73
+ END_OF_MESSAGE
74
+ end
75
+
76
+ private
77
+
78
+ # If in test mode, call test_mode_send_email with the arguments.
79
+ # Otherwise, use net/smtp to send the message to the smtp server.
80
+ def _send_email(msg, from, to)
81
+ if SimpleMailer.test_mode?
82
+ test_mode_send_email(msg, from, to)
83
+ else
84
+ Net::SMTP.start(smtp_server || DEFAULT_SMTP_HOST){|s| s.send_message(msg, from, to)}
85
+ end
86
+ end
87
+
88
+ # Handle the message in test mode. By default just adds it to the emails_sent
89
+ # array.
90
+ def test_mode_send_email(msg, from, to)
91
+ emails_sent << [msg, from, to]
92
+ end
93
+ end
@@ -0,0 +1,103 @@
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
+ SimpleMailer.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
+ SimpleMailer.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
+ SimpleMailer.instance_variable_set(:@test_mode, false)
92
+ end
93
+ end
94
+
95
+ describe "SimpleMailer module itself" do
96
+ before{@mailer = SimpleMailer}
97
+ it_should_behave_like "simple_mailer"
98
+ end
99
+
100
+ describe "Class including SimpleMailer" do
101
+ before{@mailer = Class.new{include SimpleMailer}.new}
102
+ it_should_behave_like "simple_mailer"
103
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-08 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.3.1
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: Simple email library with testing support
58
+ test_files: []
59
+