2005-05-29 12:36:22 -04:00
|
|
|
require 'action_mailer/adv_attr_accessor'
|
2005-07-01 09:17:44 -04:00
|
|
|
require 'action_mailer/part_container'
|
2005-07-06 05:53:34 -04:00
|
|
|
require 'action_mailer/utils'
|
2005-05-29 12:36:22 -04:00
|
|
|
|
|
|
|
module ActionMailer
|
|
|
|
class Part #:nodoc:
|
|
|
|
include ActionMailer::AdvAttrAccessor
|
2005-07-01 09:17:44 -04:00
|
|
|
include ActionMailer::PartContainer
|
2005-05-29 12:36:22 -04:00
|
|
|
|
|
|
|
adv_attr_accessor :content_type, :content_disposition, :charset, :body
|
|
|
|
adv_attr_accessor :filename, :transfer_encoding, :headers
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def to_mail(defaults)
|
|
|
|
part = TMail::Mail.new
|
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"
|
2005-07-06 05:53:34 -04:00
|
|
|
part.body = [Utils.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"
|
|
|
|
part.set_content_type(content_type || defaults.content_type, nil,
|
2005-07-04 12:11:22 -04:00
|
|
|
squish("charset" => nil, "name" => filename))
|
|
|
|
part.set_content_disposition(content_disposition,
|
|
|
|
squish("filename" => filename))
|
2005-05-29 12:36:22 -04:00
|
|
|
else
|
2005-07-01 09:17:44 -04:00
|
|
|
part.set_content_type(content_type || defaults.content_type, nil,
|
|
|
|
"charset" => (charset || defaults.charset))
|
|
|
|
part.set_content_disposition(content_disposition)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if String === body
|
|
|
|
part = TMail::Mail.new
|
2005-05-29 12:36:22 -04:00
|
|
|
part.body = body
|
2005-07-01 09:17:44 -04:00
|
|
|
part.set_content_type content_type, nil, { "charset" => charset }
|
|
|
|
part.set_content_disposition "inline"
|
|
|
|
m.parts << part
|
|
|
|
end
|
|
|
|
|
|
|
|
@parts.each do |p|
|
|
|
|
prt = (TMail::Mail === p ? p : p.to_mail(defaults))
|
|
|
|
part.parts << prt
|
|
|
|
end
|
|
|
|
|
|
|
|
part.set_content_type(content_type, nil, { "charset" => charset }) if content_type =~ /multipart/
|
2005-05-29 12:36:22 -04:00
|
|
|
end
|
2005-07-01 09:17:44 -04: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
|