Skip to content
This repository
Browse code

Adding inline attachment support to ActionMailer

  • Loading branch information...
commit 311d99eef01c268cedc6e9b3bdb9abc2ba5c6bfa 1 parent 21cc1ed
Mikel Lindsaar authored June 07, 2010
2  actionmailer/actionmailer.gemspec
@@ -20,5 +20,5 @@ Gem::Specification.new do |s|
20 20
   s.has_rdoc = true
21 21
 
22 22
   s.add_dependency('actionpack',  version)
23  
-  s.add_dependency('mail',        '~> 2.2.2')
  23
+  s.add_dependency('mail',        '~> 2.2.3')
24 24
 end
6  actionmailer/lib/action_mailer/base.rb
@@ -612,7 +612,11 @@ def set_content_type(m, user_content_type, class_default)
612 612
       when user_content_type.present?
613 613
         user_content_type
614 614
       when m.has_attachments?
615  
-        ["multipart", "mixed", params]
  615
+        if m.attachments.detect { |a| a.inline? }
  616
+          ["multipart", "related", params]
  617
+        else
  618
+          ["multipart", "mixed", params]
  619
+        end
616 620
       when m.multipart?
617 621
         ["multipart", "alternative", params]
618 622
       else
5  actionmailer/lib/action_mailer/mail_helper.rb
@@ -32,5 +32,10 @@ def mailer
32 32
     def message
33 33
       @_message
34 34
     end
  35
+
  36
+    # Access the message attachments list.
  37
+    def attachments
  38
+      @_message.attachments
  39
+    end
35 40
   end
36 41
 end
17  actionmailer/test/base_test.rb
@@ -33,6 +33,11 @@ def plain_text_only(hash = {})
33 33
       mail(hash)
34 34
     end
35 35
 
  36
+    def inline_attachment
  37
+      attachments.inline['logo.png'] = "\312\213\254\232"
  38
+      mail
  39
+    end
  40
+
36 41
     def attachment_with_content(hash = {})
37 42
       attachments['invoice.pdf'] = 'This is test File content'
38 43
       mail(hash)
@@ -264,6 +269,18 @@ def give_a_greeting
264 269
     assert_equal("application/pdf", email.parts[1].mime_type)
265 270
     assert_equal("VGhpcyBpcyB0ZXN0IEZpbGUgY29udGVudA==\r\n", email.parts[1].body.encoded)
266 271
   end
  272
+  
  273
+  test "can embed an inline attachment" do
  274
+    email = BaseMailer.inline_attachment
  275
+    # Need to call #encoded to force the JIT sort on parts
  276
+    email.encoded
  277
+    assert_equal(2, email.parts.length)
  278
+    assert_equal("multipart/related", email.mime_type)
  279
+    assert_equal("multipart/alternative", email.parts[0].mime_type)
  280
+      assert_equal("text/plain", email.parts[0].parts[0].mime_type)
  281
+      assert_equal("text/html",  email.parts[0].parts[1].mime_type)
  282
+    assert_equal("logo.png", email.parts[1].filename)
  283
+  end
267 284
 
268 285
   # Defaults values
269 286
   test "uses default charset from class" do
5  actionmailer/test/fixtures/base_mailer/inline_attachment.html.erb
... ...
@@ -0,0 +1,5 @@
  1
+<h1>Inline Image</h1>
  2
+
  3
+<%= image_tag attachments['logo.png'].url %>
  4
+
  5
+<p>This is an image that is inline</p>
4  actionmailer/test/fixtures/base_mailer/inline_attachment.text.erb
... ...
@@ -0,0 +1,4 @@
  1
+Inline Image
  2
+
  3
+No image for you
  4
+

0 notes on commit 311d99e

Please sign in to comment.
Something went wrong with that request. Please try again.