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
@@ -1,65 +0,0 @@
|
|
1
|
-
module Padrino
|
2
|
-
module Mailer
|
3
|
-
##
|
4
|
-
# This represents a particular mail object which will need to be sent
|
5
|
-
# A mail_object requires the mail attributes and the delivery_settings
|
6
|
-
#
|
7
|
-
class MailObject
|
8
|
-
##
|
9
|
-
# Initialize a new MailObject
|
10
|
-
#
|
11
|
-
# ==== Examples
|
12
|
-
#
|
13
|
-
# Padrino::Mailer::MailObject.new(
|
14
|
-
# :subject => "Hey this is my subject",
|
15
|
-
# :to => "info@padrinorb.org",
|
16
|
-
# :from => "foo@bar.com",
|
17
|
-
# :body => "This is my body"
|
18
|
-
# ).deliver
|
19
|
-
#
|
20
|
-
def initialize(mail_attributes={}, smtp_settings={})
|
21
|
-
@mail_attributes = mail_attributes.dup
|
22
|
-
@smtp_settings = smtp_settings.dup if smtp_settings.present?
|
23
|
-
end
|
24
|
-
|
25
|
-
##
|
26
|
-
# Constructs the delivery attributes for the message and then sends the mail
|
27
|
-
#
|
28
|
-
# ==== Examples
|
29
|
-
#
|
30
|
-
# @mail_object.deliver
|
31
|
-
#
|
32
|
-
def deliver
|
33
|
-
@mail_attributes.reverse_merge!(:via => self.delivery_method.to_sym)
|
34
|
-
@mail_attributes.reverse_merge!(:smtp => @smtp_settings) if using_smtp?
|
35
|
-
self.send_mail(@mail_attributes)
|
36
|
-
end
|
37
|
-
|
38
|
-
protected
|
39
|
-
##
|
40
|
-
# Returns the delivery method to use for this mail object
|
41
|
-
#
|
42
|
-
# ==== Examples
|
43
|
-
#
|
44
|
-
# @mo.delivery_method => :smtp || :sendmail
|
45
|
-
#
|
46
|
-
def delivery_method
|
47
|
-
@mail_attributes[:via] || (@smtp_settings.present? ? :smtp : :sendmail)
|
48
|
-
end
|
49
|
-
|
50
|
-
##
|
51
|
-
# Returns true if the mail object is going to be delivered using smtp
|
52
|
-
#
|
53
|
-
def using_smtp?
|
54
|
-
delivery_method.to_s =~ /smtp/
|
55
|
-
end
|
56
|
-
|
57
|
-
##
|
58
|
-
# Performs the actual email sending
|
59
|
-
#
|
60
|
-
def send_mail(delivery_attributes)
|
61
|
-
Delivery.mail(delivery_attributes) && true
|
62
|
-
end
|
63
|
-
end # MailObject
|
64
|
-
end # Mailer
|
65
|
-
end # Padrino
|
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'sinatra/base'
|
2
|
-
require 'haml'
|
3
|
-
|
4
|
-
class MailerDemo < Sinatra::Base
|
5
|
-
configure do
|
6
|
-
set :root, File.dirname(__FILE__)
|
7
|
-
set :smtp_settings, {
|
8
|
-
:host => 'smtp.gmail.com',
|
9
|
-
:port => '587',
|
10
|
-
:tls => true,
|
11
|
-
:user => 'user',
|
12
|
-
:pass => 'pass',
|
13
|
-
:auth => :plain
|
14
|
-
}
|
15
|
-
end
|
16
|
-
|
17
|
-
register Padrino::Mailer
|
18
|
-
|
19
|
-
class SampleMailer < Padrino::Mailer::Base
|
20
|
-
def birthday_message(name, age)
|
21
|
-
subject "Happy Birthday!"
|
22
|
-
to 'john@fake.com'
|
23
|
-
from 'noreply@birthday.com'
|
24
|
-
body 'name' => name, 'age' => age
|
25
|
-
via :smtp
|
26
|
-
end
|
27
|
-
|
28
|
-
def anniversary_message(names, years_married)
|
29
|
-
subject "Happy anniversary!"
|
30
|
-
to 'julie@fake.com'
|
31
|
-
from 'noreply@anniversary.com'
|
32
|
-
body 'names' => names, 'years_married' => years_married
|
33
|
-
content_type 'text/html'
|
34
|
-
end
|
35
|
-
|
36
|
-
def welcome_message(name)
|
37
|
-
template 'sample_mailer/foo_message'
|
38
|
-
subject "Welcome Message!"
|
39
|
-
to 'john@fake.com'
|
40
|
-
from 'noreply@custom.com'
|
41
|
-
body 'name' => name
|
42
|
-
via :smtp
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
post "/deliver/plain" do
|
47
|
-
result = SampleMailer.deliver_birthday_message("Joey", 21)
|
48
|
-
result ? "mail delivered" : 'mail not delivered'
|
49
|
-
end
|
50
|
-
|
51
|
-
post "/deliver/custom" do
|
52
|
-
result = SampleMailer.deliver_welcome_message("Bobby")
|
53
|
-
result ? "mail delivered" : 'mail not delivered'
|
54
|
-
end
|
55
|
-
|
56
|
-
post "/deliver/html" do
|
57
|
-
result = SampleMailer.deliver_anniversary_message("Joey & Charlotte", 16)
|
58
|
-
result ? "mail delivered" : 'mail not delivered'
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
class MailerUser
|
63
|
-
|
64
|
-
end
|
data/test/test_base.rb
DELETED
@@ -1,86 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
-
|
3
|
-
class DemoMailer < Padrino::Mailer::Base
|
4
|
-
def sample_mail
|
5
|
-
from 'test@default.com'
|
6
|
-
cc 'foo@bar.com'
|
7
|
-
bcc 'bar@foo.com'
|
8
|
-
to 'test@test.com'
|
9
|
-
reply_to 'foobar@foobar.com'
|
10
|
-
body "Hello world!"
|
11
|
-
via :sendmail
|
12
|
-
end
|
13
|
-
|
14
|
-
def sample_mail_smtp
|
15
|
-
from 'test@default.com'
|
16
|
-
to 'test@test.com'
|
17
|
-
body "SMTP Hello world!"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class TestMailerBase < Test::Unit::TestCase
|
22
|
-
include Padrino::Mailer
|
23
|
-
|
24
|
-
context 'for defining email attributes' do
|
25
|
-
DemoMailer.mail_fields.each do |field|
|
26
|
-
should "support setting '#{field}' attribute" do
|
27
|
-
demo_mailer = DemoMailer.new(:sample_mail)
|
28
|
-
demo_mailer.send(field, "some_value")
|
29
|
-
assert_equal({ field => "some_value" }, demo_mailer.mail_attributes)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
should "allow defining text body" do
|
34
|
-
demo_mailer = DemoMailer.new(:sample_mail)
|
35
|
-
demo_mailer.body("Hello world!")
|
36
|
-
assert_equal({ :body => "Hello world!" }, demo_mailer.mail_attributes)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context 'for retrieving template path' do
|
41
|
-
should "return correct path" do
|
42
|
-
demo_mailer = DemoMailer.new(:sample_mail)
|
43
|
-
assert_match %r{demo_mailer/sample_mail.erb}, demo_mailer.template_path
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
context 'for #deliver class method' do
|
48
|
-
should "perform the email delivery for sendmail" do
|
49
|
-
Delivery.expects(:mail).with(:from => 'test@default.com', :to => 'test@test.com', :body => "Hello world!", :via => :sendmail,
|
50
|
-
:cc => 'foo@bar.com', :bcc => 'bar@foo.com', :reply_to => 'foobar@foobar.com')
|
51
|
-
DemoMailer.deliver(:sample_mail)
|
52
|
-
end
|
53
|
-
|
54
|
-
should "perform the email delivery for smtp" do
|
55
|
-
DemoMailer.smtp_settings = { :host => 'smtp.arcadic.com' }
|
56
|
-
Delivery.expects(:mail).with(:from => 'test@default.com', :to => 'test@test.com',
|
57
|
-
:body => "SMTP Hello world!", :via => :smtp, :smtp => { :host => 'smtp.arcadic.com' })
|
58
|
-
DemoMailer.deliver(:sample_mail_smtp)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'for #respond_to? class method' do
|
63
|
-
should "respond as true for any delivery method calls for mails that exist" do
|
64
|
-
assert DemoMailer.respond_to?(:deliver_sample_mail)
|
65
|
-
end
|
66
|
-
|
67
|
-
should "respond as false for any delivery method calls for mails that don't exist" do
|
68
|
-
assert_equal false, DemoMailer.respond_to?(:deliver_faker_mail)
|
69
|
-
end
|
70
|
-
|
71
|
-
should "respond as true for any non-delivery methods that exist" do
|
72
|
-
assert DemoMailer.respond_to?(:inspect)
|
73
|
-
end
|
74
|
-
|
75
|
-
should "respond as false for any non-delivery methods that don't exist" do
|
76
|
-
assert_equal false, DemoMailer.respond_to?(:fake_method)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'for #method_missing dynamic delivery' do
|
81
|
-
should 'invoke deliver method with appropriate parameters' do
|
82
|
-
DemoMailer.expects(:deliver).with("example_name", "test", 5)
|
83
|
-
DemoMailer.deliver_example_name("test", 5)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
data/test/test_mail_object.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
-
|
3
|
-
class TestMailObject < Test::Unit::TestCase
|
4
|
-
include Padrino::Mailer
|
5
|
-
|
6
|
-
context 'for #deliver method' do
|
7
|
-
should "send mail with attributes default to sendmail no smtp" do
|
8
|
-
mail_object = Padrino::Mailer::MailObject.new(:to => "test@john.com", :from => "sender@sent.com", :body => "Hello")
|
9
|
-
Delivery.expects(:mail).with(:to => "test@john.com", :from => "sender@sent.com", :body => "Hello", :via => :sendmail)
|
10
|
-
mail_object.deliver
|
11
|
-
end
|
12
|
-
|
13
|
-
should "send mail with attributes default to smtp if set" do
|
14
|
-
mail_object = Padrino::Mailer::MailObject.new({:to => "test@john.com", :body => "Hello"}, { :host => 'smtp.gmail.com' })
|
15
|
-
Delivery.expects(:mail).with(:to => "test@john.com", :body => "Hello", :via => :smtp, :smtp => { :host => 'smtp.gmail.com' })
|
16
|
-
mail_object.deliver
|
17
|
-
end
|
18
|
-
|
19
|
-
should "send mail with attributes use sendmail if explicit" do
|
20
|
-
mail_object = Padrino::Mailer::MailObject.new({:to => "test@john.com", :via => :sendmail }, { :host => 'smtp.gmail.com' })
|
21
|
-
Delivery.expects(:mail).with(:to => "test@john.com", :via => :sendmail)
|
22
|
-
mail_object.deliver
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|