Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Attachment Support (#26) #36

Closed
wants to merge 15 commits into from

4 participants

David Cornu Jeremy tylercollier Vasiliy Ermolovich
David Cornu

Write attachments to an attachments directory alongside plain.html and rich.html

Changes

  • Modified delivery_method.rb to use the text_part and html_part methods provided by mail to avoid rendering attachments as the plain version
  • Modified message.rb to write out the attachments and added an @attachments variable to simplify the template
  • Added links to the attachments in message.html.erb and changed styling accordingly
  • Modified delivery_method_spec.rb to test added functionality
David Cornu

@cj are the changes working out for you?

Jeremy

I successively used https://github.com/davidcornu/letter_opener.git to test attachments. Very nice.

Jeremy

bump.

Is there any technical reason this hasn't been merged?

Tyler Collier and others added some commits
Tyler Collier Regexp replacement of inline attachment urls with relative path, so t…
…hat e.g. images will shown in html body
25d9463
Tyler Collier Added a test 3f78683
David Cornu Merge pull request #1 from tylercollier/master
Inline attachments
ead65b4
tylercollier

Ditto: I successively used https://github.com/davidcornu/letter_opener.git to test attachments. Very nice.

Suggest merging this.

Ryan Bates ryanb referenced this pull request
Closed

Attachments? #26

Vasiliy Ermolovich
Collaborator

@davidcornu hey, thanks for the pull request. Could you rebase it against master? Thanks!

David Cornu

@nashby Will do

David Cornu

@nashby, I've rebased davidcornu/letter_opener#master against ryanb/letter_opener#master and pushed it to davidcornu/letter_opener#rebased as pushing to my own master would cause all sorts of havoc.

Let me know if you'd like me to open another pull request with that branch.

Vasiliy Ermolovich
Collaborator

@davidcornu yeah, please open a new PR. Thanks.

David Cornu

#48
Closing this pull request.

David Cornu davidcornu closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  lib/letter_opener.rb
View
@@ -1,6 +1,7 @@
require "fileutils"
require "digest/sha1"
require "cgi"
+require "uri"
require "launchy"
require "letter_opener/message"
4 lib/letter_opener/delivery_method.rb
View
@@ -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}"))
23 lib/letter_opener/message.html.erb
View
@@ -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>
23 lib/letter_opener/message.rb
View
@@ -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
@@ -28,7 +42,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
36 spec/letter_opener/delivery_method_spec.rb
View
@@ -46,5 +46,41 @@
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
+
+ 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
Something went wrong with that request. Please try again.