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.
7 include AdvAttrAccessor
, PartContainer
, Utils
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
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
19 # The content disposition of this part, typically either "inline" or
21 adv_attr_accessor
:content_disposition
23 # The content type of the part.
24 adv_attr_accessor
:content_type
26 # The filename to use for this subpart (usually for attachments).
27 adv_attr_accessor
:filename
29 # Accessor for specifying additional headers to include with this part.
30 adv_attr_accessor
:headers
32 # The transfer encoding to use for this subpart, like "base64" or
34 adv_attr_accessor
:transfer_encoding
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]
43 @filename = params
[:filename]
44 @transfer_encoding = params
[:transfer_encoding] || "quoted-printable"
45 @headers = params
[:headers] || {}
49 # Convert the part to a mail object which can be included in the parts
50 # list of another mail object.
52 part
= TMail
::Mail.new
54 real_content_type
, ctype_attrs
= parse_content_type(defaults
)
57 part
.content_transfer_encoding
= transfer_encoding
|| "quoted-printable"
58 case (transfer_encoding
|| "").downcase
60 part
.body
= TMail
::Base64.folding_encode(body
)
61 when "quoted-printable"
62 part
.body
= [normalize_new_lines(body
)].pack("M*")
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
))
77 part
.set_content_type(real_content_type
, nil, ctype_attrs
)
78 part
.set_content_disposition(content_disposition
)
82 @parts.unshift Part
.new(:charset => charset
, :body => @body, :content_type => 'text/plain')
87 prt
= (TMail
::Mail === p
? p
: p
.to_mail(defaults
))
91 if real_content_type
=~
/multipart/
92 ctype_attrs
.delete
'charset'
93 part
.set_content_type(real_content_type
, nil, ctype_attrs
)
97 headers
.each
{ |k
,v
| part
[k
] = v
}
103 def squish(values
={})
104 values
.delete_if
{ |k
,v
| v
.nil? }