diff options
Diffstat (limited to 'actionmailer/lib/action_mailer/base.rb')
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 83 |
1 files changed, 62 insertions, 21 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index bef9bb8e79..9d7af72362 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -251,7 +251,7 @@ module ActionMailer #:nodoc: # and appear last in the mime encoded message. You can also pick a different order from inside a method with # +implicit_parts_order+. class Base - include AdvAttrAccessor, PartContainer, Quoting, Utils + include AdvAttrAccessor, Quoting, Utils include AbstractController::RenderingController include AbstractController::LocalizedCache @@ -366,6 +366,30 @@ module ActionMailer #:nodoc: # Alias controller_path to mailer_name so render :partial in views work. alias :controller_path :mailer_name + # Add a part to a multipart message, with the given content-type. The + # part itself is yielded to the block so that other properties (charset, + # body, headers, etc.) can be set on it. + def part(params) + params = {:content_type => params} if String === params + if custom_headers = params.delete(:headers) + STDERR.puts("Passing custom headers with :headers => {} is deprecated. Please just pass in custom headers directly.") + params = params.merge(custom_headers) + end + part = Mail::Part.new(params) + yield part if block_given? + @parts << part + end + + # Add an attachment to a multipart message. This is simply a part with the + # content-disposition set to "attachment". + def attachment(params, &block) + params = { :content_type => params } if String === params + params = { :content_disposition => "attachment", + :content_transfer_encoding => "base64" }.merge(params) + params[:data] = params.delete(:body) if params[:body] + part(params, &block) + end + class << self attr_writer :mailer_name @@ -411,8 +435,7 @@ module ActionMailer #:nodoc: # end def receive(raw_email) logger.info "Received mail:\n #{raw_email}" unless logger.nil? - mail = TMail::Mail.parse(raw_email) - mail.base64_decode + mail = Mail.new(raw_email) new.receive(mail) end @@ -458,7 +481,7 @@ module ActionMailer #:nodoc: end # Initialize the mailer via the given +method_name+. The body will be - # rendered and a new TMail::Mail object created. + # rendered and a new Mail object created. def create!(method_name, *parameters) #:nodoc: initialize_defaults(method_name) __send__(method_name, *parameters) @@ -474,7 +497,7 @@ module ActionMailer #:nodoc: @mail = create_mail end - # Delivers a TMail::Mail object. By default, it delivers the cached mail + # Delivers a Mail object. By default, it delivers the cached mail # object (from the <tt>create!</tt> method). If no cached mail object exists, and # no alternate has been given as the parameter, this will fail. def deliver!(mail = @mail) @@ -522,18 +545,18 @@ module ActionMailer #:nodoc: super # Run deprecation hooks if String === response_body - @parts.unshift Part.new( - :content_type => "text/plain", - :disposition => "inline", - :charset => charset, + @parts.unshift Mail::Part.new( + :content_type => ["text", "plain", {:charset => charset}], + :content_disposition => "inline", :body => response_body ) else self.class.template_root.find_all(@template, {}, mailer_name).each do |template| - @parts << Part.new( - :content_type => template.mime_type ? template.mime_type.to_s : "text/plain", - :disposition => "inline", - :charset => charset, + ct = template.mime_type ? template.mime_type.to_s : "text/plain" + main_type, sub_type = ct.split("/") + @parts << Mail::Part.new( + :content_type => [main_type, sub_type, {:charset => charset}], + :content_disposition => "inline", :body => render_to_body(:_template => template) ) end @@ -553,8 +576,8 @@ module ActionMailer #:nodoc: order = order.collect { |s| s.downcase } parts = parts.sort do |a, b| - a_ct = a.content_type.downcase - b_ct = b.content_type.downcase + a_ct = a.content_type.string.downcase + b_ct = b.content_type.string.downcase a_in = order.include? a_ct b_in = order.include? b_ct @@ -579,7 +602,7 @@ module ActionMailer #:nodoc: end def create_mail - m = TMail::Mail.new + m = Mail.new m.subject, = quote_any_if_necessary(charset, subject) m.to, m.from = quote_any_address_if_necessary(charset, recipients, from) @@ -594,25 +617,43 @@ module ActionMailer #:nodoc: real_content_type, ctype_attrs = parse_content_type if @parts.empty? - m.set_content_type(real_content_type, nil, ctype_attrs) + main_type, sub_type = split_content_type(real_content_type) + m.content_type([main_type, sub_type, ctype_attrs]) m.body = normalize_new_lines(body) elsif @parts.size == 1 && @parts.first.parts.empty? - m.set_content_type(real_content_type, nil, ctype_attrs) + main_type, sub_type = split_content_type(real_content_type) + m.content_type([main_type, sub_type, ctype_attrs]) m.body = normalize_new_lines(@parts.first.body) else @parts.each do |p| - part = (TMail::Mail === p ? p : p.to_mail(self)) - m.parts << part + m.add_part(p) end if real_content_type =~ /multipart/ ctype_attrs.delete "charset" - m.set_content_type(real_content_type, nil, ctype_attrs) + main_type, sub_type = split_content_type(real_content_type) + m.content_type([main_type, sub_type, ctype_attrs]) end end @mail = m end + + def split_content_type(ct) + ct.to_s.split("/") + end + + def parse_content_type(defaults=nil) + if content_type.blank? + return defaults ? + [ defaults.content_type, { 'charset' => defaults.charset } ] : + [ nil, {} ] + end + ctype, *attrs = content_type.split(/;\s*/) + attrs = attrs.inject({}) { |h,s| k,v = s.split(/=/, 2); h[k] = v; h } + [ctype, {"charset" => charset || defaults && defaults.charset}.merge(attrs)] + end + end end |