kusor-sinatra_mailer 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.markdown +87 -0
- data/Rakefile +19 -0
- data/lib/sinatra/mailer.rb +123 -0
- data/sinatra_mailer.gemspec +30 -0
- data/test/test_classic_application.rb +66 -0
- data/test/test_modular_application.rb +68 -0
- metadata +89 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Nicolás Sanguinetti
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
Sinatra::Mailer
|
2
|
+
===============
|
3
|
+
|
4
|
+
Adds an `email` method to your email handlers, that receives a hash of values
|
5
|
+
to create your email.
|
6
|
+
|
7
|
+
For example:
|
8
|
+
|
9
|
+
post "/signup" do
|
10
|
+
# sign up the user, and then:
|
11
|
+
email :to => @user.email,
|
12
|
+
:from => "awesomeness@example.com",
|
13
|
+
:subject => "Welcome to Awesomeness!",
|
14
|
+
:body => haml(:some_template)
|
15
|
+
end
|
16
|
+
|
17
|
+
Usage
|
18
|
+
=====
|
19
|
+
|
20
|
+
|
21
|
+
Classic top-level Sinatra style applications
|
22
|
+
--------------------------------------------
|
23
|
+
|
24
|
+
require 'sinatra/mailer'
|
25
|
+
|
26
|
+
Modular Sinatra style applications
|
27
|
+
--------------------------------------------
|
28
|
+
|
29
|
+
require 'sinatra/mailer'
|
30
|
+
|
31
|
+
class MyModularApp < Sinatra::Base
|
32
|
+
register Sinatra::Mailer
|
33
|
+
end
|
34
|
+
|
35
|
+
Configuration
|
36
|
+
=============
|
37
|
+
|
38
|
+
This plugin is very dirty yet :) Since it's just a port to Sinatra of
|
39
|
+
[Merb::Mailer][merb-mailer]. So the configuration is not Sinatra-y, yet.
|
40
|
+
But we'll get to that.
|
41
|
+
|
42
|
+
Using SMTP
|
43
|
+
----------
|
44
|
+
|
45
|
+
Sinatra::Mailer.config = {
|
46
|
+
:host => 'smtp.yourserver.com',
|
47
|
+
:port => '25',
|
48
|
+
:user => 'user',
|
49
|
+
:pass => 'pass',
|
50
|
+
:auth => :plain # :plain, :login, :cram_md5, the default is no auth
|
51
|
+
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
52
|
+
}
|
53
|
+
|
54
|
+
Using Gmail SMTP
|
55
|
+
----------------
|
56
|
+
|
57
|
+
You need [smtp-tls][], a gem that improves `net/smtp` to add support for secure
|
58
|
+
servers such as Gmail.
|
59
|
+
|
60
|
+
require "smtp-tls"
|
61
|
+
|
62
|
+
Sinatra::Mailer.config = {
|
63
|
+
:host => 'smtp.gmail.com',
|
64
|
+
:port => '587',
|
65
|
+
:user => 'user@gmail.com',
|
66
|
+
:pass => 'pass',
|
67
|
+
:auth => :plain
|
68
|
+
}
|
69
|
+
|
70
|
+
Make sure that when you call your `email` method you pass the `:text` option
|
71
|
+
and not `:body`.
|
72
|
+
|
73
|
+
Using sendmail
|
74
|
+
--------------
|
75
|
+
|
76
|
+
Sinatra::Mailer.config = {:sendmail_path => '/somewhere/odd', :arguments => '-i -t'}
|
77
|
+
Sinatra::Mailer.delivery_method = :sendmail
|
78
|
+
|
79
|
+
Credits
|
80
|
+
=======
|
81
|
+
|
82
|
+
This has been blatantly adapted from [Merb::Mailer][merb-mailer], so all credit
|
83
|
+
is theirs, I just ported it to [Sinatra][Sinatra].
|
84
|
+
|
85
|
+
[merb-mailer]: http://github.com/wycats/merb-more/tree/master/merb-mailer
|
86
|
+
[smtp-tls]: http://github.com/ambethia/smtp-tls/tree/master
|
87
|
+
[Sinatra]: http://sinatrarb.com
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rake/gempackagetask'
|
2
|
+
require 'rubygems/specification'
|
3
|
+
|
4
|
+
$spec = eval(File.read('sinatra_mailer.gemspec'))
|
5
|
+
|
6
|
+
Rake::GemPackageTask.new($spec) do |pkg|
|
7
|
+
pkg.need_zip = true
|
8
|
+
pkg.need_tar = true
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rake/testtask'
|
12
|
+
|
13
|
+
Rake::TestTask.new('test') do |t|
|
14
|
+
t.libs = [File.expand_path('lib')]
|
15
|
+
t.pattern = 'test/test_*.rb'
|
16
|
+
t.warning = true
|
17
|
+
end
|
18
|
+
|
19
|
+
Rake::Task['test'].comment = 'Run tests for sinatra_mailer'
|
@@ -0,0 +1,123 @@
|
|
1
|
+
# Shamelssly stolen from Merb::Mailer
|
2
|
+
# http://merbivore.com
|
3
|
+
|
4
|
+
require 'net/smtp'
|
5
|
+
require 'rubygems'
|
6
|
+
require 'mailfactory'
|
7
|
+
|
8
|
+
class MailFactory
|
9
|
+
attr_reader :html, :text
|
10
|
+
end
|
11
|
+
|
12
|
+
module Sinatra
|
13
|
+
# You'll need a simple config like this in your configure block if you want
|
14
|
+
# to actually send mail:
|
15
|
+
#
|
16
|
+
# Sinatra::Mailer.config = {
|
17
|
+
# :host => 'smtp.yourserver.com',
|
18
|
+
# :port => '25',
|
19
|
+
# :user => 'user',
|
20
|
+
# :pass => 'pass',
|
21
|
+
# :auth => :plain # :plain, :login, :cram_md5, the default is no auth
|
22
|
+
# :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
23
|
+
# }
|
24
|
+
#
|
25
|
+
# or
|
26
|
+
#
|
27
|
+
# Sinatra::Mailer.config = {:sendmail_path => '/somewhere/odd'}
|
28
|
+
# Sinatra::Mailer.delivery_method = :sendmail
|
29
|
+
#
|
30
|
+
# From your event handlers then, you can just call the 'email' method to deliver an email:
|
31
|
+
#
|
32
|
+
# email :to => 'foo@bar.com',
|
33
|
+
# :from => 'bar@foo.com',
|
34
|
+
# :subject => 'Welcome to whatever!',
|
35
|
+
# :body => haml(:sometemplate)
|
36
|
+
#
|
37
|
+
module Mailer
|
38
|
+
class << self
|
39
|
+
attr_accessor :config, :delivery_method
|
40
|
+
|
41
|
+
@@deliveries = []
|
42
|
+
|
43
|
+
def deliveries
|
44
|
+
@@deliveries
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module Helpers
|
49
|
+
def email(mail_options={})
|
50
|
+
Email.new(mail_options).deliver!
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.registered(app)
|
55
|
+
app.helpers Sinatra::Mailer::Helpers
|
56
|
+
end
|
57
|
+
|
58
|
+
class Email
|
59
|
+
attr_accessor :mail, :config
|
60
|
+
|
61
|
+
# Sends the mail using sendmail.
|
62
|
+
def sendmail
|
63
|
+
sendmail_arguments = config[:sendmail_arguments] || "#{@mail.to}"
|
64
|
+
sendmail = IO.popen("#{config[:sendmail_path]} #{sendmail_arguments}", 'w+')
|
65
|
+
sendmail.puts @mail.to_s
|
66
|
+
sendmail.close
|
67
|
+
end
|
68
|
+
|
69
|
+
# Sends the mail using SMTP.
|
70
|
+
def net_smtp
|
71
|
+
Net::SMTP.start(config[:host], config[:port].to_i, config[:domain],
|
72
|
+
config[:user], config[:pass], config[:auth]) { |smtp|
|
73
|
+
smtp.send_message(@mail.to_s, @mail.from.first, @mail.to.to_s.split(/[,;]/))
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
# Delivers the mail with the specified delivery method, defaulting to
|
78
|
+
# net_smtp.
|
79
|
+
def deliver!
|
80
|
+
send(Mailer.delivery_method || :net_smtp)
|
81
|
+
end
|
82
|
+
|
83
|
+
# ==== Parameters
|
84
|
+
# file_or_files<File, Array[File]>:: File(s) to attach.
|
85
|
+
# filename<String>::
|
86
|
+
# type<~to_s>::
|
87
|
+
# The attachment MIME type. If left out, it will be determined from
|
88
|
+
# file_or_files.
|
89
|
+
# headers<String, Array>:: Additional attachment headers.
|
90
|
+
#
|
91
|
+
# ==== Raises
|
92
|
+
# ArgumentError::
|
93
|
+
# file_or_files was not a File or an Array of File instances.
|
94
|
+
def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
|
95
|
+
type = nil, headers = nil)
|
96
|
+
if file_or_files.is_a?(Array)
|
97
|
+
file_or_files.each {|k,v| @mail.add_attachment_as k, *v}
|
98
|
+
else
|
99
|
+
raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" if !file_or_files.is_a?(File)
|
100
|
+
@mail.add_attachment_as(file_or_files, filename, type, headers)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# ==== Parameters
|
105
|
+
# o<Hash{~to_s => Object}>:: Configuration commands to send to MailFactory.
|
106
|
+
def initialize(o={})
|
107
|
+
self.config = Mailer.config || {:sendmail_path => '/usr/sbin/sendmail'}
|
108
|
+
o[:rawhtml] = o.delete(:html)
|
109
|
+
m = MailFactory.new()
|
110
|
+
o.each { |k,v| m.send "#{k}=", v }
|
111
|
+
@mail = m
|
112
|
+
end
|
113
|
+
|
114
|
+
# Tests mail sending by adding the mail to deliveries.
|
115
|
+
def test_send
|
116
|
+
Sinatra::Mailer.deliveries << @mail
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
register Mailer
|
123
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "sinatra_mailer"
|
3
|
+
s.version = "0.9.3"
|
4
|
+
s.date = "2009-05-26"
|
5
|
+
s.summary = "Send emails from Sinatra like Merb::Mailer"
|
6
|
+
s.email = "kusorbox@gmail.com"
|
7
|
+
s.homepage = "http://github.com/kusor/sinatra-mailer/tree/master"
|
8
|
+
s.description = "Sinatra::Mailer extension extracted from Merb::Mailer by Nicolás Sanguinetti."
|
9
|
+
s.authors = ["Nicolás Sanguinetti", "Pedro P. Candel"]
|
10
|
+
|
11
|
+
s.has_rdoc = false
|
12
|
+
s.rdoc_options = ["--main", "README.markdown"]
|
13
|
+
s.extra_rdoc_files = ["README.markdown"]
|
14
|
+
|
15
|
+
s.files = %w[
|
16
|
+
LICENSE
|
17
|
+
README.markdown
|
18
|
+
sinatra_mailer.gemspec
|
19
|
+
Rakefile
|
20
|
+
lib/sinatra/mailer.rb
|
21
|
+
]
|
22
|
+
s.test_files = %w[
|
23
|
+
test/test_classic_application.rb
|
24
|
+
test/test_modular_application.rb
|
25
|
+
]
|
26
|
+
|
27
|
+
s.add_dependency 'sinatra', '>= 0.9.2'
|
28
|
+
s.add_dependency 'mailfactory', '>= 1.4.0'
|
29
|
+
s.add_development_dependency 'rack-test', '>= 0.3.0'
|
30
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'sinatra', '>= 0.9.1'
|
5
|
+
require 'sinatra'
|
6
|
+
|
7
|
+
require 'sinatra/mailer'
|
8
|
+
|
9
|
+
set :environment, :test
|
10
|
+
|
11
|
+
configure do
|
12
|
+
Sinatra::Mailer.config = {:sendmail_path => '/usr/sbin/sendmail'}
|
13
|
+
Sinatra::Mailer.delivery_method = :test_send
|
14
|
+
end
|
15
|
+
|
16
|
+
get '/' do
|
17
|
+
'Classic sample application'
|
18
|
+
end
|
19
|
+
|
20
|
+
get '/defined' do
|
21
|
+
sent = email(
|
22
|
+
:to => 'foo@example.com',
|
23
|
+
:from => 'bar@example.com',
|
24
|
+
:subject => 'Test Mail',
|
25
|
+
:body => 'Plain text mail'
|
26
|
+
)
|
27
|
+
'Email sent!'
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
require "test/unit"
|
32
|
+
require "rack/test"
|
33
|
+
|
34
|
+
begin
|
35
|
+
require "redgreen"
|
36
|
+
rescue LoadError
|
37
|
+
end
|
38
|
+
|
39
|
+
class TestClassicApplication < Test::Unit::TestCase
|
40
|
+
|
41
|
+
include Rack::Test::Methods
|
42
|
+
|
43
|
+
def app
|
44
|
+
Sinatra::Application
|
45
|
+
end
|
46
|
+
|
47
|
+
def teardown
|
48
|
+
Sinatra::Mailer.deliveries.clear
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_home_ok
|
52
|
+
get '/'
|
53
|
+
assert_equal 200, last_response.status
|
54
|
+
assert last_response.body.length > 0
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_helper_method
|
58
|
+
assert defined?(:email)
|
59
|
+
get '/defined'
|
60
|
+
assert_equal 200, last_response.status
|
61
|
+
assert_equal 1, Sinatra::Mailer.deliveries.size
|
62
|
+
delivery = Sinatra::Mailer.deliveries.last
|
63
|
+
assert delivery.kind_of?(MailFactory)
|
64
|
+
assert_equal ['foo@example.com'], delivery.get_header('to')
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'sinatra', '>= 0.9.1'
|
5
|
+
require 'sinatra/base'
|
6
|
+
|
7
|
+
require 'sinatra/mailer'
|
8
|
+
|
9
|
+
class ModularAppSample < Sinatra::Base
|
10
|
+
|
11
|
+
register Sinatra::Mailer
|
12
|
+
|
13
|
+
configure do
|
14
|
+
Sinatra::Mailer.config = {:sendmail_path => '/usr/sbin/sendmail'}
|
15
|
+
Sinatra::Mailer.delivery_method = :test_send
|
16
|
+
end
|
17
|
+
|
18
|
+
get '/' do
|
19
|
+
'Modular sample application'
|
20
|
+
end
|
21
|
+
|
22
|
+
get '/defined' do
|
23
|
+
sent = email(
|
24
|
+
:to => 'foo@example.com',
|
25
|
+
:from => 'bar@example.com',
|
26
|
+
:subject => 'Test Mail',
|
27
|
+
:body => 'Plain text mail'
|
28
|
+
)
|
29
|
+
'Email sent!'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
require "test/unit"
|
34
|
+
require "rack/test"
|
35
|
+
|
36
|
+
begin
|
37
|
+
require "redgreen"
|
38
|
+
rescue LoadError
|
39
|
+
end
|
40
|
+
|
41
|
+
class TestModularApplication < Test::Unit::TestCase
|
42
|
+
|
43
|
+
include Rack::Test::Methods
|
44
|
+
|
45
|
+
def app
|
46
|
+
ModularAppSample.new
|
47
|
+
end
|
48
|
+
|
49
|
+
def teardown
|
50
|
+
Sinatra::Mailer.deliveries.clear
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_home_ok
|
54
|
+
get '/'
|
55
|
+
assert_equal 200, last_response.status
|
56
|
+
assert last_response.body.length > 0
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_helper_method
|
60
|
+
assert defined?(:email)
|
61
|
+
get '/defined'
|
62
|
+
assert_equal 200, last_response.status
|
63
|
+
assert_equal 1, Sinatra::Mailer.deliveries.size
|
64
|
+
delivery = Sinatra::Mailer.deliveries.last
|
65
|
+
assert delivery.kind_of?(MailFactory)
|
66
|
+
assert_equal ['foo@example.com'], delivery.get_header('to')
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kusor-sinatra_mailer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Nicol\xC3\xA1s Sanguinetti"
|
8
|
+
- Pedro P. Candel
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-05-26 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: sinatra
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.9.2
|
25
|
+
version:
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: mailfactory
|
28
|
+
type: :runtime
|
29
|
+
version_requirement:
|
30
|
+
version_requirements: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.4.0
|
35
|
+
version:
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rack-test
|
38
|
+
type: :development
|
39
|
+
version_requirement:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 0.3.0
|
45
|
+
version:
|
46
|
+
description: "Sinatra::Mailer extension extracted from Merb::Mailer by Nicol\xC3\xA1s Sanguinetti."
|
47
|
+
email: kusorbox@gmail.com
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
extra_rdoc_files:
|
53
|
+
- README.markdown
|
54
|
+
files:
|
55
|
+
- LICENSE
|
56
|
+
- README.markdown
|
57
|
+
- sinatra_mailer.gemspec
|
58
|
+
- Rakefile
|
59
|
+
- lib/sinatra/mailer.rb
|
60
|
+
has_rdoc: false
|
61
|
+
homepage: http://github.com/kusor/sinatra-mailer/tree/master
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --main
|
65
|
+
- README.markdown
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
version:
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.2.0
|
84
|
+
signing_key:
|
85
|
+
specification_version: 2
|
86
|
+
summary: Send emails from Sinatra like Merb::Mailer
|
87
|
+
test_files:
|
88
|
+
- test/test_classic_application.rb
|
89
|
+
- test/test_modular_application.rb
|