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
@@ -0,0 +1,125 @@
|
|
1
|
+
module Padrino
|
2
|
+
module Mailer
|
3
|
+
module Helpers
|
4
|
+
def self.included(base) #:nodoc:
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
##
|
9
|
+
# Delivers an email with the given mail attributes (to, from, subject, cc, bcc, body, et.al)
|
10
|
+
#
|
11
|
+
# ==== Examples
|
12
|
+
#
|
13
|
+
# email do
|
14
|
+
# to @user.email
|
15
|
+
# from "awesomeness@example.com",
|
16
|
+
# subject "Welcome to Awesomeness!"
|
17
|
+
# locals :a => a, :b => b
|
18
|
+
# render 'path/to/my/template'
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
def email(mail_attributes={}, &block)
|
22
|
+
settings.email(mail_attributes, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Delivers a mailer message email with the given attributes
|
27
|
+
#
|
28
|
+
# ==== Examples
|
29
|
+
#
|
30
|
+
# deliver(:sample, :birthday, "Joey", 21)
|
31
|
+
# deliver(:example, :message, "John")
|
32
|
+
#
|
33
|
+
def deliver(mailer_name, message_name, *attributes)
|
34
|
+
settings.deliver(mailer_name, message_name, *attributes)
|
35
|
+
end
|
36
|
+
|
37
|
+
module ClassMethods
|
38
|
+
def inherited(subclass) #:nodoc:
|
39
|
+
@_registered_mailers ||= {}
|
40
|
+
super(subclass)
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Returns all registered mailers for this application
|
45
|
+
#
|
46
|
+
def registered_mailers
|
47
|
+
@_registered_mailers ||= {}
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Defines a mailer object allowing the definition of various email messages that can be delivered
|
52
|
+
#
|
53
|
+
# ==== Examples
|
54
|
+
#
|
55
|
+
# mailer :sample do
|
56
|
+
# email :birthday do |name, age|
|
57
|
+
# subject 'Happy Birthday!'
|
58
|
+
# to 'john@fake.com'
|
59
|
+
# from 'noreply@birthday.com'
|
60
|
+
# locals :name => name, :age => age
|
61
|
+
# render 'sample/birthday'
|
62
|
+
# end
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
def mailer(name, &block)
|
66
|
+
mailer = Padrino::Mailer::Base.new(self, name, &block)
|
67
|
+
mailer.delivery_settings = delivery_settings
|
68
|
+
registered_mailers[name] = mailer
|
69
|
+
mailer
|
70
|
+
end
|
71
|
+
alias :mailers :mailer
|
72
|
+
|
73
|
+
##
|
74
|
+
# Delivers a mailer message email with the given attributes
|
75
|
+
#
|
76
|
+
# ==== Examples
|
77
|
+
#
|
78
|
+
# deliver(:sample, :birthday, "Joey", 21)
|
79
|
+
# deliver(:example, :message, "John")
|
80
|
+
#
|
81
|
+
def deliver(mailer_name, message_name, *attributes)
|
82
|
+
registered_mailers[mailer_name].messages[message_name].call(*attributes).deliver
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# Delivers an email with the given mail attributes (to, from, subject, cc, bcc, body, et.al) using settings of
|
87
|
+
# the given app.
|
88
|
+
#
|
89
|
+
# ==== Examples
|
90
|
+
#
|
91
|
+
# MyApp.email(:to => 'to@ma.il', :from => 'from@ma.il', :subject => 'Welcome!', :body => 'Welcome Here!')
|
92
|
+
#
|
93
|
+
# # or if you prefer blocks
|
94
|
+
#
|
95
|
+
# MyApp.email do
|
96
|
+
# to @user.email
|
97
|
+
# from "awesomeness@example.com",
|
98
|
+
# subject "Welcome to Awesomeness!"
|
99
|
+
# body 'path/to/my/template', :locals => { :a => a, :b => b }
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
def email(mail_attributes={}, &block)
|
103
|
+
message = Mail::Message.new(self)
|
104
|
+
message.delivery_method(*delivery_settings)
|
105
|
+
message.instance_eval(&block) if block_given?
|
106
|
+
mail_attributes.each_pair { |k, v| message.method(k).call(v) }
|
107
|
+
message.deliver
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
##
|
112
|
+
# Return the parsed delivery method
|
113
|
+
#
|
114
|
+
def delivery_settings
|
115
|
+
@_delivery_setting ||= begin
|
116
|
+
return [:sendmail, { :location => `which sendmail`.chomp }] unless respond_to?(:delivery_method)
|
117
|
+
return [delivery_method.keys[0], delivery_method.values[0]] if delivery_method.is_a?(Hash)
|
118
|
+
return [delivery_method, {}] if delivery_method.is_a?(Symbol)
|
119
|
+
[nil, {}]
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end # Helpers
|
124
|
+
end # Mailer
|
125
|
+
end # Padrino
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Padrino
|
2
|
+
module Mailer
|
3
|
+
module Mime
|
4
|
+
|
5
|
+
##
|
6
|
+
# Returns Symbol with mime type if found, otherwise use +fallback+.
|
7
|
+
# +mime+ should be the content type like "text/plain"
|
8
|
+
# +fallback+ may be any symbol
|
9
|
+
#
|
10
|
+
# Also see the documentation for MIME_TYPES
|
11
|
+
#
|
12
|
+
# ==== Examples
|
13
|
+
#
|
14
|
+
# => :plain
|
15
|
+
# Padrino::Mailer::Mime.mime_type('text/plain')
|
16
|
+
# => :html
|
17
|
+
# Padrino::Mailer::Mime.mime_type('text/html')
|
18
|
+
#
|
19
|
+
# This is a shortcut for:
|
20
|
+
#
|
21
|
+
# Padrino::Mailer::Mime::MIME_TYPES.fetch('text/plain', :plain)
|
22
|
+
#
|
23
|
+
def self.mime_type(mime, fallback=:plain)
|
24
|
+
MIME_TYPES.fetch(mime.to_s.downcase, fallback)
|
25
|
+
end
|
26
|
+
|
27
|
+
# List of most common mime-types, selected various sources
|
28
|
+
# according to their usefulness in a emailg scope for Ruby
|
29
|
+
# users.
|
30
|
+
#
|
31
|
+
# You can add your own mime types like:
|
32
|
+
#
|
33
|
+
# Padrino::Mailer::MIME_TYPES.merge!("text/xml" => :xml)
|
34
|
+
#
|
35
|
+
MIME_TYPES = {
|
36
|
+
"text/html" => :html,
|
37
|
+
"text/plain" => :plain,
|
38
|
+
"text/xml" => :xml
|
39
|
+
}
|
40
|
+
end # Mime
|
41
|
+
end # Mailer
|
42
|
+
end # Padrino
|
data/padrino-mailer.gemspec
CHANGED
@@ -1,77 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
# -*- encoding: utf-8 -*-
|
1
|
+
require File.expand_path("../../padrino-core/lib/padrino-core/version.rb", __FILE__)
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler'
|
5
4
|
|
6
5
|
Gem::Specification.new do |s|
|
7
6
|
s.name = %q{padrino-mailer}
|
8
|
-
s.
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
7
|
+
s.rubyforge_project = %q{padrino-mailer}
|
11
8
|
s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
|
12
|
-
s.date = %q{2010-04-22}
|
13
|
-
s.description = %q{Mailer system for padrino allowing easy delivery of application emails}
|
14
9
|
s.email = %q{padrinorb@gmail.com}
|
15
|
-
s.
|
16
|
-
"README.rdoc"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
".document",
|
20
|
-
".gitignore",
|
21
|
-
"LICENSE",
|
22
|
-
"README.rdoc",
|
23
|
-
"Rakefile",
|
24
|
-
"lib/padrino-mailer.rb",
|
25
|
-
"lib/padrino-mailer/base.rb",
|
26
|
-
"lib/padrino-mailer/delivery.rb",
|
27
|
-
"lib/padrino-mailer/mail_object.rb",
|
28
|
-
"padrino-mailer.gemspec",
|
29
|
-
"test/fixtures/mailer_app/app.rb",
|
30
|
-
"test/fixtures/mailer_app/views/demo_mailer/sample_mail.erb",
|
31
|
-
"test/fixtures/mailer_app/views/sample_mailer/anniversary_message.erb",
|
32
|
-
"test/fixtures/mailer_app/views/sample_mailer/birthday_message.erb",
|
33
|
-
"test/fixtures/mailer_app/views/sample_mailer/foo_message.erb",
|
34
|
-
"test/helper.rb",
|
35
|
-
"test/test_base.rb",
|
36
|
-
"test/test_mail_object.rb",
|
37
|
-
"test/test_padrino_mailer.rb"
|
38
|
-
]
|
10
|
+
s.summary = %q{Mailer system for padrino}
|
39
11
|
s.homepage = %q{http://github.com/padrino/padrino-framework/tree/master/padrino-mailer}
|
12
|
+
s.description = %q{Mailer system for padrino allowing easy delivery of application emails}
|
13
|
+
s.required_rubygems_version = ">= 1.3.6"
|
14
|
+
s.version = Padrino.version
|
15
|
+
s.date = Time.now.strftime("%Y-%m-%d")
|
16
|
+
s.extra_rdoc_files = Dir["*.rdoc"]
|
17
|
+
s.files = %w(.document .gitignore LICENSE README.rdoc Rakefile padrino-mailer.gemspec) + Dir.glob("{bin,lib,test}/**/*")
|
40
18
|
s.rdoc_options = ["--charset=UTF-8"]
|
41
|
-
s.
|
42
|
-
s.
|
43
|
-
s.
|
44
|
-
|
45
|
-
|
46
|
-
if s.respond_to? :specification_version then
|
47
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
-
s.specification_version = 3
|
49
|
-
|
50
|
-
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
-
s.add_runtime_dependency(%q<padrino-core>, ["= 0.9.10"])
|
52
|
-
s.add_runtime_dependency(%q<tmail>, [">= 1.2"])
|
53
|
-
s.add_development_dependency(%q<shoulda>, [">= 2.10.3"])
|
54
|
-
s.add_development_dependency(%q<haml>, [">= 2.2.1"])
|
55
|
-
s.add_development_dependency(%q<mocha>, [">= 0.9.7"])
|
56
|
-
s.add_development_dependency(%q<rack-test>, [">= 0.5.0"])
|
57
|
-
s.add_development_dependency(%q<webrat>, [">= 0.5.1"])
|
58
|
-
else
|
59
|
-
s.add_dependency(%q<padrino-core>, ["= 0.9.10"])
|
60
|
-
s.add_dependency(%q<tmail>, [">= 1.2"])
|
61
|
-
s.add_dependency(%q<shoulda>, [">= 2.10.3"])
|
62
|
-
s.add_dependency(%q<haml>, [">= 2.2.1"])
|
63
|
-
s.add_dependency(%q<mocha>, [">= 0.9.7"])
|
64
|
-
s.add_dependency(%q<rack-test>, [">= 0.5.0"])
|
65
|
-
s.add_dependency(%q<webrat>, [">= 0.5.1"])
|
66
|
-
end
|
67
|
-
else
|
68
|
-
s.add_dependency(%q<padrino-core>, ["= 0.9.10"])
|
69
|
-
s.add_dependency(%q<tmail>, [">= 1.2"])
|
70
|
-
s.add_dependency(%q<shoulda>, [">= 2.10.3"])
|
71
|
-
s.add_dependency(%q<haml>, [">= 2.2.1"])
|
72
|
-
s.add_dependency(%q<mocha>, [">= 0.9.7"])
|
73
|
-
s.add_dependency(%q<rack-test>, [">= 0.5.0"])
|
74
|
-
s.add_dependency(%q<webrat>, [">= 0.5.1"])
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
19
|
+
s.require_path = 'lib'
|
20
|
+
s.add_runtime_dependency(%q<padrino-core>, ["= #{Padrino.version}"])
|
21
|
+
s.add_bundler_dependencies :mailer, :development
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
This is a body of text from a template
|
@@ -0,0 +1 @@
|
|
1
|
+
Layout Basic <%= yield %>
|
@@ -0,0 +1,69 @@
|
|
1
|
+
ENV['PADRINO_ENV'] = 'test'
|
2
|
+
PADRINO_ROOT = File.dirname(__FILE__) unless defined? PADRINO_ROOT
|
3
|
+
|
4
|
+
class PadrinoApp < Padrino::Application
|
5
|
+
register Padrino::Mailer
|
6
|
+
|
7
|
+
set :delivery_method, :smtp => {
|
8
|
+
:address => "smtp.gmail.com",
|
9
|
+
:port => 587,
|
10
|
+
:domain => 'your.host.name',
|
11
|
+
:user_name => '<username>',
|
12
|
+
:password => '<password>',
|
13
|
+
:authentication => 'plain',
|
14
|
+
:enable_starttls_auto => true
|
15
|
+
}
|
16
|
+
|
17
|
+
mailer :sample do
|
18
|
+
email :birthday do |name, age|
|
19
|
+
subject "Happy Birthday!"
|
20
|
+
to 'john@fake.com'
|
21
|
+
from 'noreply@birthday.com'
|
22
|
+
locals :name => name, :age => age
|
23
|
+
via :test
|
24
|
+
render 'sample/birthday'
|
25
|
+
end
|
26
|
+
|
27
|
+
email :anniversary do |names, years_married|
|
28
|
+
subject "Happy anniversary!"
|
29
|
+
to 'julie@fake.com'
|
30
|
+
from 'noreply@anniversary.com'
|
31
|
+
content_type :html
|
32
|
+
via :test
|
33
|
+
locals :names => names, :years_married => years_married
|
34
|
+
render 'sample/anniversary'
|
35
|
+
end
|
36
|
+
|
37
|
+
message :welcome do |name|
|
38
|
+
subject "Welcome Message!"
|
39
|
+
to 'john@fake.com'
|
40
|
+
from 'noreply@custom.com'
|
41
|
+
locals :name => name
|
42
|
+
via :test
|
43
|
+
render 'sample/foo_message'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
post "/deliver/inline" do
|
48
|
+
result = email(:to => "john@apple.com", :from => "joe@smith.com", :subject => "Test Email", :body => "Test Body", :via => :test)
|
49
|
+
result ? "mail delivered" : 'mail not delivered'
|
50
|
+
end
|
51
|
+
|
52
|
+
post "/deliver/plain" do
|
53
|
+
result = deliver(:sample, :birthday, "Joey", 21)
|
54
|
+
result ? "mail delivered" : 'mail not delivered'
|
55
|
+
end
|
56
|
+
|
57
|
+
post "/deliver/html" do
|
58
|
+
result = deliver(:sample, :anniversary, "Joey & Charlotte", 16)
|
59
|
+
result ? "mail delivered" : 'mail not delivered'
|
60
|
+
end
|
61
|
+
|
62
|
+
post "/deliver/custom" do
|
63
|
+
result = deliver(:sample, :welcome, "Bobby")
|
64
|
+
result ? "mail delivered" : 'mail not delivered'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
Padrino.mount_core("PadrinoApp")
|
69
|
+
Padrino.load!
|
data/test/fixtures/{mailer_app/views/demo_mailer → padrino_app/views/mailers/demo}/sample_mail.erb
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'haml'
|
3
|
+
|
4
|
+
class SinatraApp < Sinatra::Base
|
5
|
+
register Padrino::Mailer
|
6
|
+
|
7
|
+
set :root, File.dirname(__FILE__)
|
8
|
+
set :delivery_method, :smtp => {
|
9
|
+
:address => "smtp.gmail.com",
|
10
|
+
:port => 587,
|
11
|
+
:domain => 'your.host.name',
|
12
|
+
:user_name => '<username>',
|
13
|
+
:password => '<password>',
|
14
|
+
:authentication => 'plain',
|
15
|
+
:enable_starttls_auto => true
|
16
|
+
}
|
17
|
+
|
18
|
+
mailer :sample do
|
19
|
+
email :birthday do |name, age|
|
20
|
+
subject "Happy Birthday!"
|
21
|
+
to 'john@fake.com'
|
22
|
+
from 'noreply@birthday.com'
|
23
|
+
locals :name => name, :age => age
|
24
|
+
via :test
|
25
|
+
render 'sample/birthday'
|
26
|
+
end
|
27
|
+
|
28
|
+
email :anniversary do |names, years_married|
|
29
|
+
subject "Happy anniversary!"
|
30
|
+
to 'julie@fake.com'
|
31
|
+
from 'noreply@anniversary.com'
|
32
|
+
locals :names => names, :years_married => years_married
|
33
|
+
content_type :html
|
34
|
+
via :test
|
35
|
+
render 'sample/anniversary'
|
36
|
+
end
|
37
|
+
|
38
|
+
message :welcome do |name|
|
39
|
+
subject "Welcome Message!"
|
40
|
+
to 'john@fake.com'
|
41
|
+
from 'noreply@custom.com'
|
42
|
+
locals :name => name
|
43
|
+
via :test
|
44
|
+
render 'sample/foo_message'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
post "/deliver/inline" do
|
49
|
+
result = email(:to => "john@apple.com", :from => "joe@smith.com", :subject => "Test Email", :body => "Test Body", :via => :test)
|
50
|
+
result ? "mail delivered" : 'mail not delivered'
|
51
|
+
end
|
52
|
+
|
53
|
+
post "/deliver/plain" do
|
54
|
+
result = deliver(:sample, :birthday, "Joey", 21)
|
55
|
+
result ? "mail delivered" : 'mail not delivered'
|
56
|
+
end
|
57
|
+
|
58
|
+
post "/deliver/html" do
|
59
|
+
result = deliver(:sample, :anniversary, "Joey & Charlotte", 16)
|
60
|
+
result ? "mail delivered" : 'mail not delivered'
|
61
|
+
end
|
62
|
+
|
63
|
+
post "/deliver/custom" do
|
64
|
+
result = deliver(:sample, :welcome, "Bobby")
|
65
|
+
result ? "mail delivered" : 'mail not delivered'
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
This is a sample message
|
@@ -0,0 +1 @@
|
|
1
|
+
Hello to <%= name %>
|
@@ -0,0 +1 @@
|
|
1
|
+
This is a foo message in mailers/alternate dir
|
@@ -0,0 +1 @@
|
|
1
|
+
This is a bar message in mailers dir
|
@@ -0,0 +1 @@
|
|
1
|
+
Hello World
|
@@ -0,0 +1 @@
|
|
1
|
+
Salve Mondo
|
@@ -0,0 +1 @@
|
|
1
|
+
Layout Sample <%= yield %>
|