未验证 提交 397693a5 编写于 作者: A Aaron Patterson 提交者: GitHub

Merge pull request #26445 from dracos/multiparty

Correctly wrap inline attachments.
......@@ -842,6 +842,7 @@ def mail(headers = {}, &block)
@_mail_was_called = true
create_parts_from_responses(message, responses)
wrap_inline_attachments(message)
# Set up content type, reapply charset and handle parts order
message.content_type = set_content_type(message, content_type, headers[:content_type])
......@@ -871,7 +872,7 @@ def set_content_type(m, user_content_type, class_default) # :doc:
when user_content_type.present?
user_content_type
when m.has_attachments?
if m.attachments.detect(&:inline?)
if m.attachments.all?(&:inline?)
["multipart", "related", params]
else
["multipart", "mixed", params]
......@@ -969,6 +970,27 @@ def each_template(paths, name, &block)
end
end
def wrap_inline_attachments(message)
# If we have both types of attachment, wrap all the inline attachments
# in multipart/related, but not the actual attachments
if message.attachments.detect(&:inline?) && message.attachments.detect { |a| !a.inline? }
related = Mail::Part.new
related.content_type = "multipart/related"
mixed = [ related ]
message.parts.each do |p|
if p.attachment? && !p.inline?
mixed << p
else
related.add_part(p)
end
end
message.parts.clear
mixed.each { |c| message.add_part(c) }
end
end
def create_parts_from_responses(m, responses)
if responses.size == 1 && !m.has_attachments?
responses[0].each { |k, v| m[k] = v }
......
......@@ -186,6 +186,20 @@ class BaseTest < ActiveSupport::TestCase
assert_equal("logo.png", email.parts[1].filename)
end
test "can embed an inline attachment and other attachments" do
email = BaseMailer.inline_and_other_attachments
# Need to call #encoded to force the JIT sort on parts
email.encoded
assert_equal(2, email.parts.length)
assert_equal("multipart/mixed", email.mime_type)
assert_equal("multipart/related", email.parts[0].mime_type)
assert_equal("multipart/alternative", email.parts[0].parts[0].mime_type)
assert_equal("text/plain", email.parts[0].parts[0].parts[0].mime_type)
assert_equal("text/html", email.parts[0].parts[0].parts[1].mime_type)
assert_equal("logo.png", email.parts[0].parts[1].filename)
assert_equal("certificate.pdf", email.parts[1].filename)
end
# Defaults values
test "uses default charset from class" do
with_default BaseMailer, charset: "US-ASCII" do
......
<h1>Inline Image</h1>
<%= image_tag attachments['logo.png'].url %>
<p>This is an image that is inline</p>
\ No newline at end of file
......@@ -44,6 +44,12 @@ def inline_attachment
mail
end
def inline_and_other_attachments
attachments.inline["logo.png"] = "\312\213\254\232"
attachments["certificate.pdf"] = "This is test File content"
mail
end
def attachment_with_content(hash = {})
attachments["invoice.pdf"] = "This is test File content"
mail(hash)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册