Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #36 from davidcornu/master

Attachment Support (#26)

* davidcornu/master:
  Don't recreate files if already present
  Added test for attachments
  mail.attachments returns an array which evaluates to true, changed to a size check
  Forgot a pixel
  Always show attachment section
  only show rich/plain links if the parts exist
  Use mail's text_part and html_part methods to avoid rendering attachments as plain text
  Turns out there was a good reason for that
  Remove height constraint from headers and add more padding so more attachments can fit
  Better styling
  Require 'uri' for escaping filenames
  Tentative attachment support
  • Loading branch information...
commit 15b1a1558e57f1eaddebfe96076f48af38508e0a 2 parents 5772e55 + 3973b99
@cj authored
View
1  lib/letter_opener.rb
@@ -1,6 +1,7 @@
require "fileutils"
require "digest/sha1"
require "cgi"
+require "uri"
require "launchy"
require "letter_opener/message"
View
4 lib/letter_opener/delivery_method.rb
@@ -6,7 +6,9 @@ def initialize(options = {})
def deliver!(mail)
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 = []
+ messages << Message.new(location, mail, mail.html_part) if mail.html_part
+ messages << Message.new(location, mail, mail.text_part) if mail.text_part
messages << Message.new(location, mail) if messages.empty?
messages.each { |message| message.render }
Launchy.open(URI.parse("file://#{messages.first.filepath}"))
View
23 lib/letter_opener/message.html.erb
@@ -6,7 +6,7 @@
top: 0px;
left: 0;
width: 100%;
- height: 85px;
+ height: 90px;
padding: 10px 0 0 0;
margin: 0;
background: #fff;
@@ -22,7 +22,7 @@
}
#message_headers dt {
- width: 60px;
+ width: 100px;
padding: 1px;
float: left;
text-align: right;
@@ -31,7 +31,7 @@
}
#message_headers dd {
- margin-left: 70px;
+ margin-left: 110px;
padding: 1px;
}
@@ -51,7 +51,7 @@
}
body {
- margin-top: 96px !important;
+ margin-top: 101px !important;
}
</style>
@@ -68,13 +68,24 @@
<dt>To:</dt>
<dd><%= mail.to.join(", ") %></dd>
+
+ <dt>Attachments:</dt>
+ <dd>
+ <% if @attachments.size > 0 %>
+ <% @attachments.each do |filename, path| %>
+ <a href="<%= path %>"><%= filename %></a>&nbsp;
+ <% end %>
+ <% else %>
+ None
+ <% end %>
+ </dd>
</dl>
<% if mail.multipart? %>
<p class="alternate">
- <% if type == "plain" %>
+ <% if type == "plain" && mail.html_part %>
<a href="rich.html">View HTML version</a>
- <% else %>
+ <% elsif type == "rich" && mail.text_part %>
<a href="plain.html">View plain text version</a>
<% end %>
</p>
View
14 lib/letter_opener/message.rb
@@ -6,10 +6,24 @@ def initialize(location, mail, part = nil)
@location = location
@mail = mail
@part = part
+ @attachments = []
end
def render
FileUtils.mkdir_p(@location)
+
+ if mail.attachments.size > 0
+ attachments_dir = File.join(@location,'attachments')
+ FileUtils.mkdir_p(attachments_dir)
+ mail.attachments.each do |attachment|
+ path = File.join(attachments_dir, attachment.filename)
+ unless File.exists?(path) # true if other parts have already been rendered
+ File.open(path, 'wb') { |f| f.write(attachment.body.raw_source) }
+ end
+ @attachments << [attachment.filename, "attachments/#{URI.escape(attachment.filename)}"]
+ end
+ end
+
File.open(filepath, 'w') do |f|
f.write ERB.new(template).result(binding)
end
View
16 spec/letter_opener/delivery_method_spec.rb
@@ -46,5 +46,21 @@
html.should include("View plain text version")
html.should include("<h1>This is HTML</h1>")
end
+
+ it "saves attachments into a seperate directory" do
+ mail = Mail.deliver do
+ from 'foo@example.com'
+ to 'bar@example.com'
+ subject 'With attachments'
+ text_part do
+ body 'This is <plain> text'
+ end
+ attachments[File.basename(__FILE__)] = File.read(__FILE__)
+ end
+ attachment_path = Dir["#{@location}/*/attachments/#{File.basename(__FILE__)}"].first
+ File.exists?(attachment_path).should == true
+ text = File.read(Dir["#{@location}/*/plain.html"].first)
+ text.should include(File.basename(__FILE__))
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.