Skip to content
This repository was archived by the owner on Mar 2, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
metaclass (0.0.1)
mime-types (1.23)
mocha (0.14.0)
metaclass (~> 0.0.1)
polyglot (0.3.3)
rack (1.5.2)
rack-test (0.6.2)
Expand All @@ -28,6 +31,7 @@ PLATFORMS
DEPENDENCIES
mail (~> 2.2)
mail_view!
mocha
rack-test (~> 0.6)
rake
tmail (~> 1.2)
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ And run `bundle install`.
Usage
-----

Since most emails do something interesting with database data, you'll need to write some scenarios to load messages with fake data. Its similar to writing mailer unit tests but you see a visual representation of the output instead.
Since most emails do something interesting with database data, you'll need to write some scenarios to load messages with fake data. It's similar to writing mailer unit tests but you see a visual representation of the output instead.

```ruby
# app/mailers/mail_preview.rb or lib/mail_preview.rb
Expand Down Expand Up @@ -49,6 +49,8 @@ Since most emails do something interesting with database data, you'll need to wr

Methods must return a [Mail][1] or [TMail][2] object. Using ActionMailer, call `Notifier.create_action_name(args)` to return a compatible TMail object. Now on ActionMailer 3.x, `Notifier.action_name(args)` will return a Mail object.

To also mail an actual email, just add "[email protected]" to the URL query string. For example, if "http://.../invitation" is a MailPreview path, then "http://.../[email protected]" will display the MailPreview AND send an actual email to [email protected]. (Unless overridden, the email's from and to addresses will be the specified address.)

Routing
-------

Expand Down
16 changes: 16 additions & 0 deletions lib/mail_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def call(env)
if actions.include?(name) && !missing_format
mail = build_mail(name)

email_addr = get_email_address(request.params["email"])
if email_addr
sendable = mail.dup
sendable[:to] = email_addr
sendable[:from] ||= email_addr
sendable.deliver
end

# Requested a specific bare MIME part. Render it verbatim.
if part_type = request.params['part']
if part = find_part(mail, part_type)
Expand Down Expand Up @@ -126,4 +134,12 @@ def find_part(mail, matching_content_type)
mail
end
end

def get_email_address(params_email)
email_regex.match(params_email) ? params_email : nil
end

def email_regex
/.+@.+\..+/
end
end
1 change: 1 addition & 0 deletions mail_view.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'mail', '~> 2.2'
s.add_development_dependency 'tmail', '~> 1.2'
s.add_development_dependency 'rake'
s.add_development_dependency 'mocha'

s.files = %w(init.rb) + Dir['lib/**/*']
end
86 changes: 76 additions & 10 deletions test/test_mail_view.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'test/unit'
require 'rack/test'

require 'mocha/setup'
require 'mail_view'
require 'mail'
require 'tmail'
Expand Down Expand Up @@ -197,8 +198,7 @@ def test_message_header_uses_full_display_names
assert_match 'Another Peek <[email protected]>', unescaped_body
end

def test_html_message
get '/html_message'
def html_message_asserts
assert last_response.ok?
assert_match iframe_src_match('text/html'), last_response.body
assert_no_match %r(View as), last_response.body
Expand All @@ -208,8 +208,21 @@ def test_html_message
assert_equal '<h1>Hello</h1>', last_response.body
end

def test_nested_multipart_message
get '/nested_multipart_message'
def test_html_message
Net::SMTP.expects(:new).never
get '/html_message'
html_message_asserts
end

def test_html_message_with_email_addr
mock_smtp = mock()
Net::SMTP.expects(:new).returns(mock_smtp)
mock_smtp.expects(:start)
get '/[email protected]'
html_message_asserts
end

def nested_multipart_message_asserts
assert last_response.ok?
assert_match iframe_src_match('text/html'), last_response.body
assert_match %r(View as), last_response.body
Expand All @@ -219,8 +232,21 @@ def test_nested_multipart_message
assert_equal '<h1>Hello</h1>', last_response.body
end

def test_multipart_alternative
get '/multipart_alternative'
def test_nested_multipart_message
Net::SMTP.expects(:new).never
get '/nested_multipart_message'
nested_multipart_message_asserts
end

def test_nested_multipart_message_with_email_addr
mock_smtp = mock()
Net::SMTP.expects(:new).returns(mock_smtp)
mock_smtp.expects(:start)
get '/[email protected]'
nested_multipart_message_asserts
end

def multipart_alternative_asserts
assert last_response.ok?
assert_match iframe_src_match('text/html'), last_response.body
assert_match 'View as', last_response.body
Expand All @@ -230,8 +256,21 @@ def test_multipart_alternative
assert_equal '<h1>This is HTML</h1>', last_response.body
end

def test_multipart_alternative_as_html
get '/multipart_alternative.html'
def test_multipart_alternative
Net::SMTP.expects(:new).never
get '/multipart_alternative'
multipart_alternative_asserts
end

def test_multipart_alternative_with_email_addr
mock_smtp = mock()
Net::SMTP.expects(:new).returns(mock_smtp)
mock_smtp.expects(:start)
get '/[email protected]'
multipart_alternative_asserts
end

def multipart_alternative_as_html_asserts
assert last_response.ok?
assert_match iframe_src_match('text/html'), last_response.body
assert_match 'View as', last_response.body
Expand All @@ -241,8 +280,21 @@ def test_multipart_alternative_as_html
assert_equal '<h1>This is HTML</h1>', last_response.body
end

def test_multipart_alternative_as_text
get '/multipart_alternative.txt'
def test_multipart_alternative_as_html
Net::SMTP.expects(:new).never
get '/multipart_alternative.html'
multipart_alternative_as_html_asserts
end

def test_multipart_alternative_as_html_with_email_addr
mock_smtp = mock()
Net::SMTP.expects(:new).returns(mock_smtp)
mock_smtp.expects(:start)
get '/[email protected]'
multipart_alternative_as_html_asserts
end

def multipart_alternative_as_text_asserts
assert last_response.ok?
assert_match iframe_src_match('text/plain'), last_response.body
assert_match 'View as', last_response.body
Expand All @@ -252,6 +304,20 @@ def test_multipart_alternative_as_text
assert_equal 'This is plain text', last_response.body
end

def test_multipart_alternative_as_text
Net::SMTP.expects(:new).never
get '/multipart_alternative.txt'
multipart_alternative_as_text_asserts
end

def test_multipart_alternative_as_text_with_email_addr
mock_smtp = mock()
Net::SMTP.expects(:new).returns(mock_smtp)
mock_smtp.expects(:start)
get '/[email protected]'
multipart_alternative_as_text_asserts
end

def test_multipart_alternative_text_as_default
get '/multipart_alternative_text_default'
assert last_response.ok?
Expand Down