2005-05-29 12:36:22 -04:00
|
|
|
module ActionMailer
|
2005-10-16 11:00:27 -04:00
|
|
|
# Represents a subpart of an email message. It shares many similar
|
|
|
|
# attributes of ActionMailer::Base. Although you can create parts manually
|
2008-05-25 07:29:00 -04:00
|
|
|
# and add them to the +parts+ list of the mailer, it is easier
|
2005-10-16 11:00:27 -04:00
|
|
|
# to use the helper methods in ActionMailer::PartContainer.
|
|
|
|
class Part
|
2008-11-23 13:27:25 -05:00
|
|
|
include AdvAttrAccessor, PartContainer, Utils
|
2005-05-29 12:36:22 -04:00
|
|
|
|
2005-10-16 11:00:27 -04:00
|
|
|
# Represents the body of the part, as a string. This should not be a
|
|
|
|
# Hash (like ActionMailer::Base), but if you want a template to be rendered
|
2008-05-25 07:29:00 -04:00
|
|
|
# into the body of a subpart you can do it with the mailer's +render+ method
|
2005-10-16 11:00:27 -04:00
|
|
|
# and assign the result here.
|
|
|
|
adv_attr_accessor :body
|
|
|
|
|
|
|
|
# Specify the charset for this subpart. By default, it will be the charset
|
|
|
|
# of the containing part or mailer.
|
|
|
|
adv_attr_accessor :charset
|
|
|
|
|
|
|
|
# The content disposition of this part, typically either "inline" or
|
|
|
|
# "attachment".
|
|
|
|
adv_attr_accessor :content_disposition
|
|
|
|
|
|
|
|
# The content type of the part.
|
|
|
|
adv_attr_accessor :content_type
|
|
|
|
|
|
|
|
# The filename to use for this subpart (usually for attachments).
|
|
|
|
adv_attr_accessor :filename
|
|
|
|
|
|
|
|
# Accessor for specifying additional headers to include with this part.
|
|
|
|
adv_attr_accessor :headers
|
|
|
|
|
|
|
|
# The transfer encoding to use for this subpart, like "base64" or
|
|
|
|
# "quoted-printable".
|
|
|
|
adv_attr_accessor :transfer_encoding
|
2005-05-29 12:36:22 -04:00
|
|
|
|
2005-10-16 11:00:27 -04:00
|
|
|
# Create a new part from the given +params+ hash. The valid params keys
|
|
|
|
# correspond to the accessors.
|
2005-05-29 12:36:22 -04:00
|
|
|
def initialize(params)
|
2005-06-03 06:57:06 -04:00
|
|
|
@content_type = params[:content_type]
|
2005-05-29 12:36:22 -04:00
|
|
|
@content_disposition = params[:disposition] || "inline"
|
|
|
|
@charset = params[:charset]
|
|
|
|
@body = params[:body]
|
|
|
|
@filename = params[:filename]
|
|
|
|
@transfer_encoding = params[:transfer_encoding] || "quoted-printable"
|
|
|
|
@headers = params[:headers] || {}
|
2005-07-01 09:17:44 -04:00
|
|
|
@parts = []
|
2005-05-29 12:36:22 -04:00
|
|
|
end
|
|
|
|
|
2005-10-16 11:00:27 -04:00
|
|
|
# Convert the part to a mail object which can be included in the parts
|
|
|
|
# list of another mail object.
|
2005-05-29 12:36:22 -04:00
|
|
|
def to_mail(defaults)
|
|
|
|
part = TMail::Mail.new
|
2005-07-01 09:17:44 -04:00
|
|
|
|
2006-03-18 18:53:07 -05:00
|
|
|
real_content_type, ctype_attrs = parse_content_type(defaults)
|
|
|
|
|
2005-07-01 09:17:44 -04:00
|
|
|
if @parts.empty?
|
|
|
|
part.content_transfer_encoding = transfer_encoding || "quoted-printable"
|
|
|
|
case (transfer_encoding || "").downcase
|
|
|
|
when "base64" then
|
|
|
|
part.body = TMail::Base64.folding_encode(body)
|
|
|
|
when "quoted-printable"
|
2008-11-23 13:27:25 -05:00
|
|
|
part.body = [normalize_new_lines(body)].pack("M*")
|
2005-07-01 09:17:44 -04:00
|
|
|
else
|
|
|
|
part.body = body
|
|
|
|
end
|
|
|
|
|
|
|
|
# Always set the content_type after setting the body and or parts!
|
|
|
|
# Also don't set filename and name when there is none (like in
|
|
|
|
# non-attachment parts)
|
|
|
|
if content_disposition == "attachment"
|
2006-03-18 18:53:07 -05:00
|
|
|
ctype_attrs.delete "charset"
|
|
|
|
part.set_content_type(real_content_type, nil,
|
|
|
|
squish("name" => filename).merge(ctype_attrs))
|
2005-07-04 12:11:22 -04:00
|
|
|
part.set_content_disposition(content_disposition,
|
2006-03-18 18:53:07 -05:00
|
|
|
squish("filename" => filename).merge(ctype_attrs))
|
2005-05-29 12:36:22 -04:00
|
|
|
else
|
2006-03-18 18:53:07 -05:00
|
|
|
part.set_content_type(real_content_type, nil, ctype_attrs)
|
2005-07-01 09:17:44 -04:00
|
|
|
part.set_content_disposition(content_disposition)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if String === body
|
2007-11-28 21:52:49 -05:00
|
|
|
@parts.unshift Part.new(:charset => charset, :body => @body, :content_type => 'text/plain')
|
|
|
|
@body = nil
|
2005-07-01 09:17:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@parts.each do |p|
|
|
|
|
prt = (TMail::Mail === p ? p : p.to_mail(defaults))
|
|
|
|
part.parts << prt
|
|
|
|
end
|
|
|
|
|
2006-03-18 18:53:07 -05:00
|
|
|
part.set_content_type(real_content_type, nil, ctype_attrs) if real_content_type =~ /multipart/
|
2005-05-29 12:36:22 -04:00
|
|
|
end
|
2006-03-18 18:06:25 -05:00
|
|
|
|
2006-03-18 18:53:07 -05:00
|
|
|
headers.each { |k,v| part[k] = v }
|
2006-03-18 18:06:25 -05:00
|
|
|
|
2005-05-29 12:36:22 -04:00
|
|
|
part
|
|
|
|
end
|
2005-07-04 12:11:22 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def squish(values={})
|
|
|
|
values.delete_if { |k,v| v.nil? }
|
|
|
|
end
|
2005-05-29 12:36:22 -04:00
|
|
|
end
|
|
|
|
end
|