1 require 'action_mailer/adv_attr_accessor'
2 require 'action_mailer/part_container'
3 require 'action_mailer/utils'
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.
11 include ActionMailer
::AdvAttrAccessor
12 include ActionMailer
::PartContainer
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
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
24 # The content disposition of this part, typically either "inline" or
26 adv_attr_accessor
:content_disposition
28 # The content type of the part.
29 adv_attr_accessor
:content_type
31 # The filename to use for this subpart (usually for attachments).
32 adv_attr_accessor
:filename
34 # Accessor for specifying additional headers to include with this part.
35 adv_attr_accessor
:headers
37 # The transfer encoding to use for this subpart, like "base64" or
39 adv_attr_accessor
:transfer_encoding
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]
48 @filename = params
[:filename]
49 @transfer_encoding = params
[:transfer_encoding] || "quoted-printable"
50 @headers = params
[:headers] || {}
54 # Convert the part to a mail object which can be included in the parts
55 # list of another mail object.
57 part
= TMail
::Mail.new
59 real_content_type
, ctype_attrs
= parse_content_type(defaults
)
62 part
.content_transfer_encoding
= transfer_encoding
|| "quoted-printable"
63 case (transfer_encoding
|| "").downcase
65 part
.body
= TMail
::Base64.folding_encode(body
)
66 when "quoted-printable"
67 part
.body
= [Utils
.normalize_new_lines(body
)].pack("M*")
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
))
82 part
.set_content_type(real_content_type
, nil, ctype_attrs
)
83 part
.set_content_disposition(content_disposition
)
87 @parts.unshift Part
.new(:charset => charset
, :body => @body, :content_type => 'text/plain')
92 prt
= (TMail
::Mail === p
? p
: p
.to_mail(defaults
))
96 part
.set_content_type(real_content_type
, nil, ctype_attrs
) if real_content_type
=~
/multipart/
99 headers
.each
{ |k
,v
| part
[k
] = v
}
106 def squish(values
={})
107 values
.delete_if
{ |k
,v
| v
.nil? }