merb-mailer 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +282 -0
- data/Rakefile +36 -24
- data/lib/generators/mailer_generator.rb +34 -0
- data/lib/generators/templates/mailer/app/mailers/%file_name%_mailer.rb +11 -0
- data/lib/generators/templates/mailer/app/mailers/views/%file_name%_mailer/notify_on_event.text.erb +1 -0
- data/lib/generators/templates/mailer/spec/mailers/%file_name%_mailer_spec.rb +39 -0
- data/lib/merb-mailer.rb +4 -1
- data/lib/merb-mailer/mail_controller.rb +25 -1
- data/lib/merb-mailer/mailer.rb +19 -17
- data/lib/merb-mailer/mailer_mixin.rb +25 -0
- data/spec/mailer_generator_spec.rb +2 -0
- data/spec/mailer_spec.rb +1 -1
- metadata +27 -11
- data/README +0 -4
- data/lib/merb-mailer/merb_controller.rb +0 -21
data/README.textile
ADDED
@@ -0,0 +1,282 @@
|
|
1
|
+
h2. Overview.
|
2
|
+
|
3
|
+
A plugin for the Merb framework that allows you to send email from Merb application.
|
4
|
+
It separates email composition and sending into micro MVC: you may have mail controllers
|
5
|
+
that compose complex emails, emails have their own views and models (models use MailFactory
|
6
|
+
library and non-persistable).
|
7
|
+
|
8
|
+
|
9
|
+
h2. Installation.
|
10
|
+
|
11
|
+
(sudo) gem install merb-mailer
|
12
|
+
|
13
|
+
It will install "mailfactory":http://mailfactory.rubyforge.org/ gem and "mime-types":http://mime-types.rubyforge.org/ that mailfactory depends on.
|
14
|
+
|
15
|
+
|
16
|
+
h2. Configuration.
|
17
|
+
|
18
|
+
In init.rb, include a dependency on 'merb-mailer'.With versions of Merb earlier than 0.9.3 Merb::Mailer may raise a NameError when configuration is put into
|
19
|
+
after_app_loads block. Put it to the very end of your init file and it should work. This problem does not exist in Git HEAD.
|
20
|
+
|
21
|
+
|
22
|
+
h3. Using SMTP.
|
23
|
+
|
24
|
+
<pre><code class="ruby">
|
25
|
+
Merb::Mailer.config = {
|
26
|
+
:host => 'smtp.yourserver.com',
|
27
|
+
:port => '25',
|
28
|
+
:user => 'user',
|
29
|
+
:pass => 'pass',
|
30
|
+
:auth => :plain # :plain, :login, :cram_md5, the default is no auth
|
31
|
+
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
32
|
+
}
|
33
|
+
</code></pre>
|
34
|
+
|
35
|
+
|
36
|
+
h3. Using Gmail SMTP.
|
37
|
+
|
38
|
+
Configuration example for Gmail SMTP:
|
39
|
+
|
40
|
+
<pre><code class="ruby">
|
41
|
+
Merb::Mailer.config = {
|
42
|
+
:host => 'smtp.gmail.com',
|
43
|
+
:port => '587',
|
44
|
+
:user => 'user@gmail.com',
|
45
|
+
:pass => 'pass',
|
46
|
+
:auth => :plain
|
47
|
+
}
|
48
|
+
</code></pre>
|
49
|
+
|
50
|
+
* require "smtp_tls":http://www.rubyinside.com/how-to-use-gmails-smtp-server-with-rails-394.html
|
51
|
+
* Use :text option instead of :body when deliver.
|
52
|
+
|
53
|
+
<pre><code class="ruby">
|
54
|
+
m = Merb::Mailer.new :to => 'foo@bar.com',
|
55
|
+
:from => 'bar@foo.com',
|
56
|
+
:subject => 'Welcome to whatever!',
|
57
|
+
:text => partial(:sometemplate)
|
58
|
+
m.deliver!
|
59
|
+
</code></pre>
|
60
|
+
|
61
|
+
h3. Using Sendmail.
|
62
|
+
|
63
|
+
Merb::Mailer.config = {:sendmail_path => '/somewhere/odd'}
|
64
|
+
Merb::Mailer.delivery_method = :sendmail
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
h2. Sending mail from controllers.
|
69
|
+
|
70
|
+
Merb mailer plugin has idea of separation of mailer logic from mailer templates
|
71
|
+
to the point mailers in Merb have own tiny MVC architecture for emails.
|
72
|
+
Your application controllers usually delegate email sending to mail controllers
|
73
|
+
instead of doing it on their own.
|
74
|
+
|
75
|
+
To send your mail using mail controller you use send_mail method that
|
76
|
+
takes mail controller class, action name, mail parameters and action parameters.
|
77
|
+
params hash with action parameters mentioned above is accessible in mail controller's
|
78
|
+
action.
|
79
|
+
|
80
|
+
<pre>
|
81
|
+
<code class="ruby">
|
82
|
+
def send_activation_email(person)
|
83
|
+
send_mail PeopleMailer, :activation, {
|
84
|
+
:from => "no-reply@example.com",
|
85
|
+
:to => person.email,
|
86
|
+
:subject => "Please activate your account"
|
87
|
+
}, {
|
88
|
+
:name => person.name
|
89
|
+
}
|
90
|
+
end
|
91
|
+
</code>
|
92
|
+
</pre>
|
93
|
+
|
94
|
+
Mail parameters you can specify:
|
95
|
+
|
96
|
+
* :to
|
97
|
+
* :from
|
98
|
+
* :replyto
|
99
|
+
* :subject
|
100
|
+
* :body
|
101
|
+
* :cc
|
102
|
+
|
103
|
+
|
104
|
+
Example of Merb controller:
|
105
|
+
<pre>
|
106
|
+
<code class="ruby">
|
107
|
+
class ProductDeliveryMailer < Merb::MailController
|
108
|
+
def notify_on_delivery
|
109
|
+
@delivery_info = params[:details]
|
110
|
+
# you can access @delivery_info in rendered template
|
111
|
+
render_mail
|
112
|
+
end
|
113
|
+
end
|
114
|
+
</code>
|
115
|
+
</pre>
|
116
|
+
|
117
|
+
Mail templates are kept under app/views/mail controller name/template name just like with regular controllers.
|
118
|
+
Content types in template name may be either text or html, like in
|
119
|
+
app/mailers/views/user_mailer/hello.text.erb for the mailer controller above:
|
120
|
+
|
121
|
+
<pre>
|
122
|
+
Hello <%= params[:name] %>
|
123
|
+
</pre>
|
124
|
+
|
125
|
+
render_mail works similarly to render method. It takes either action as Symbol or Hash of
|
126
|
+
template paths. Most of the times naming templates the same as your actions works so you
|
127
|
+
can just use
|
128
|
+
|
129
|
+
<pre>
|
130
|
+
<code class="ruby">
|
131
|
+
class ResourceShortageMailer < Merb::MailController
|
132
|
+
def notify_on_disk_space_shortage
|
133
|
+
render_mail
|
134
|
+
end
|
135
|
+
end
|
136
|
+
</code>
|
137
|
+
</pre>
|
138
|
+
|
139
|
+
and it will render app/mailers/views/resource_shortage_mailer/notify_on_disk_space_shortage.text.erb.
|
140
|
+
|
141
|
+
If you need to specify template path explicitly, you can do it for both text and html:
|
142
|
+
|
143
|
+
<pre>
|
144
|
+
<code class="ruby">
|
145
|
+
class ResourceShortageMailer < Merb::MailController
|
146
|
+
def notify_on_disk_space_shortage
|
147
|
+
render_mail :action => { :html => :hdd_space_shortage_detailed, :text => :hdd_space_shortage }
|
148
|
+
end
|
149
|
+
end
|
150
|
+
</code>
|
151
|
+
</pre>
|
152
|
+
|
153
|
+
This will look for app/mailers/views/resource_shortage_mailer/hdd_space_shortage_detailed.html.erb for
|
154
|
+
html and app/mailers/views/resource_shortage_mailer/hdd_space_shortage.text.erb for text.
|
155
|
+
|
156
|
+
See "render_mail documentation":http://merbivore.com/documentation/merb-more/0.9.3/merb-mailer/index.html?a=M000019&name=render_mail
|
157
|
+
for more examples, this method has a lot of options how you can use it.
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
h2. Using Merb mailer to send emails outside of controllers.
|
162
|
+
|
163
|
+
There are two ways of sending email with merb-mailer: using mail controllers and using just Merb::Mailer.
|
164
|
+
Here is example of using Merb::Mailer to deliver emails from model hook.
|
165
|
+
|
166
|
+
class Person
|
167
|
+
include DataMapper::Resource
|
168
|
+
|
169
|
+
after :create, :deliver_activation_notification
|
170
|
+
|
171
|
+
protected
|
172
|
+
|
173
|
+
def deliver_activation_notification
|
174
|
+
body_string = <<-EOS
|
175
|
+
Please activate your account...
|
176
|
+
EOS
|
177
|
+
|
178
|
+
Merb::Mailer.new(
|
179
|
+
:from => "no-reply@webapp.com",
|
180
|
+
:to => self.email,
|
181
|
+
:subject => "Activate your account",
|
182
|
+
:body => body_string
|
183
|
+
).deliver!
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
In this example we deliver signup activation email from model hook.
|
188
|
+
Keep in mind that Merb::Mailer is a thin wrapper around MailFactory that
|
189
|
+
provides several ways of sending emails. You can access plain text email body
|
190
|
+
with text method of mailer and html body with html method, respectively.
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
h2. Testing mailers.
|
195
|
+
|
196
|
+
A word of warning: merb-mailer does not raise exceptions when your template is not found.
|
197
|
+
So if nothing gets rendered, check merb test environment log to make sure you have no
|
198
|
+
warnings. If it is considered a bug, file a ticket to LightHouse.
|
199
|
+
|
200
|
+
Make sure you use test sending method so emails won't be sent on each tests run.
|
201
|
+
Add this line to your test file:
|
202
|
+
|
203
|
+
<pre>
|
204
|
+
<code class="ruby">
|
205
|
+
Merb::Mailer.delivery_method = :test_send
|
206
|
+
</code>
|
207
|
+
</pre>
|
208
|
+
|
209
|
+
Here is an example of helper to test mailers themselves:
|
210
|
+
<pre>
|
211
|
+
<code class="ruby">
|
212
|
+
def describe_mail(mailer, template, &block)
|
213
|
+
describe "/#{mailer.to_s.downcase}/#{template}" do
|
214
|
+
before :each do
|
215
|
+
@mailer_class, @template = mailer, template
|
216
|
+
@assigns = {}
|
217
|
+
end
|
218
|
+
|
219
|
+
def deliver(send_params={}, mail_params={})
|
220
|
+
mail_params = {:from => "from@example.com", :to => "to@example.com", :subject => "Please activate your account"}.merge(mail_params)
|
221
|
+
@mailer_class.new(send_params).dispatch_and_deliver @template.to_sym, mail_params
|
222
|
+
@mail = Merb::Mailer.deliveries.last
|
223
|
+
end
|
224
|
+
|
225
|
+
instance_eval &block
|
226
|
+
end
|
227
|
+
end
|
228
|
+
</code>
|
229
|
+
</pre>
|
230
|
+
|
231
|
+
Mailer controller specs may look like this then:
|
232
|
+
<pre>
|
233
|
+
<code class="ruby">
|
234
|
+
require File.join(File.dirname(__FILE__),'..','spec_helper')
|
235
|
+
|
236
|
+
describe_mail UserMailer, :hello do
|
237
|
+
it "sends activation" do
|
238
|
+
deliver :name => "Jamie"
|
239
|
+
@mail.text.should == "Please activate your account, Jamie"
|
240
|
+
end
|
241
|
+
end
|
242
|
+
</code>
|
243
|
+
</pre>
|
244
|
+
|
245
|
+
Most of mail controller specs verify delivered email headers like
|
246
|
+
to, subject or body. To access deliveries you use Merb::Mailer.deliveries
|
247
|
+
array.
|
248
|
+
|
249
|
+
It is recommended to clear it on test setup first:
|
250
|
+
|
251
|
+
<pre>
|
252
|
+
<code class="ruby">
|
253
|
+
require File.join(File.dirname(__FILE__),'..','spec_helper')
|
254
|
+
|
255
|
+
describe_mail UserMailer, :hello do
|
256
|
+
before :each do
|
257
|
+
Merb::Mailer.deliveries.clear
|
258
|
+
end
|
259
|
+
end
|
260
|
+
</code>
|
261
|
+
</pre>
|
262
|
+
|
263
|
+
To do actual matching you can create a helper like this:
|
264
|
+
|
265
|
+
<pre>
|
266
|
+
<code class="ruby">
|
267
|
+
def last_delivered_email
|
268
|
+
Merb::Mailer.deliveries.last
|
269
|
+
end
|
270
|
+
</code>
|
271
|
+
</pre>
|
272
|
+
|
273
|
+
and use it like this:
|
274
|
+
|
275
|
+
<pre>
|
276
|
+
<code class="ruby">
|
277
|
+
last_delivered_email.from.first.should == "no-reply@webapp.com"
|
278
|
+
</code>
|
279
|
+
</pre>
|
280
|
+
|
281
|
+
Note that MailFactory that is used by merb-mailer under the covers returns headers
|
282
|
+
as Arrays. This is why we used from.first in example above.
|
data/Rakefile
CHANGED
@@ -1,50 +1,62 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake/gempackagetask'
|
3
|
+
require "extlib"
|
4
|
+
require 'merb-core/tasks/merb_rake_helper'
|
3
5
|
require "spec/rake/spectask"
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
##############################################################################
|
8
|
+
# Package && release
|
9
|
+
##############################################################################
|
10
|
+
RUBY_FORGE_PROJECT = "merb"
|
11
|
+
PROJECT_URL = "http://merbivore.com"
|
12
|
+
PROJECT_SUMMARY = "Merb plugin that provides mailer functionality to Merb"
|
13
|
+
PROJECT_DESCRIPTION = PROJECT_SUMMARY
|
14
|
+
|
15
|
+
GEM_AUTHOR = "Yehuda Katz"
|
16
|
+
GEM_EMAIL = "ykatz@engineyard.com"
|
17
|
+
|
18
|
+
GEM_NAME = "merb-mailer"
|
19
|
+
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
|
20
|
+
GEM_VERSION = (Merb::MORE_VERSION rescue "0.9.4") + PKG_BUILD
|
21
|
+
|
22
|
+
RELEASE_NAME = "REL #{GEM_VERSION}"
|
23
|
+
|
24
|
+
require "extlib/tasks/release"
|
12
25
|
|
13
26
|
spec = Gem::Specification.new do |s|
|
14
|
-
s.
|
15
|
-
s.
|
27
|
+
s.rubyforge_project = RUBY_FORGE_PROJECT
|
28
|
+
s.name = GEM_NAME
|
29
|
+
s.version = GEM_VERSION
|
16
30
|
s.platform = Gem::Platform::RUBY
|
17
31
|
s.has_rdoc = true
|
18
|
-
s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
|
19
|
-
s.summary =
|
20
|
-
s.description =
|
21
|
-
s.author =
|
22
|
-
s.email =
|
23
|
-
s.homepage =
|
24
|
-
s.add_dependency('merb-core', '>= 0.9.
|
25
|
-
s.add_dependency(
|
32
|
+
s.extra_rdoc_files = ["README.textile", "LICENSE", 'TODO']
|
33
|
+
s.summary = PROJECT_SUMMARY
|
34
|
+
s.description = PROJECT_DESCRIPTION
|
35
|
+
s.author = GEM_AUTHOR
|
36
|
+
s.email = GEM_EMAIL
|
37
|
+
s.homepage = PROJECT_URL
|
38
|
+
s.add_dependency('merb-core', '>= 0.9.4')
|
39
|
+
s.add_dependency('mailfactory', '>= 1.2.3')
|
26
40
|
s.require_path = 'lib'
|
27
|
-
s.
|
28
|
-
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
|
41
|
+
s.files = %w(LICENSE README.textile Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
|
29
42
|
end
|
30
43
|
|
31
44
|
Rake::GemPackageTask.new(spec) do |pkg|
|
32
45
|
pkg.gem_spec = spec
|
33
46
|
end
|
34
47
|
|
35
|
-
|
36
|
-
|
48
|
+
desc "Install the gem"
|
37
49
|
task :install => [:package] do
|
38
|
-
sh %{sudo gem install #{install_home} pkg/#{
|
50
|
+
sh %{#{sudo} gem install #{install_home} pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources}
|
39
51
|
end
|
40
52
|
|
41
53
|
namespace :jruby do
|
42
54
|
|
43
55
|
desc "Run :package and install the resulting .gem with jruby"
|
44
56
|
task :install => :package do
|
45
|
-
sh %{#{
|
57
|
+
sh %{#{sudo} jruby -S gem install #{install_home} pkg/#{GEM_NAME}-#{GEM_VERSION}.gem --no-rdoc --no-ri}
|
46
58
|
end
|
47
|
-
|
59
|
+
|
48
60
|
end
|
49
61
|
|
50
62
|
desc "Run all specs"
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Merb::Generators
|
2
|
+
class MailerGenerator < NamespacedGenerator
|
3
|
+
|
4
|
+
def self.source_root
|
5
|
+
File.dirname(__FILE__) / 'templates' / 'mailer'
|
6
|
+
end
|
7
|
+
|
8
|
+
desc <<-DESC
|
9
|
+
Generates a mailer
|
10
|
+
DESC
|
11
|
+
|
12
|
+
option :testing_framework, :desc => 'Testing framework to use (one of: spec, test_unit)'
|
13
|
+
|
14
|
+
first_argument :name, :required => true, :desc => "mailer name"
|
15
|
+
|
16
|
+
template :mailer do
|
17
|
+
source('app/mailers/%file_name%_mailer.rb')
|
18
|
+
destination("app/mailers", base_path, "#{file_name}_mailer.rb")
|
19
|
+
end
|
20
|
+
|
21
|
+
template :notify_on_event do
|
22
|
+
source('app/mailers/views/%file_name%_mailer/notify_on_event.text.erb')
|
23
|
+
destination("app/mailers/views", base_path, "#{file_name}_mailer/notify_on_event.text.erb")
|
24
|
+
end
|
25
|
+
|
26
|
+
template :controller_spec, :testing_framework => :rspec do
|
27
|
+
source('spec/mailers/%file_name%_mailer_spec.rb')
|
28
|
+
destination("spec/mailers", base_path, "#{file_name}_mailer_spec.rb")
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
add :mailer, MailerGenerator
|
34
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<% with_modules(modules) do -%>
|
2
|
+
class <%= class_name %>Mailer < Merb::MailController
|
3
|
+
|
4
|
+
def notify_on_event
|
5
|
+
# use params[] passed to this controller to get data
|
6
|
+
# read more at http://wiki.merbivore.com/pages/mailers
|
7
|
+
render_mail
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
<% end -%>
|
data/lib/generators/templates/mailer/app/mailers/views/%file_name%_mailer/notify_on_event.text.erb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Sample template for plain text email.
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "spec_helper")
|
2
|
+
|
3
|
+
# Move this to your spec_helper.rb.
|
4
|
+
module MailControllerTestHelper
|
5
|
+
# Helper to clear mail deliveries.
|
6
|
+
def clear_mail_deliveries
|
7
|
+
Merb::Mailer.deliveries.clear
|
8
|
+
end
|
9
|
+
|
10
|
+
# Helper to access last delivered mail.
|
11
|
+
# In test mode merb-mailer puts email to
|
12
|
+
# collection accessible as Merb::Mailer.deliveries.
|
13
|
+
def last_delivered_mail
|
14
|
+
Merb::Mailer.deliveries.last
|
15
|
+
end
|
16
|
+
|
17
|
+
# Helper to deliver
|
18
|
+
def deliver(action, mail_params = {}, send_params = {})
|
19
|
+
<%= class_name %>Mailer.dispatch_and_deliver(action, { :from => "no-reply@webapp.com", :to => "recepient@person.com" }.merge(mail_params), send_params)
|
20
|
+
@delivery = last_delivered_mail
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe <%= class_name %>Mailer, "#notify_on_event email template" do
|
25
|
+
include MailControllerTestHelper
|
26
|
+
|
27
|
+
before :each do
|
28
|
+
clear_mail_deliveries
|
29
|
+
|
30
|
+
# instantiate some fixture objects
|
31
|
+
end
|
32
|
+
|
33
|
+
it "includes welcome phrase in email text" do
|
34
|
+
violated "Mailer controller deserves to have specs, too."
|
35
|
+
|
36
|
+
# <%= class_name %>Mailer.dispatch_and_deliver(:notify_on_event, {}, { :name => "merb-mailer user" })
|
37
|
+
# last_delivered_mail.text.should =~ /Hello, merb-mailer user!/
|
38
|
+
end
|
39
|
+
end
|
data/lib/merb-mailer.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
1
|
require "merb-mailer/mailer"
|
2
2
|
require "merb-mailer/mail_controller"
|
3
|
-
require "merb-mailer/
|
3
|
+
require "merb-mailer/mailer_mixin"
|
4
|
+
|
5
|
+
Merb::Controller.send(:include, Merb::MailerMixin)
|
6
|
+
Merb.add_generators(File.join(File.dirname(__FILE__), 'generators', 'mailer_generator'))
|
@@ -71,6 +71,13 @@ module Merb
|
|
71
71
|
attr_accessor :params, :mailer, :mail
|
72
72
|
attr_reader :session, :base_controller
|
73
73
|
|
74
|
+
cattr_accessor :_subclasses
|
75
|
+
self._subclasses = Set.new
|
76
|
+
|
77
|
+
# ==== Returns
|
78
|
+
# Array[Class]:: Classes that inherit from Merb::MailController.
|
79
|
+
def self.subclasses_list() _subclasses end
|
80
|
+
|
74
81
|
# ==== Parameters
|
75
82
|
# action<~to_s>:: The name of the action that will be rendered.
|
76
83
|
# type<~to_s>::
|
@@ -84,6 +91,23 @@ module Merb
|
|
84
91
|
def _template_location(action, type = nil, controller = controller_name)
|
85
92
|
"#{controller}/#{action}.#{type}"
|
86
93
|
end
|
94
|
+
|
95
|
+
# The location to look for a template and mime-type. This is overridden
|
96
|
+
# from AbstractController, which defines a version of this that does not
|
97
|
+
# involve mime-types.
|
98
|
+
#
|
99
|
+
# ==== Parameters
|
100
|
+
# template<String>::
|
101
|
+
# The absolute path to a template - without mime and template extension.
|
102
|
+
# The mime-type extension is optional - it will be appended from the
|
103
|
+
# current content type if it hasn't been added already.
|
104
|
+
# type<~to_s>::
|
105
|
+
# The mime-type of the template that will be rendered. Defaults to nil.
|
106
|
+
#
|
107
|
+
# @public
|
108
|
+
def _absolute_template_location(template, type)
|
109
|
+
template.match(/\.#{type.to_s.escape_regexp}$/) ? template : "#{template}.#{type}"
|
110
|
+
end
|
87
111
|
|
88
112
|
# ==== Parameters
|
89
113
|
# params<Hash>:: Configuration parameters for the MailController.
|
@@ -102,7 +126,7 @@ module Merb
|
|
102
126
|
# The Merb::MailController inheriting from the base class.
|
103
127
|
def self.inherited(klass)
|
104
128
|
super
|
105
|
-
klass.
|
129
|
+
klass._template_root = Merb.dir_for(:mailer) / "views" unless self._template_root
|
106
130
|
end
|
107
131
|
|
108
132
|
# Override filters halted to return nothing.
|
data/lib/merb-mailer/mailer.rb
CHANGED
@@ -3,7 +3,7 @@ begin
|
|
3
3
|
require 'net/smtp'
|
4
4
|
rescue LoadError
|
5
5
|
Merb.logger.warn "You need to install the mailfactory gem to use Merb::Mailer"
|
6
|
-
end
|
6
|
+
end
|
7
7
|
|
8
8
|
class MailFactory
|
9
9
|
attr_reader :html, :text
|
@@ -16,27 +16,29 @@ module Merb
|
|
16
16
|
#
|
17
17
|
# Merb::Mailer.config = {
|
18
18
|
# :host => 'smtp.yourserver.com',
|
19
|
-
# :port => '25',
|
19
|
+
# :port => '25',
|
20
20
|
# :user => 'user',
|
21
21
|
# :pass => 'pass',
|
22
22
|
# :auth => :plain # :plain, :login, :cram_md5, the default is no auth
|
23
|
-
# :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
23
|
+
# :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
24
24
|
# }
|
25
|
-
#
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
#
|
26
|
+
# or
|
27
|
+
#
|
28
|
+
# Merb::Mailer.config = {:sendmail_path => '/somewhere/odd'}
|
29
29
|
# Merb::Mailer.delivery_method = :sendmail
|
30
30
|
#
|
31
31
|
# You could send mail manually like this (but it's better to use
|
32
32
|
# a MailController instead).
|
33
|
-
#
|
33
|
+
#
|
34
34
|
# m = Merb::Mailer.new :to => 'foo@bar.com',
|
35
35
|
# :from => 'bar@foo.com',
|
36
36
|
# :subject => 'Welcome to whatever!',
|
37
|
-
# :
|
38
|
-
# m.deliver!
|
39
|
-
|
37
|
+
# :html => partial(:sometemplate)
|
38
|
+
# m.deliver!
|
39
|
+
#
|
40
|
+
# You can use :text option to specify plain text email body
|
41
|
+
# and :html for HTML email body.
|
40
42
|
class Mailer
|
41
43
|
|
42
44
|
class_inheritable_accessor :config, :delivery_method, :deliveries
|
@@ -45,14 +47,14 @@ module Merb
|
|
45
47
|
|
46
48
|
# Sends the mail using sendmail.
|
47
49
|
def sendmail
|
48
|
-
sendmail = IO.popen("#{config[:sendmail_path]} #{@mail.to}", 'w+')
|
50
|
+
sendmail = IO.popen("#{config[:sendmail_path]} #{@mail.to}", 'w+')
|
49
51
|
sendmail.puts @mail.to_s
|
50
52
|
sendmail.close
|
51
53
|
end
|
52
54
|
|
53
55
|
# Sends the mail using SMTP.
|
54
56
|
def net_smtp
|
55
|
-
Net::SMTP.start(config[:host], config[:port].to_i, config[:domain],
|
57
|
+
Net::SMTP.start(config[:host], config[:port].to_i, config[:domain],
|
56
58
|
config[:user], config[:pass], config[:auth]) { |smtp|
|
57
59
|
smtp.send_message(@mail.to_s, @mail.from.first, @mail.to.to_s.split(/[,;]/))
|
58
60
|
}
|
@@ -80,7 +82,7 @@ module Merb
|
|
80
82
|
# ==== Raises
|
81
83
|
# ArgumentError::
|
82
84
|
# file_or_files was not a File or an Array of File instances.
|
83
|
-
def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
|
85
|
+
def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
|
84
86
|
type = nil, headers = nil)
|
85
87
|
if file_or_files.is_a?(Array)
|
86
88
|
file_or_files.each {|k,v| @mail.add_attachment_as k, *v}
|
@@ -89,16 +91,16 @@ module Merb
|
|
89
91
|
@mail.add_attachment_as(file_or_files, filename, type, headers)
|
90
92
|
end
|
91
93
|
end
|
92
|
-
|
94
|
+
|
93
95
|
# ==== Parameters
|
94
96
|
# o<Hash{~to_s => Object}>:: Configuration commands to send to MailFactory.
|
95
97
|
def initialize(o={})
|
96
|
-
self.config = {:sendmail_path => '/usr/sbin/sendmail'} if config.nil?
|
98
|
+
self.config = {:sendmail_path => '/usr/sbin/sendmail'} if config.nil?
|
97
99
|
o[:rawhtml] = o.delete(:html)
|
98
100
|
m = MailFactory.new()
|
99
101
|
o.each { |k,v| m.send "#{k}=", v }
|
100
102
|
@mail = m
|
101
103
|
end
|
102
|
-
|
104
|
+
|
103
105
|
end
|
104
106
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Merb
|
2
|
+
module MailerMixin
|
3
|
+
|
4
|
+
# Sends mail via a MailController (a tutorial can be found in the
|
5
|
+
# MailController docs).
|
6
|
+
#
|
7
|
+
# ==== Parameters
|
8
|
+
# klass<Class>:: The mailer class.
|
9
|
+
# method<~to_s>:: The method to call on the mailer.
|
10
|
+
# mail_params<Hash>::
|
11
|
+
# Mailing parameters, e.g. :to and :cc. See
|
12
|
+
# Merb::MailController#dispatch_and_deliver for details.
|
13
|
+
# send_params<Hash>::
|
14
|
+
# Params to send to the mailer. Defaults to the params of the current
|
15
|
+
# controller.
|
16
|
+
#
|
17
|
+
# ==== Examples
|
18
|
+
# # Send an email via the FooMailer's bar method.
|
19
|
+
# send_mail FooMailer, :bar, :from => "foo@bar.com", :to => "baz@bat.com"
|
20
|
+
def send_mail(klass, method, mail_params, send_params = nil)
|
21
|
+
klass.new(send_params || params, self).dispatch_and_deliver(method, mail_params)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
data/spec/mailer_spec.rb
CHANGED
@@ -34,7 +34,7 @@ describe "a merb mailer" do
|
|
34
34
|
delivery = TestMailer.deliveries.last
|
35
35
|
delivery.to.should include("test@test.com")
|
36
36
|
delivery.from.should include("foo@bar.com")
|
37
|
-
delivery.subject.should include("
|
37
|
+
delivery.subject.should include("=?utf-8?Q?Test_Subject=?=")
|
38
38
|
delivery.body.should include("Test")
|
39
39
|
end
|
40
40
|
|
metadata
CHANGED
@@ -1,28 +1,30 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb-mailer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yehuda Katz
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-08-13 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: merb-core
|
17
|
+
type: :runtime
|
17
18
|
version_requirement:
|
18
19
|
version_requirements: !ruby/object:Gem::Requirement
|
19
20
|
requirements:
|
20
21
|
- - ">="
|
21
22
|
- !ruby/object:Gem::Version
|
22
|
-
version: 0.9.
|
23
|
+
version: 0.9.4
|
23
24
|
version:
|
24
25
|
- !ruby/object:Gem::Dependency
|
25
26
|
name: mailfactory
|
27
|
+
type: :runtime
|
26
28
|
version_requirement:
|
27
29
|
version_requirements: !ruby/object:Gem::Requirement
|
28
30
|
requirements:
|
@@ -31,26 +33,40 @@ dependencies:
|
|
31
33
|
version: 1.2.3
|
32
34
|
version:
|
33
35
|
description: Merb plugin that provides mailer functionality to Merb
|
34
|
-
email:
|
36
|
+
email: ykatz@engineyard.com
|
35
37
|
executables: []
|
36
38
|
|
37
39
|
extensions: []
|
38
40
|
|
39
41
|
extra_rdoc_files:
|
40
|
-
- README
|
42
|
+
- README.textile
|
41
43
|
- LICENSE
|
42
44
|
- TODO
|
43
45
|
files:
|
44
46
|
- LICENSE
|
45
|
-
- README
|
47
|
+
- README.textile
|
46
48
|
- Rakefile
|
47
49
|
- TODO
|
50
|
+
- lib/generators
|
51
|
+
- lib/generators/mailer_generator.rb
|
52
|
+
- lib/generators/templates
|
53
|
+
- lib/generators/templates/mailer
|
54
|
+
- lib/generators/templates/mailer/app
|
55
|
+
- lib/generators/templates/mailer/app/mailers
|
56
|
+
- lib/generators/templates/mailer/app/mailers/%file_name%_mailer.rb
|
57
|
+
- lib/generators/templates/mailer/app/mailers/views
|
58
|
+
- lib/generators/templates/mailer/app/mailers/views/%file_name%_mailer
|
59
|
+
- lib/generators/templates/mailer/app/mailers/views/%file_name%_mailer/notify_on_event.text.erb
|
60
|
+
- lib/generators/templates/mailer/spec
|
61
|
+
- lib/generators/templates/mailer/spec/mailers
|
62
|
+
- lib/generators/templates/mailer/spec/mailers/%file_name%_mailer_spec.rb
|
48
63
|
- lib/merb-mailer
|
49
64
|
- lib/merb-mailer/mail_controller.rb
|
50
65
|
- lib/merb-mailer/mailer.rb
|
51
|
-
- lib/merb-mailer/
|
66
|
+
- lib/merb-mailer/mailer_mixin.rb
|
52
67
|
- lib/merb-mailer.rb
|
53
68
|
- spec/mail_controller_spec.rb
|
69
|
+
- spec/mailer_generator_spec.rb
|
54
70
|
- spec/mailer_spec.rb
|
55
71
|
- spec/mailers
|
56
72
|
- spec/mailers/views
|
@@ -68,7 +84,7 @@ files:
|
|
68
84
|
- spec/mailers/views/test_mail_controller/third.html.erb
|
69
85
|
- spec/spec_helper.rb
|
70
86
|
has_rdoc: true
|
71
|
-
homepage: http://
|
87
|
+
homepage: http://merbivore.com
|
72
88
|
post_install_message:
|
73
89
|
rdoc_options: []
|
74
90
|
|
@@ -88,8 +104,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
104
|
version:
|
89
105
|
requirements: []
|
90
106
|
|
91
|
-
rubyforge_project:
|
92
|
-
rubygems_version: 1.0
|
107
|
+
rubyforge_project: merb
|
108
|
+
rubygems_version: 1.2.0
|
93
109
|
signing_key:
|
94
110
|
specification_version: 2
|
95
111
|
summary: Merb plugin that provides mailer functionality to Merb
|
data/README
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
class Merb::Controller
|
2
|
-
# Sends mail via a MailController (a tutorial can be found in the
|
3
|
-
# MailController docs).
|
4
|
-
#
|
5
|
-
# ==== Parameters
|
6
|
-
# klass<Class>:: The mailer class.
|
7
|
-
# method<~to_s>:: The method to call on the mailer.
|
8
|
-
# mail_params<Hash>::
|
9
|
-
# Mailing parameters, e.g. :to and :cc. See
|
10
|
-
# Merb::MailController#dispatch_and_deliver for details.
|
11
|
-
# send_params<Hash>::
|
12
|
-
# Params to send to the mailer. Defaults to the params of the current
|
13
|
-
# controller.
|
14
|
-
#
|
15
|
-
# ==== Examples
|
16
|
-
# # Send an email via the FooMailer's bar method.
|
17
|
-
# send_mail FooMailer, :bar, :from => "foo@bar.com", :to => "baz@bat.com"
|
18
|
-
def send_mail(klass, method, mail_params, send_params = nil)
|
19
|
-
klass.new(send_params || params, self).dispatch_and_deliver(method, mail_params)
|
20
|
-
end
|
21
|
-
end
|