From 311d99eef01c268cedc6e9b3bdb9abc2ba5c6bfa Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Mon, 7 Jun 2010 21:54:53 -0400 Subject: Adding inline attachment support to ActionMailer --- actionmailer/actionmailer.gemspec | 2 +- actionmailer/lib/action_mailer/base.rb | 6 +++++- actionmailer/lib/action_mailer/mail_helper.rb | 5 +++++ actionmailer/test/base_test.rb | 17 +++++++++++++++++ .../fixtures/base_mailer/inline_attachment.html.erb | 5 +++++ .../fixtures/base_mailer/inline_attachment.text.erb | 4 ++++ 6 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 actionmailer/test/fixtures/base_mailer/inline_attachment.html.erb create mode 100644 actionmailer/test/fixtures/base_mailer/inline_attachment.text.erb (limited to 'actionmailer') diff --git a/actionmailer/actionmailer.gemspec b/actionmailer/actionmailer.gemspec index 1e34352f53..2b7c21b3f2 100644 --- a/actionmailer/actionmailer.gemspec +++ b/actionmailer/actionmailer.gemspec @@ -20,5 +20,5 @@ Gem::Specification.new do |s| s.has_rdoc = true s.add_dependency('actionpack', version) - s.add_dependency('mail', '~> 2.2.2') + s.add_dependency('mail', '~> 2.2.3') end diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 3a82979d35..9fb3ba425f 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -612,7 +612,11 @@ module ActionMailer #:nodoc: when user_content_type.present? user_content_type when m.has_attachments? - ["multipart", "mixed", params] + if m.attachments.detect { |a| a.inline? } + ["multipart", "related", params] + else + ["multipart", "mixed", params] + end when m.multipart? ["multipart", "alternative", params] else diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb index aab6e12387..b708881edf 100644 --- a/actionmailer/lib/action_mailer/mail_helper.rb +++ b/actionmailer/lib/action_mailer/mail_helper.rb @@ -32,5 +32,10 @@ module ActionMailer def message @_message end + + # Access the message attachments list. + def attachments + @_message.attachments + end end end diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 54bf3de6af..b9226196fd 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -33,6 +33,11 @@ class BaseTest < ActiveSupport::TestCase mail(hash) end + def inline_attachment + attachments.inline['logo.png'] = "\312\213\254\232" + mail + end + def attachment_with_content(hash = {}) attachments['invoice.pdf'] = 'This is test File content' mail(hash) @@ -264,6 +269,18 @@ class BaseTest < ActiveSupport::TestCase assert_equal("application/pdf", email.parts[1].mime_type) assert_equal("VGhpcyBpcyB0ZXN0IEZpbGUgY29udGVudA==\r\n", email.parts[1].body.encoded) end + + test "can embed an inline attachment" do + email = BaseMailer.inline_attachment + # Need to call #encoded to force the JIT sort on parts + email.encoded + assert_equal(2, email.parts.length) + assert_equal("multipart/related", email.mime_type) + assert_equal("multipart/alternative", email.parts[0].mime_type) + assert_equal("text/plain", email.parts[0].parts[0].mime_type) + assert_equal("text/html", email.parts[0].parts[1].mime_type) + assert_equal("logo.png", email.parts[1].filename) + end # Defaults values test "uses default charset from class" do diff --git a/actionmailer/test/fixtures/base_mailer/inline_attachment.html.erb b/actionmailer/test/fixtures/base_mailer/inline_attachment.html.erb new file mode 100644 index 0000000000..e200878127 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/inline_attachment.html.erb @@ -0,0 +1,5 @@ +

Inline Image

+ +<%= image_tag attachments['logo.png'].url %> + +

This is an image that is inline

\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/inline_attachment.text.erb b/actionmailer/test/fixtures/base_mailer/inline_attachment.text.erb new file mode 100644 index 0000000000..e161d244d2 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/inline_attachment.text.erb @@ -0,0 +1,4 @@ +Inline Image + +No image for you + -- cgit v1.2.3