Skip to content

Commit

Permalink
Tentative attachment support
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcornu committed Oct 7, 2012
1 parent 5415538 commit 9368e21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/letter_opener/message.html.erb
Expand Up @@ -78,6 +78,16 @@
<dt>BCC:</dt>
<dd><%= h mail.bcc.join(", ") %></dd>
<% end %>
<% if @attachments.size > 0 %>
<dt>Attachments:</dt>
<dd>
<% @attachments.each do |filename, path| %>
<a href="<%= path %>"><%= filename %></a>
<% end %>
</dd>
<% end %>

</dl>

<% if mail.multipart? %>
Expand Down
12 changes: 12 additions & 0 deletions lib/letter_opener/message.rb
Expand Up @@ -13,10 +13,22 @@ def initialize(location, mail, part = nil)
@location = location
@mail = mail
@part = part
@attachments = []
end

def render
FileUtils.mkdir_p(@location)

if mail.attachments
attachments_dir = File.join(@location,'attachments')
FileUtils.mkdir_p(attachments_dir)
mail.attachments.each do |attachment|
path = File.join(attachments_dir, attachment.filename)
File.open(path, 'wb') { |f| f.write(attachment.body.raw_source) }
@attachments << [attachment.filename, "attachments/#{URI.escape(attachment.filename)}"]
end
end

File.open(filepath, 'w') do |f|
f.write ERB.new(template).result(binding)
end
Expand Down

0 comments on commit 9368e21

Please sign in to comment.