Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improving multipart support and refactored out Message class

  • Loading branch information...
commit 177fd2f67ef396ec5b8fcf403f9a5100d993e72d 1 parent 74cfa35
@ryanb authored
View
10 README.rdoc
@@ -4,11 +4,11 @@ Preview mail in your browser instead of sending it. When using the included deli
== Rails Setup
-Add the gem to your development environment and run the +bundle+ command.
+First add the gem to your development environment and run the +bundle+ command to install it.
gem "letter_opener", :group => :development
-And then set the delivery method in <tt>config/environments/development.rb</tt>
+Then set the delivery method in <tt>config/environments/development.rb</tt>
config.action_mailer.delivery_method = :letter_opener
@@ -17,7 +17,7 @@ Now any delivered mail will pop up in your browser. The messages are stored in <
== Other Setup
-This can be used with any environment that uses the Mail gem. Simply set the delivery method when configuring Mail and specify a location.
+This can be used with anything that uses the Mail gem. Simply set the delivery method when configuring Mail and specify a location.
require "letter_opener"
Mail.defaults do
@@ -27,6 +27,6 @@ This can be used with any environment that uses the Mail gem. Simply set the del
== Development & Feedback
-Questions or comments? Please use the {issue tracker}[https://github.com/ryanb/private_pub/issues]. If you would like to contribue to this project, clone this repository and run +bundle+ and +rake+ to run the tests.
+Questions or comments? Please use the {issue tracker}[https://github.com/ryanb/letter_opener/issues]. If you would like to contribute to this project, clone this repository and run +bundle+ and +rake+ to run the tests.
-Special thanks to the {mail_view}[https://github.com/37signals/mail_view/] gem for inspiring this project.
+Special thanks to the {mail_view}[https://github.com/37signals/mail_view/] gem for inspiring this project and for their mail template.
View
2  lib/letter_opener.rb
@@ -1,5 +1,7 @@
require "fileutils"
require "digest/sha1"
+require "cgi"
+require "letter_opener/message"
require "letter_opener/delivery_method"
require "letter_opener/railtie" if defined? Rails
View
12 lib/letter_opener/delivery_method.rb
@@ -5,13 +5,11 @@ def initialize(options = {})
end
def deliver!(mail)
- path = File.join(@options[:location], "#{Time.now.to_i}_#{Digest::SHA1.hexdigest(mail.encoded)[0..6]}.html")
- FileUtils.mkdir_p(@options[:location])
- File.open(path, 'w') do |f|
- template = File.expand_path("../views/index.html.erb", __FILE__)
- f.write ERB.new(File.read(template)).result(binding)
- end
- Launchy.open("file://#{path}")
+ location = File.join(@options[:location], "#{Time.now.to_i}_#{Digest::SHA1.hexdigest(mail.encoded)[0..6]}")
+ messages = mail.parts.map { |part| Message.new(location, mail, part) }
+ messages << Message.new(location, mail) if messages.empty?
+ messages.each { |message| message.render }
+ Launchy.open("file://#{messages.first.filepath}")
end
end
end
View
18 lib/letter_opener/views/index.html.erb → lib/letter_opener/message.html.erb
@@ -56,7 +56,7 @@
<div id="message_headers">
<dl>
<dt>From:</dt>
- <dd><%= mail.from %></dd>
+ <dd><%= mail.from.join(", ") %></dd>
<dt>Subject:</dt>
<dd><strong><%= mail.subject %></strong></dd>
@@ -65,18 +65,22 @@
<dd><%= Time.now.strftime("%b %e, %Y %I:%M:%S %p %Z") %></dd>
<dt>To:</dt>
- <dd><%= mail.to %></dd>
+ <dd><%= mail.to.join(", ") %></dd>
</dl>
- <% if false && mail.multipart? %>
+ <% if mail.multipart? %>
<p class="alternate">
- <% if body_part.content_type && body_part.content_type.match(/text\/html/) %>
- <a href="<%= name %>.txt">View plain text version</a>
+ <% if type == "plain" %>
+ <a href="rich.html">View HTML version</a>
<% else %>
- <a href="<%= name %>.html">View HTML version</a>
+ <a href="plain.html">View plain text version</a>
<% end %>
</p>
<% end %>
</div>
-<pre id="message_body"><%= mail.body %></pre>
+<% if type == "plain" %>
+ <pre id="message_body"><%= CGI.escapeHTML(body) %></pre>
+<% else %>
+ <%= body %>
+<% end %>
View
38 lib/letter_opener/message.rb
@@ -0,0 +1,38 @@
+module LetterOpener
+ class Message
+ attr_reader :mail
+
+ def initialize(location, mail, part = nil)
+ @location = location
+ @mail = mail
+ @part = part
+ end
+
+ def render
+ FileUtils.mkdir_p(@location)
+ File.open(filepath, 'w') do |f|
+ f.write ERB.new(template).result(binding)
+ end
+ end
+
+ def template
+ File.read(File.expand_path("../message.html.erb", __FILE__))
+ end
+
+ def filepath
+ File.join(@location, "#{type}.html")
+ end
+
+ def content_type
+ @part && @part.content_type || @mail.content_type
+ end
+
+ def body
+ (@part && @part.body || @mail.body).to_s
+ end
+
+ def type
+ content_type =~ /html/ ? "rich" : "plain"
+ end
+ end
+end
View
29 spec/letter_opener/delivery_method_spec.rb
@@ -19,8 +19,31 @@
subject 'Hello'
body 'World!'
end
- html = File.read(Dir["#{@location}/*.html"].first)
- html.should include("Hello")
- html.should include("World!")
+ text = File.read(Dir["#{@location}/*/plain.html"].first)
+ text.should include("foo@example.com")
+ text.should include("bar@example.com")
+ text.should include("Hello")
+ text.should include("World!")
+ end
+
+ it "saves multipart email into html document" do
+ mail = Mail.deliver do
+ from 'foo@example.com'
+ to 'bar@example.com'
+ subject 'Many parts'
+ text_part do
+ body 'This is <plain> text'
+ end
+ html_part do
+ content_type 'text/html; charset=UTF-8'
+ body '<h1>This is HTML</h1>'
+ end
+ end
+ text = File.read(Dir["#{@location}/*/plain.html"].first)
+ text.should include("View HTML version")
+ text.should include("This is &lt;plain&gt; text")
+ html = File.read(Dir["#{@location}/*/rich.html"].first)
+ html.should include("View plain text version")
+ html.should include("<h1>This is HTML</h1>")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.