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