padrino-mailer 0.9.10 → 0.9.11
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +75 -35
- data/Rakefile +4 -52
- data/lib/padrino-mailer.rb +16 -8
- data/lib/padrino-mailer/base.rb +57 -78
- data/lib/padrino-mailer/ext.rb +231 -0
- data/lib/padrino-mailer/helpers.rb +125 -0
- data/lib/padrino-mailer/mime.rb +42 -0
- data/padrino-mailer.gemspec +15 -70
- data/test/fixtures/basic.erb +1 -0
- data/test/fixtures/layout.erb +1 -0
- data/test/fixtures/padrino_app/app.rb +69 -0
- data/test/fixtures/{mailer_app/views/demo_mailer → padrino_app/views/mailers/demo}/sample_mail.erb +0 -0
- data/test/fixtures/{mailer_app/views/sample_mailer/anniversary_message.erb → padrino_app/views/mailers/sample/anniversary.erb} +0 -0
- data/test/fixtures/{mailer_app/views/sample_mailer/birthday_message.erb → padrino_app/views/mailers/sample/birthday.erb} +0 -0
- data/test/fixtures/{mailer_app/views/sample_mailer → padrino_app/views/mailers/sample}/foo_message.erb +0 -0
- data/test/fixtures/sinatra_app/app.rb +67 -0
- data/test/fixtures/sinatra_app/views/mailers/demo/sample_mail.erb +1 -0
- data/test/fixtures/sinatra_app/views/mailers/sample/anniversary.erb +2 -0
- data/test/fixtures/sinatra_app/views/mailers/sample/birthday.erb +2 -0
- data/test/fixtures/sinatra_app/views/mailers/sample/foo_message.erb +1 -0
- data/test/fixtures/views/mailers/alternate/foo.erb +1 -0
- data/test/fixtures/views/mailers/bar.erb +1 -0
- data/test/fixtures/views/mailers/i18n/hello.en.erb +1 -0
- data/test/fixtures/views/mailers/i18n/hello.it.erb +1 -0
- data/test/fixtures/views/mailers/layouts/sample.erb +1 -0
- data/test/fixtures/views/mailers/multipart/basic.html.erb +1 -0
- data/test/fixtures/views/mailers/multipart/basic.plain.erb +1 -0
- data/test/fixtures/views/mailers/sample/foo.erb +1 -0
- data/test/helper.rb +38 -42
- data/test/test_email.rb +158 -0
- data/test/test_message.rb +153 -0
- data/test/test_padrino_mailer.rb +64 -23
- data/test/test_part.rb +119 -0
- metadata +95 -51
- data/lib/padrino-mailer/delivery.rb +0 -110
- data/lib/padrino-mailer/mail_object.rb +0 -65
- data/test/fixtures/mailer_app/app.rb +0 -64
- data/test/test_base.rb +0 -86
- data/test/test_mail_object.rb +0 -25
data/README.rdoc
CHANGED
@@ -2,60 +2,100 @@
|
|
2
2
|
|
3
3
|
=== Overview
|
4
4
|
|
5
|
-
This component
|
6
|
-
|
7
|
-
The
|
5
|
+
This component creates an easy and intuitive interface for delivering email within a Sinatra application. The mail library is utilized
|
6
|
+
to do the bulk of the work. There is full support for rendering email templates, using an html content type and for file attachments.
|
7
|
+
The Padrino Mailer uses a familiar Sinatra syntax similar to that of defining routes for a controller.
|
8
8
|
|
9
9
|
=== Usage
|
10
10
|
|
11
|
-
Let's take a look at using the
|
12
|
-
|
11
|
+
Let's take a look at using the Mailer in an application. By default, the Mailer uses the built-in sendmail
|
12
|
+
command on the server. However, smtp is also supported using the following configuration:
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
14
|
+
# Example for configuring gmail smtp
|
15
|
+
set :delivery_method, :smtp => {
|
16
|
+
:address => "smtp.gmail.com",
|
17
|
+
:port => 587,
|
18
|
+
:domain => 'your.host.name',
|
19
|
+
:user_name => '<username>',
|
20
|
+
:password => '<password>',
|
21
|
+
:authentication => 'plain',
|
22
|
+
:enable_starttls_auto => true
|
23
|
+
}
|
22
24
|
|
23
|
-
Once
|
24
|
-
|
25
|
+
Once the delivery settings have been defined, the default will become smtp delivery but can be overwritten in each message.
|
26
|
+
|
27
|
+
Padrino supports sending quick emails (using either sendmail or smtp) right from your controllers.
|
28
|
+
This is ideal for ‘one-off’ emails where the ‘full’ mailer declaration is simply unnecessary.
|
29
|
+
|
30
|
+
Delivering an email from within your controller is simple:
|
31
|
+
|
32
|
+
# app/controllers/session.rb
|
33
|
+
post :create do
|
34
|
+
email(:to => "john@smith.com", :subject => "Successfully Registered!", :body => "Test Body")
|
35
|
+
end
|
36
|
+
|
37
|
+
Padrino also supports structured mailer declarations. We can define a new mailer using a <tt>mailer</tt> block.
|
25
38
|
|
26
39
|
# app/mailers/sample_mailer.rb
|
27
|
-
|
28
|
-
|
29
|
-
from
|
30
|
-
to
|
40
|
+
MyAppName.mailers :sample do
|
41
|
+
email :registration do |name|
|
42
|
+
from 'admin@site.com'
|
43
|
+
to 'user@domain.com'
|
31
44
|
subject 'Welcome to the site!'
|
32
|
-
|
33
|
-
|
34
|
-
charset 'windows-1252'
|
35
|
-
via :sendmail
|
36
|
-
|
45
|
+
locals :name => name
|
46
|
+
content_type 'html' # optional, defaults to plain/text
|
47
|
+
charset 'windows-1252' # optional, defaults to utf-8
|
48
|
+
via :sendmail # optional, smtp if defined otherwise sendmail
|
49
|
+
render 'registration'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
In addition to a standard body, Padrino also easily supports multi-part emails:
|
54
|
+
|
55
|
+
# app/mailers/sample_mailer.rb
|
56
|
+
MyAppName.mailers :sample do
|
57
|
+
email :registration do |name|
|
58
|
+
to 'padrino@test.lindsaar.net'
|
59
|
+
subject "nested multipart"
|
60
|
+
from "test@example.com"
|
61
|
+
|
62
|
+
text_part { render('multipart/basic.plain') }
|
63
|
+
html_part { render('multipart/basic.html') }
|
37
64
|
end
|
38
65
|
end
|
39
66
|
|
40
|
-
|
41
|
-
is passing the <tt>name</tt> attribute to the body message template which should be defined in
|
42
|
-
<tt>[views_path]/sample_mailer/registration_email.erb</tt> as shown below:
|
67
|
+
Defaults can also be declared on a per-mailer or app-wide basis:
|
43
68
|
|
44
|
-
#
|
69
|
+
# app/app.rb
|
70
|
+
set :mailer_defaults, :from => 'admin@site.com'
|
71
|
+
|
72
|
+
# app/mailers/sample_mailer.rb
|
73
|
+
MyAppName.mailers :sample do
|
74
|
+
defaults :content_type => 'html'
|
75
|
+
email :registration do |name, age|
|
76
|
+
# Uses default 'content_type' and 'from' values but can also overwrite them
|
77
|
+
to 'user@domain.com'
|
78
|
+
subject 'Welcome to the site!'
|
79
|
+
locals :name => name
|
80
|
+
render 'registration'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
This defines a message called '<tt>registration</tt>' with the specified attributes. The <tt>body</tt> method
|
85
|
+
is invoking the render function passing the <tt>name</tt> attribute to the body message template which is defined in
|
86
|
+
<tt>[views_path]/mailers/sample/registration.erb</tt> as shown below:
|
87
|
+
|
88
|
+
# ./views/mailers/sample/registration.erb
|
45
89
|
This is the body of the email and can access the <%= name %> that was passed in from the mailer definition
|
46
90
|
That's all there is to defining the body of the email which can be plain text or html
|
47
91
|
|
48
92
|
Once the mailer definition has been completed and the template has been defined, the email can be sent using:
|
49
93
|
|
50
|
-
|
51
|
-
|
52
|
-
or if you like the method_missing approach:
|
53
|
-
|
54
|
-
SampleMailer.deliver_registration_email "Bob", 'bob@bobby.com'
|
94
|
+
deliver(:sample, :registration_email, "Bob", "21")
|
55
95
|
|
56
|
-
And that will then deliver the email according the the configured options. This is
|
96
|
+
And that will then deliver the email according the the configured options. This is all you need to send basic emails.
|
57
97
|
|
58
|
-
Be sure to check out the
|
98
|
+
The mailer also supports the attachment of files and various other options. Be sure to check out the
|
59
99
|
{Padrino Mailer}[http://www.padrinorb.com/guides/padrino-mailer] guide for more details on usage.
|
60
100
|
|
61
101
|
== Copyright
|
data/Rakefile
CHANGED
@@ -1,53 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require File.expand_path("../../padrino-core/lib/padrino-core/version.rb", __FILE__)
|
5
|
-
|
6
|
-
begin
|
7
|
-
require 'jeweler'
|
8
|
-
Jeweler::Tasks.new do |gem|
|
9
|
-
gem.name = "padrino-mailer"
|
10
|
-
gem.summary = "Mailer system for padrino"
|
11
|
-
gem.description = "Mailer system for padrino allowing easy delivery of application emails"
|
12
|
-
gem.email = "padrinorb@gmail.com"
|
13
|
-
gem.homepage = "http://github.com/padrino/padrino-framework/tree/master/padrino-mailer"
|
14
|
-
gem.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
|
15
|
-
gem.rubyforge_project = 'padrino-mailer'
|
16
|
-
gem.version = Padrino.version
|
17
|
-
gem.add_runtime_dependency "padrino-core", "= #{Padrino.version}"
|
18
|
-
gem.add_runtime_dependency "tmail", ">= 1.2"
|
19
|
-
gem.add_development_dependency "shoulda", ">= 2.10.3"
|
20
|
-
gem.add_development_dependency "haml", ">= 2.2.1"
|
21
|
-
gem.add_development_dependency "mocha", ">= 0.9.7"
|
22
|
-
gem.add_development_dependency "rack-test", ">= 0.5.0"
|
23
|
-
gem.add_development_dependency "webrat", ">= 0.5.1"
|
24
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
25
|
-
end
|
26
|
-
Jeweler::GemcutterTasks.new
|
27
|
-
Jeweler::RubyforgeTasks.new { |r| r.doc_task = :none }
|
28
|
-
rescue LoadError
|
29
|
-
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
30
|
-
end
|
1
|
+
# coding:utf-8
|
2
|
+
RAKE_ROOT = __FILE__
|
31
3
|
|
32
|
-
require '
|
33
|
-
|
34
|
-
test.libs << 'test'
|
35
|
-
test.pattern = 'test/**/test_*.rb'
|
36
|
-
test.verbose = true
|
37
|
-
end
|
38
|
-
|
39
|
-
begin
|
40
|
-
require 'rcov/rcovtask'
|
41
|
-
Rcov::RcovTask.new do |rcov|
|
42
|
-
rcov.libs << 'test'
|
43
|
-
rcov.pattern = 'test/**/test_*.rb'
|
44
|
-
rcov.verbose = true
|
45
|
-
rcov.rcov_opts << ['--exclude /Gems/1.8/gems/,padrino-core,padrino-cache,padrino-gen,padrino-helpers,padrino-admin']
|
46
|
-
end
|
47
|
-
rescue LoadError
|
48
|
-
task :rcov do
|
49
|
-
abort "RCov is not available. In order to run rcov, you must: sudo gem install relevance-rcov"
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
task :default => :test
|
4
|
+
require 'rubygems'
|
5
|
+
require File.expand_path(File.dirname(__FILE__) + '/../gem_rake_helper')
|
data/lib/padrino-mailer.rb
CHANGED
@@ -4,23 +4,31 @@ begin
|
|
4
4
|
rescue LoadError
|
5
5
|
require 'sinatra/tilt'
|
6
6
|
end
|
7
|
-
require 'padrino-core/support_lite'
|
7
|
+
require 'padrino-core/support_lite' unless defined?(SupportLite)
|
8
|
+
require 'mail'
|
8
9
|
|
9
|
-
|
10
|
+
# Require respecting order of our dependencies
|
11
|
+
FileSet.glob_require('padrino-mailer/**/*.rb', __FILE__)
|
10
12
|
|
11
13
|
module Padrino
|
12
14
|
##
|
13
|
-
# This component uses
|
14
|
-
#
|
15
|
-
# There is full support for using an html content type as well as for file attachments.
|
15
|
+
# This component uses the 'mail' library to create a powerful but simple mailer system within Padrino (and Sinatra).
|
16
|
+
# There is full support for using plain or html content types as well as for attaching files.
|
16
17
|
# The MailerPlugin has many similarities to ActionMailer but is much lighterweight and (arguably) easier to use.
|
17
18
|
#
|
18
19
|
module Mailer
|
19
20
|
##
|
20
|
-
#
|
21
|
+
# Register
|
21
22
|
#
|
22
|
-
|
23
|
-
|
23
|
+
# Padrino::Mailer::Helpers
|
24
|
+
#
|
25
|
+
# for Padrino::Application
|
26
|
+
#
|
27
|
+
class << self
|
28
|
+
def registered(app)
|
29
|
+
app.helpers Padrino::Mailer::Helpers
|
30
|
+
end
|
31
|
+
alias :included :registered
|
24
32
|
end
|
25
33
|
end # Mailer
|
26
34
|
end # Padrino
|
data/lib/padrino-mailer/base.rb
CHANGED
@@ -3,100 +3,79 @@ module Padrino
|
|
3
3
|
##
|
4
4
|
# This is the abstract class that other mailers will inherit from in order to send mail
|
5
5
|
#
|
6
|
-
# You can set the default delivery settings through:
|
6
|
+
# You can set the default delivery settings from your app through:
|
7
7
|
#
|
8
|
-
#
|
9
|
-
# :
|
10
|
-
# :port
|
11
|
-
# :
|
12
|
-
# :
|
13
|
-
# :
|
14
|
-
# :domain
|
8
|
+
# set :delivery_method, :smtp => {
|
9
|
+
# :address => 'smtp.yourserver.com',
|
10
|
+
# :port => '25',
|
11
|
+
# :user_name => 'user',
|
12
|
+
# :password => 'pass',
|
13
|
+
# :authentication => :plain # :plain, :login, :cram_md5, no auth by default
|
14
|
+
# :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
15
15
|
# }
|
16
16
|
#
|
17
|
+
# or sendmail:
|
18
|
+
#
|
19
|
+
# set :delivery_method, :sendmail
|
20
|
+
#
|
21
|
+
# or for tests:
|
22
|
+
#
|
23
|
+
# set :delivery_method, :test
|
24
|
+
#
|
17
25
|
# and then all delivered mail will use these settings unless otherwise specified.
|
18
26
|
#
|
19
27
|
class Base
|
20
|
-
|
21
|
-
# Returns the available mail fields when composing a message
|
22
|
-
#
|
23
|
-
def self.mail_fields
|
24
|
-
[:to, :cc, :bcc, :reply_to, :from, :subject, :content_type, :charset, :via, :attachments, :template]
|
25
|
-
end
|
26
|
-
|
27
|
-
@@views_path = []
|
28
|
-
cattr_accessor :smtp_settings
|
29
|
-
cattr_accessor :views_path
|
30
|
-
attr_accessor :mail_attributes
|
31
|
-
|
32
|
-
def initialize(mail_name=nil) #:nodoc:
|
33
|
-
@mail_name = mail_name
|
34
|
-
@mail_attributes = {}
|
35
|
-
end
|
36
|
-
|
37
|
-
##
|
38
|
-
# Defines a method allowing mail attributes to be set into a hash for use when delivering
|
39
|
-
#
|
40
|
-
self.mail_fields.each do |field|
|
41
|
-
define_method(field) { |value| @mail_attributes[field] = value }
|
42
|
-
end
|
43
|
-
|
44
|
-
##
|
45
|
-
# Assigns the body key to the mail attributes either with the rendered body from a template or the given string value
|
46
|
-
#
|
47
|
-
def body(body_value)
|
48
|
-
final_template = template_path
|
49
|
-
raise "Template for '#{@mail_name}' could not be located in views path!" unless final_template
|
50
|
-
@mail_attributes[:body] = Tilt.new(final_template).render(self, body_value.symbolize_keys) if body_value.is_a?(Hash)
|
51
|
-
@mail_attributes[:body] = body_value if body_value.is_a?(String)
|
52
|
-
end
|
28
|
+
attr_accessor :delivery_settings, :app, :mailer_name, :messages
|
53
29
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
return template if template
|
61
|
-
end
|
30
|
+
def initialize(app, name, &block) #:nodoc:
|
31
|
+
@mailer_name = name
|
32
|
+
@messages = {}
|
33
|
+
@defaults = {}
|
34
|
+
@app = app
|
35
|
+
instance_eval(&block)
|
62
36
|
end
|
63
37
|
|
64
38
|
##
|
65
|
-
#
|
66
|
-
# mail_name corresponds to the name of a defined method within the mailer class
|
39
|
+
# Defines a mailer object allowing the definition of various email messages that can be delivered
|
67
40
|
#
|
68
41
|
# ==== Examples
|
69
42
|
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
##
|
79
|
-
# Returns true if a mail exists with the name being delivered
|
43
|
+
# email :birthday do |name, age|
|
44
|
+
# subject "Happy Birthday!"
|
45
|
+
# to 'john@fake.com'
|
46
|
+
# from 'noreply@birthday.com'
|
47
|
+
# locals 'name' => name, 'age' => age
|
48
|
+
# render 'birthday'
|
49
|
+
# end
|
80
50
|
#
|
81
|
-
def
|
82
|
-
|
51
|
+
def email(name, &block)
|
52
|
+
raise "The email '#{name}' is already defined" if self.messages[name].present?
|
53
|
+
self.messages[name] = Proc.new { |*attrs|
|
54
|
+
message = Mail::Message.new(self.app)
|
55
|
+
message.defaults = self.defaults if self.defaults.any?
|
56
|
+
message.delivery_method(*delivery_settings)
|
57
|
+
message.instance_exec(*attrs, &block)
|
58
|
+
message
|
59
|
+
}
|
83
60
|
end
|
84
|
-
|
85
|
-
|
86
|
-
#
|
87
|
-
#
|
61
|
+
alias :message :email
|
62
|
+
|
63
|
+
# Defines the default attributes for a message in this mailer (including app-wide defaults)
|
64
|
+
#
|
65
|
+
# ==== Examples
|
88
66
|
#
|
89
|
-
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
67
|
+
# mailer :alternate do
|
68
|
+
# defaults :from => 'padrino@from.com', :to => 'padrino@to.com'
|
69
|
+
# email(:foo) do ... end
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
def defaults(attributes=nil)
|
73
|
+
if attributes.nil? # Retrieve the default values
|
74
|
+
@app.respond_to?(:mailer_defaults) ? @app.mailer_defaults.merge(@defaults) : @defaults
|
75
|
+
else # updates the default values
|
76
|
+
@defaults = attributes
|
99
77
|
end
|
78
|
+
end
|
100
79
|
end # Base
|
101
80
|
end # Mailer
|
102
|
-
end # Padrino
|
81
|
+
end # Padrino
|
@@ -0,0 +1,231 @@
|
|
1
|
+
module Mail
|
2
|
+
class Message
|
3
|
+
include Sinatra::Templates
|
4
|
+
include Padrino::Rendering if defined?(Padrino::Rendering)
|
5
|
+
|
6
|
+
def initialize_with_app(*args, &block)
|
7
|
+
@template_cache = Tilt::Cache.new
|
8
|
+
# Check if we have an app passed into initialize
|
9
|
+
if args[0].respond_to?(:views) && args[0].respond_to?(:reload_templates?)
|
10
|
+
app = args.shift
|
11
|
+
settings.views = File.join(app.views, 'mailers')
|
12
|
+
settings.reload_templates = app.reload_templates?
|
13
|
+
else
|
14
|
+
# Set a default view for this class
|
15
|
+
settings.views = File.expand_path("./mailers")
|
16
|
+
settings.reload_templates = true
|
17
|
+
end
|
18
|
+
|
19
|
+
# Run the original initialize
|
20
|
+
initialize_without_app(*args, &block)
|
21
|
+
end
|
22
|
+
alias_method_chain :initialize, :app
|
23
|
+
|
24
|
+
##
|
25
|
+
# Setup like in Sinatra/Padrino apps content_type and template lookup.
|
26
|
+
#
|
27
|
+
# ==== Examples
|
28
|
+
#
|
29
|
+
# # This add a email plain part if a template called bar.plain.* is found
|
30
|
+
# # and a html part if a template called bar.html.* is found
|
31
|
+
# email do
|
32
|
+
# from 'from@email.com'
|
33
|
+
# to 'to@email.com'
|
34
|
+
# subject 'Welcome here'
|
35
|
+
# provides :plain, :html
|
36
|
+
# render "foo/bar"
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
def provides(*formats)
|
40
|
+
if formats.empty?
|
41
|
+
@_provides ||= []
|
42
|
+
else
|
43
|
+
@_provides = formats.flatten.compact
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Helper to add a text part to a multipart/alternative email. If this and
|
49
|
+
# html_part are both defined in a message, then it will be a multipart/alternative
|
50
|
+
# message and set itself that way.
|
51
|
+
#
|
52
|
+
# ==== Examples
|
53
|
+
#
|
54
|
+
# text_part "Some text"
|
55
|
+
# text_part { render('multipart/basic.text') }
|
56
|
+
#
|
57
|
+
def text_part(value=nil, &block)
|
58
|
+
if block_given? || value
|
59
|
+
@text_part = self.part(:content_type => "text/plain", :body => value, :part_block => block)
|
60
|
+
add_multipart_alternate_header unless html_part.blank?
|
61
|
+
else
|
62
|
+
@text_part || find_first_mime_type("text/plain")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Helper to add a html part to a multipart/alternative email. If this and
|
68
|
+
# text_part are both defined in a message, then it will be a multipart/alternative
|
69
|
+
# message and set itself that way.
|
70
|
+
#
|
71
|
+
# ==== Examples
|
72
|
+
#
|
73
|
+
# html_part "Some <b>Html</b> text"
|
74
|
+
# html_part { render('multipart/basic.html') }
|
75
|
+
#
|
76
|
+
def html_part(value=nil, &block)
|
77
|
+
if block_given? || value
|
78
|
+
@html_part = self.part(:content_type => "text/html", :body => value, :part_block => block)
|
79
|
+
add_multipart_alternate_header unless text_part.blank?
|
80
|
+
else
|
81
|
+
@html_part || find_first_mime_type("text/html")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# Allows you to add a part in block form to an existing mail message object
|
87
|
+
#
|
88
|
+
# ==== Examples
|
89
|
+
#
|
90
|
+
# mail = Mail.new do
|
91
|
+
# part :content_type => "multipart/alternative", :content_disposition => "inline" do |p|
|
92
|
+
# p.part :content_type => "text/plain", :body => "test text\nline #2"
|
93
|
+
# p.part :content_type => "text/html", :body => "<b>test</b> HTML<br/>\nline #2"
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
def part(params = {}, &block)
|
98
|
+
part_block = params.delete(:part_block)
|
99
|
+
new_part = Mail::Part.new(params)
|
100
|
+
new_part.settings.views = settings.views
|
101
|
+
new_part.settings.reload_templates = settings.reload_templates?
|
102
|
+
new_part.instance_eval(&part_block) if part_block
|
103
|
+
yield new_part if block_given?
|
104
|
+
add_part(new_part)
|
105
|
+
end
|
106
|
+
|
107
|
+
def do_delivery_with_logging
|
108
|
+
logger.debug "Sending email to: #{destinations.join(" ")}"
|
109
|
+
encoded.to_lf.split("\n").each { |line| logger << (" " + line) } if logger.debug?
|
110
|
+
do_delivery_without_logging
|
111
|
+
end
|
112
|
+
alias_method_chain :do_delivery, :logging if Padrino.respond_to?(:logger)
|
113
|
+
|
114
|
+
##
|
115
|
+
# Sinatra and Padrino compatibility
|
116
|
+
#
|
117
|
+
def settings
|
118
|
+
self.class
|
119
|
+
end
|
120
|
+
alias :options :settings
|
121
|
+
|
122
|
+
##
|
123
|
+
# Sets the message defined template path to the given view path
|
124
|
+
#
|
125
|
+
def views(value)
|
126
|
+
settings.views = value
|
127
|
+
end
|
128
|
+
|
129
|
+
##
|
130
|
+
# Sets the local variables available within the message template
|
131
|
+
#
|
132
|
+
def locals(value)
|
133
|
+
@_locals = value
|
134
|
+
end
|
135
|
+
|
136
|
+
##
|
137
|
+
# Returns the templates for this message
|
138
|
+
#
|
139
|
+
def self.templates
|
140
|
+
@_templates ||= {}
|
141
|
+
end
|
142
|
+
|
143
|
+
##
|
144
|
+
# Sets the message defined template path to the given view path
|
145
|
+
#
|
146
|
+
def self.views=(value)
|
147
|
+
@_views = value
|
148
|
+
end
|
149
|
+
|
150
|
+
##
|
151
|
+
# Returns the template view path defined for this message
|
152
|
+
#
|
153
|
+
def self.views
|
154
|
+
@_views
|
155
|
+
end
|
156
|
+
|
157
|
+
##
|
158
|
+
# Modify whether templates should be reloaded (for development)
|
159
|
+
#
|
160
|
+
def self.reload_templates=(value)
|
161
|
+
@_reload_templates = value
|
162
|
+
end
|
163
|
+
|
164
|
+
##
|
165
|
+
# Returns true if the templates will be reloaded; false otherwise.
|
166
|
+
#
|
167
|
+
def self.reload_templates?
|
168
|
+
@_reload_templates
|
169
|
+
end
|
170
|
+
|
171
|
+
##
|
172
|
+
# Return the path of this file, only for compatiblity with sinatra rendering methods
|
173
|
+
#
|
174
|
+
def self.caller_locations
|
175
|
+
[[File.dirname(__FILE__), 1]]
|
176
|
+
end
|
177
|
+
|
178
|
+
##
|
179
|
+
# Modify the default attributes for this message (if not explicitly specified)
|
180
|
+
#
|
181
|
+
def defaults=(attributes)
|
182
|
+
@_defaults = attributes
|
183
|
+
@_defaults.each_pair { |k, v| default(k.to_sym, v) } if @_defaults.is_a?(Hash)
|
184
|
+
end
|
185
|
+
|
186
|
+
# Shortcut for delivery_method with smarter smtp overwrites
|
187
|
+
def via(method = nil, settings = {})
|
188
|
+
if method.nil?
|
189
|
+
delivery_method
|
190
|
+
elsif method.to_sym != :smtp
|
191
|
+
delivery_method(method, settings)
|
192
|
+
elsif method.to_sym == :smtp && (settings.any? || delivery_method.class.to_s !~ /smtp/i)
|
193
|
+
delivery_method(method, settings)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
##
|
198
|
+
# If the value is empty return a symbol that rappresent the content type so:
|
199
|
+
#
|
200
|
+
# "text/plain" => :plain
|
201
|
+
#
|
202
|
+
# See Padrino::Mailer::Mime for more usage informations.
|
203
|
+
#
|
204
|
+
def content_type_with_symbol(value=nil)
|
205
|
+
value = Padrino::Mailer::Mime::MIME_TYPES.find { |k,v| v == value }[0] rescue value if value.is_a?(Symbol)
|
206
|
+
mime = content_type_without_symbol(value)
|
207
|
+
Padrino::Mailer::Mime.mime_type(mime)
|
208
|
+
end
|
209
|
+
alias_method_chain :content_type, :symbol
|
210
|
+
|
211
|
+
private
|
212
|
+
|
213
|
+
# Defines the render for the mailer utilizing the padrino 'rendering' module
|
214
|
+
def render(engine, data=nil, options={}, locals={}, &block)
|
215
|
+
locals = @_locals if options[:locals].blank? && locals.blank?
|
216
|
+
# Reload templates
|
217
|
+
@template_cache.clear if settings.reload_templates?
|
218
|
+
# Setup provides
|
219
|
+
provides.each do |format|
|
220
|
+
part do |p|
|
221
|
+
p.content_type(format)
|
222
|
+
p.send(:render, engine, data, options, locals, &block)
|
223
|
+
add_multipart_alternate_header unless html_part.blank?
|
224
|
+
end
|
225
|
+
end
|
226
|
+
# Setup the body if we don't have provides
|
227
|
+
self.body = super(engine, data, options, locals, &block) if provides.empty?
|
228
|
+
end
|
229
|
+
|
230
|
+
end # Message
|
231
|
+
end # Mail
|