Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #2179: actionmailer_inline_images.rb

File actionmailer_inline_images.rb, 2.1 kB (added by mike@michaelhoughton.co.uk, 4 years ago)
Line 
1 module ActionMailer
2  
3   module PartContainer
4
5     # Add an inline attachment to a multipart message.
6     def inline_attachment(params, &block)
7       params = { :content_type => params } if String === params
8       params = { :disposition => "inline",
9                  :transfer_encoding => "base64" }.merge(params)
10       params[:headers] ||= {}
11       params[:headers]['Content-ID'] = params[:cid]
12       part(params, &block)
13     end
14
15   end
16
17   class Part
18
19     def to_mail(defaults)
20       part = TMail::Mail.new
21
22       if @parts.empty?
23         part.content_transfer_encoding = transfer_encoding || "quoted-printable"
24         case (transfer_encoding || "").downcase
25           when "base64" then
26             part.body = TMail::Base64.folding_encode(body)
27           when "quoted-printable"
28             part.body = [Utils.normalize_new_lines(body)].pack("M*")
29           else
30             part.body = body
31         end
32
33         # Always set the content_type after setting the body and or parts
34
35         # CHANGE: treat attachments and inline files the same
36         if content_disposition == "attachment" || ((content_disposition == "inline") && filename)
37             part.set_content_type(content_type || defaults.content_type, nil,
38             squish("charset" => nil, "name" => filename))
39         else
40           part.set_content_type(content_type || defaults.content_type, nil,
41             "charset" => (charset || defaults.charset))   
42         end 
43                      
44         part.set_content_disposition(content_disposition, squish("filename" => filename))
45         headers.each {|k,v| part[k] = v }
46         # END CHANGE
47
48       else
49         if String === body
50           part = TMail::Mail.new
51           part.body = body
52           part.set_content_type content_type, nil, { "charset" => charset }
53           part.set_content_disposition "inline"
54           m.parts << part
55         end
56          
57         @parts.each do |p|
58           prt = (TMail::Mail === p ? p : p.to_mail(defaults))
59           part.parts << prt
60         end
61        
62         part.set_content_type(content_type, nil, { "charset" => charset }) if content_type =~ /multipart/
63       end
64    
65       part
66     end
67   end
68 end