Permalink
Browse files

Merge pull request #48 from davidcornu/rebased

Attachment Support - Rebased version of pull request #36
  • Loading branch information...
2 parents 5415538 + 09dc52d commit 0f69108f9d9df815dc0303145d6c608318ca34fc @nashby nashby committed Oct 9, 2012
@@ -1,6 +1,7 @@
require "fileutils"
require "digest/sha1"
require "cgi"
+require "uri"
require "launchy"
require "letter_opener/message"
@@ -19,7 +19,7 @@
}
#message_headers dt {
- width: 62px;
+ width: 92px;
padding: 1px;
margin: 0;
float: left;
@@ -29,7 +29,7 @@
}
#message_headers dd {
- margin: 0 0 0 72px;
+ margin: 0 0 0 102px;
padding: 1px;
}
@@ -78,13 +78,24 @@
<dt>BCC:</dt>
<dd><%= h mail.bcc.join(", ") %></dd>
<% end %>
+
+ <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>
@@ -3,7 +3,9 @@ class Message
attr_reader :mail
def self.rendered_messages(location, mail)
- messages = mail.parts.map { |part| new(location, mail, part) }
+ messages = []
+ messages << new(location, mail, mail.html_part) if mail.html_part
+ messages << new(location, mail, mail.text_part) if mail.text_part
messages << new(location, mail) if messages.empty?
messages.each(&:render)
messages.sort
@@ -13,10 +15,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
@@ -35,7 +51,14 @@ def content_type
end
def body
- @body ||= (@part && @part.body || @mail.body).to_s
+ if !@body_string
+ @body_string = (@part && @part.body || @mail.body).to_s
+ mail.attachments.each do |attachment|
+ @body_string.gsub!(attachment.url, "attachments/#{attachment.filename}")
+ end
+ @body = @body_string
+ end
+ @body
end
def from
@@ -166,4 +166,40 @@
plain.should include("World!")
end
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
+
+ it "replaces inline attachment urls" do
+ mail = Mail.deliver do
+ from 'foo@example.com'
+ to 'bar@example.com'
+ subject 'With attachments'
+ attachments[File.basename(__FILE__)] = File.read(__FILE__)
+ url = attachments[0].url
+ html_part do
+ content_type 'text/html; charset=UTF-8'
+ body "Here's an image: <img src='#{url}' />"
+ end
+ end
+ attachment_path = Dir["#{location}/*/attachments/#{File.basename(__FILE__)}"].first
+ File.exists?(attachment_path).should == true
+ text = File.read(Dir["#{location}/*/rich.html"].first)
+ mail.parts[0].body.should include(mail.attachments[0].url)
+ text.should_not include(mail.attachments[0].url)
+ text.should include("attachments/#{File.basename(__FILE__)}")
+ end
end

0 comments on commit 0f69108

Please sign in to comment.