Froze rails gems
[depot.git] / vendor / rails / actionmailer / lib / action_mailer / part_container.rb
1 module ActionMailer
2 # Accessors and helpers that ActionMailer::Base and ActionMailer::Part have
3 # in common. Using these helpers you can easily add subparts or attachments
4 # to your message:
5 #
6 # def my_mail_message(...)
7 # ...
8 # part "text/plain" do |p|
9 # p.body "hello, world"
10 # p.transfer_encoding "base64"
11 # end
12 #
13 # attachment "image/jpg" do |a|
14 # a.body = File.read("hello.jpg")
15 # a.filename = "hello.jpg"
16 # end
17 # end
18 module PartContainer
19 # The list of subparts of this container
20 attr_reader :parts
21
22 # Add a part to a multipart message, with the given content-type. The
23 # part itself is yielded to the block so that other properties (charset,
24 # body, headers, etc.) can be set on it.
25 def part(params)
26 params = {:content_type => params} if String === params
27 part = Part.new(params)
28 yield part if block_given?
29 @parts << part
30 end
31
32 # Add an attachment to a multipart message. This is simply a part with the
33 # content-disposition set to "attachment".
34 def attachment(params, &block)
35 params = { :content_type => params } if String === params
36 params = { :disposition => "attachment",
37 :transfer_encoding => "base64" }.merge(params)
38 part(params, &block)
39 end
40
41 private
42
43 def parse_content_type(defaults=nil)
44 return [defaults && defaults.content_type, {}] if content_type.blank?
45 ctype, *attrs = content_type.split(/;\s*/)
46 attrs = attrs.inject({}) { |h,s| k,v = s.split(/=/, 2); h[k] = v; h }
47 [ctype, {"charset" => charset || defaults && defaults.charset}.merge(attrs)]
48 end
49
50 end
51 end