Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / actionmailer / lib / action_mailer / part.rb
1 module ActionMailer
2 # Represents a subpart of an email message. It shares many similar
3 # attributes of ActionMailer::Base. Although you can create parts manually
4 # and add them to the +parts+ list of the mailer, it is easier
5 # to use the helper methods in ActionMailer::PartContainer.
6 class Part
7 include AdvAttrAccessor, PartContainer, Utils
8
9 # Represents the body of the part, as a string. This should not be a
10 # Hash (like ActionMailer::Base), but if you want a template to be rendered
11 # into the body of a subpart you can do it with the mailer's +render+ method
12 # and assign the result here.
13 adv_attr_accessor :body
14
15 # Specify the charset for this subpart. By default, it will be the charset
16 # of the containing part or mailer.
17 adv_attr_accessor :charset
18
19 # The content disposition of this part, typically either "inline" or
20 # "attachment".
21 adv_attr_accessor :content_disposition
22
23 # The content type of the part.
24 adv_attr_accessor :content_type
25
26 # The filename to use for this subpart (usually for attachments).
27 adv_attr_accessor :filename
28
29 # Accessor for specifying additional headers to include with this part.
30 adv_attr_accessor :headers
31
32 # The transfer encoding to use for this subpart, like "base64" or
33 # "quoted-printable".
34 adv_attr_accessor :transfer_encoding
35
36 # Create a new part from the given +params+ hash. The valid params keys
37 # correspond to the accessors.
38 def initialize(params)
39 @content_type = params[:content_type]
40 @content_disposition = params[:disposition] || "inline"
41 @charset = params[:charset]
42 @body = params[:body]
43 @filename = params[:filename]
44 @transfer_encoding = params[:transfer_encoding] || "quoted-printable"
45 @headers = params[:headers] || {}
46 @parts = []
47 end
48
49 # Convert the part to a mail object which can be included in the parts
50 # list of another mail object.
51 def to_mail(defaults)
52 part = TMail::Mail.new
53
54 real_content_type, ctype_attrs = parse_content_type(defaults)
55
56 if @parts.empty?
57 part.content_transfer_encoding = transfer_encoding || "quoted-printable"
58 case (transfer_encoding || "").downcase
59 when "base64" then
60 part.body = TMail::Base64.folding_encode(body)
61 when "quoted-printable"
62 part.body = [normalize_new_lines(body)].pack("M*")
63 else
64 part.body = body
65 end
66
67 # Always set the content_type after setting the body and or parts!
68 # Also don't set filename and name when there is none (like in
69 # non-attachment parts)
70 if content_disposition == "attachment"
71 ctype_attrs.delete "charset"
72 part.set_content_type(real_content_type, nil,
73 squish("name" => filename).merge(ctype_attrs))
74 part.set_content_disposition(content_disposition,
75 squish("filename" => filename).merge(ctype_attrs))
76 else
77 part.set_content_type(real_content_type, nil, ctype_attrs)
78 part.set_content_disposition(content_disposition)
79 end
80 else
81 if String === body
82 @parts.unshift Part.new(:charset => charset, :body => @body, :content_type => 'text/plain')
83 @body = nil
84 end
85
86 @parts.each do |p|
87 prt = (TMail::Mail === p ? p : p.to_mail(defaults))
88 part.parts << prt
89 end
90
91 if real_content_type =~ /multipart/
92 ctype_attrs.delete 'charset'
93 part.set_content_type(real_content_type, nil, ctype_attrs)
94 end
95 end
96
97 headers.each { |k,v| part[k] = v }
98
99 part
100 end
101
102 private
103 def squish(values={})
104 values.delete_if { |k,v| v.nil? }
105 end
106 end
107 end