From 5b0c8a1266016c6418555399da83fbc7210c6734 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Fri, 15 Jan 2010 20:03:45 +1100 Subject: Removing internal delivery agents --- actionmailer/lib/action_mailer/delivery_method.rb | 56 ---------------------- .../lib/action_mailer/delivery_method/file.rb | 21 -------- .../lib/action_mailer/delivery_method/sendmail.rb | 22 --------- .../lib/action_mailer/delivery_method/smtp.rb | 30 ------------ .../lib/action_mailer/delivery_method/test.rb | 12 ----- 5 files changed, 141 deletions(-) delete mode 100644 actionmailer/lib/action_mailer/delivery_method.rb delete mode 100644 actionmailer/lib/action_mailer/delivery_method/file.rb delete mode 100644 actionmailer/lib/action_mailer/delivery_method/sendmail.rb delete mode 100644 actionmailer/lib/action_mailer/delivery_method/smtp.rb delete mode 100644 actionmailer/lib/action_mailer/delivery_method/test.rb (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/delivery_method.rb b/actionmailer/lib/action_mailer/delivery_method.rb deleted file mode 100644 index 4f7d3afc3c..0000000000 --- a/actionmailer/lib/action_mailer/delivery_method.rb +++ /dev/null @@ -1,56 +0,0 @@ -require 'active_support/core_ext/class' - -module ActionMailer - module DeliveryMethod - autoload :File, 'action_mailer/delivery_method/file' - autoload :Sendmail, 'action_mailer/delivery_method/sendmail' - autoload :Smtp, 'action_mailer/delivery_method/smtp' - autoload :Test, 'action_mailer/delivery_method/test' - - # Creates a new DeliveryMethod object according to the given options. - # - # If no arguments are passed to this method, then a new - # ActionMailer::DeliveryMethod::Stmp object will be returned. - # - # If you pass a Symbol as the first argument, then a corresponding - # delivery method class under the ActionMailer::DeliveryMethod namespace - # will be created. - # For example: - # - # ActionMailer::DeliveryMethod.lookup_method(:sendmail) - # # => returns a new ActionMailer::DeliveryMethod::Sendmail object - # - # If the first argument is not a Symbol, then it will simply be returned: - # - # ActionMailer::DeliveryMethod.lookup_method(MyOwnDeliveryMethod.new) - # # => returns MyOwnDeliveryMethod.new - def self.lookup_method(delivery_method) - case delivery_method - when Symbol - method_name = delivery_method.to_s.camelize - method_class = ActionMailer::DeliveryMethod.const_get(method_name) - method_class.new - when nil # default - Smtp.new - else - delivery_method - end - end - - # An abstract delivery method class. There are multiple delivery method classes. - # See the classes under the ActionMailer::DeliveryMethod, e.g. - # ActionMailer::DeliveryMethod::Smtp. - # Smtp is the default delivery method for production - # while Test is used in testing. - # - # each delivery method exposes just one method - # - # delivery_method = ActionMailer::DeliveryMethod::Smtp.new - # delivery_method.perform_delivery(mail) # send the mail via smtp - # - class Method - superclass_delegating_accessor :settings - self.settings = {} - end - end -end diff --git a/actionmailer/lib/action_mailer/delivery_method/file.rb b/actionmailer/lib/action_mailer/delivery_method/file.rb deleted file mode 100644 index 571e32df49..0000000000 --- a/actionmailer/lib/action_mailer/delivery_method/file.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'tmpdir' - -module ActionMailer - module DeliveryMethod - - # A delivery method implementation which writes all mails to a file. - class File < Method - self.settings = { - :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" - } - - def perform_delivery(mail) - FileUtils.mkdir_p settings[:location] - - mail.destinations.uniq.each do |to| - ::File.open(::File.join(settings[:location], to), 'a') { |f| f.write(mail) } - end - end - end - end -end diff --git a/actionmailer/lib/action_mailer/delivery_method/sendmail.rb b/actionmailer/lib/action_mailer/delivery_method/sendmail.rb deleted file mode 100644 index db55af79f1..0000000000 --- a/actionmailer/lib/action_mailer/delivery_method/sendmail.rb +++ /dev/null @@ -1,22 +0,0 @@ -module ActionMailer - module DeliveryMethod - - # A delivery method implementation which sends via sendmail. - class Sendmail < Method - self.settings = { - :location => '/usr/sbin/sendmail', - :arguments => '-i -t' - } - - def perform_delivery(mail) - sendmail_args = settings[:arguments] - sendmail_args += " -f \"#{mail['return-path']}\"" if mail['return-path'] - IO.popen("#{settings[:location]} #{sendmail_args}","w+") do |sm| - sm.print(mail.encoded.gsub(/\r/, '')) - sm.flush - end - end - end - - end -end diff --git a/actionmailer/lib/action_mailer/delivery_method/smtp.rb b/actionmailer/lib/action_mailer/delivery_method/smtp.rb deleted file mode 100644 index af30c498b5..0000000000 --- a/actionmailer/lib/action_mailer/delivery_method/smtp.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'net/smtp' - -module ActionMailer - module DeliveryMethod - # A delivery method implementation which sends via smtp. - class Smtp < Method - self.settings = { - :address => "localhost", - :port => 25, - :domain => 'localhost.localdomain', - :user_name => nil, - :password => nil, - :authentication => nil, - :enable_starttls_auto => true, - } - - def perform_delivery(mail) - destinations = mail.destinations - sender = (mail['return-path'] && mail['return-path'].address) || mail['from'] - - smtp = Net::SMTP.new(settings[:address], settings[:port]) - smtp.enable_starttls_auto if settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto) - smtp.start(settings[:domain], settings[:user_name], settings[:password], - settings[:authentication]) do |smtp| - smtp.sendmail(mail.encoded, sender, destinations) - end - end - end - end -end diff --git a/actionmailer/lib/action_mailer/delivery_method/test.rb b/actionmailer/lib/action_mailer/delivery_method/test.rb deleted file mode 100644 index 6e3239d52a..0000000000 --- a/actionmailer/lib/action_mailer/delivery_method/test.rb +++ /dev/null @@ -1,12 +0,0 @@ -module ActionMailer - module DeliveryMethod - - # A delivery method implementation designed for testing, which just appends each record to the :deliveries array - class Test < Method - def perform_delivery(mail) - ActionMailer::Base.deliveries << mail - end - end - - end -end -- cgit v1.2.3 From 0750304c0117c85f06cf1ea608747b8fda0a0ecd Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Sat, 16 Jan 2010 22:10:11 +1100 Subject: Migrated over to Mail doing delivery. --- actionmailer/lib/action_mailer/base.rb | 87 +++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 22 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index b9a01356ba..89bd45a994 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -355,20 +355,42 @@ module ActionMailer #:nodoc: alias :controller_path :mailer_name class << self - attr_writer :mailer_name - - delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::File, :prefix => :file - delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Sendmail, :prefix => :sendmail - delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Smtp, :prefix => :smtp def mailer_name @mailer_name ||= name.underscore end - alias :controller_path :mailer_name + attr_writer :mailer_name + + def file_settings + @file_settings ||= {:location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"} + end + attr_writer :file_settings - def delivery_method=(method_name) - @delivery_method = ActionMailer::DeliveryMethod.lookup_method(method_name) + def sendmail_settings + @sendmail_settings ||= { :location => '/usr/sbin/sendmail', + :arguments => '-i -t' } end + attr_writer :sendmail_settings + + def smtp_settings + @smtp_settings ||= { :address => "localhost", + :port => 25, + :domain => 'localhost.localdomain', + :user_name => nil, + :password => nil, + :authentication => nil, + :enable_starttls_auto => true } + end + attr_writer :smtp_settings + + def custom_settings + @custom_settings ||= {} + end + attr_writer :custom_settings + + attr_writer :delivery_method + + alias :controller_path :mailer_name def respond_to?(method_symbol, include_private = false) #:nodoc: matches_dynamic_method?(method_symbol) || super @@ -413,7 +435,35 @@ module ActionMailer #:nodoc: # email.set_some_obscure_header "frobnicate" # MyMailer.deliver(email) def deliver(mail) - new.deliver!(mail) + raise "no mail object available for delivery!" unless mail + + begin + ActiveSupport::Notifications.instrument("action_mailer.deliver", + :mailer => self.name) do |payload| + set_payload_for_mail(payload, mail) + mail.delivery_method delivery_method, delivery_settings + if @@perform_deliveries + mail.deliver! + self.deliveries << mail + end + end + rescue Exception => e # Net::SMTP errors or sendmail pipe errors + raise e if raise_delivery_errors + end + + mail + end + + # Get the delivery settings set. This is set using the :smtp_settings, + # :sendmail_settings, :file_settings or :custom_setings + # options hashes. You can set :custom_settings if you are providing + # your own Custom Delivery Method and want to pass options to it. + def delivery_settings + if [:smtp, :sendmail, :file].include?(delivery_method) + instance_variable_get("@#{delivery_method}_settings") + else + @custom_settings + end end def template_root @@ -506,19 +556,7 @@ module ActionMailer #:nodoc: # object (from the create! method). If no cached mail object exists, and # no alternate has been given as the parameter, this will fail. def deliver!(mail = @mail) - raise "no mail object available for delivery!" unless mail - - begin - ActiveSupport::Notifications.instrument("action_mailer.deliver", - :template => template, :mailer => self.class.name) do |payload| - self.class.set_payload_for_mail(payload, mail) - self.delivery_method.perform_delivery(mail) if perform_deliveries - end - rescue Exception => e # Net::SMTP errors or sendmail pipe errors - raise e if raise_delivery_errors - end - - mail + self.class.deliver(mail) end private @@ -533,6 +571,11 @@ module ActionMailer #:nodoc: @mime_version ||= @@default_mime_version.dup if @@default_mime_version @mailer_name ||= self.class.mailer_name.dup + @delivery_method = self.class.delivery_method + @smtp_settings = self.class.smtp_settings.dup + @sendmail_settings = self.class.sendmail_settings.dup + @file_settings = self.class.file_settings.dup + @custom_settings = self.class.custom_settings.dup @template ||= method_name @parts ||= [] -- cgit v1.2.3 From ccb7d9def3c20037c9ed5989d8cae1ed68763f4f Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Sun, 17 Jan 2010 23:27:59 +1100 Subject: Fixing up base to refactor settings --- actionmailer/lib/action_mailer/base.rb | 70 +++++++++++++--------------------- 1 file changed, 27 insertions(+), 43 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 89bd45a994..c1cce73303 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -361,39 +361,26 @@ module ActionMailer #:nodoc: end attr_writer :mailer_name - def file_settings - @file_settings ||= {:location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"} - end - attr_writer :file_settings - - def sendmail_settings - @sendmail_settings ||= { :location => '/usr/sbin/sendmail', - :arguments => '-i -t' } - end - attr_writer :sendmail_settings - - def smtp_settings - @smtp_settings ||= { :address => "localhost", - :port => 25, - :domain => 'localhost.localdomain', - :user_name => nil, - :password => nil, - :authentication => nil, - :enable_starttls_auto => true } - end - attr_writer :smtp_settings - - def custom_settings - @custom_settings ||= {} + def delivery_settings + @delivery_settings ||= { :file => { :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" }, + :smtp => { :address => "localhost", + :port => 25, + :domain => 'localhost.localdomain', + :user_name => nil, + :password => nil, + :authentication => nil, + :enable_starttls_auto => true }, + :sendmail => { :location => '/usr/sbin/sendmail', + :arguments => '-i -t' } + } end - attr_writer :custom_settings attr_writer :delivery_method alias :controller_path :mailer_name def respond_to?(method_symbol, include_private = false) #:nodoc: - matches_dynamic_method?(method_symbol) || super + matches_dynamic_method?(method_symbol) || matches_settings_method?(method_symbol) || super end def method_missing(method_symbol, *parameters) #:nodoc: @@ -404,6 +391,8 @@ module ActionMailer #:nodoc: when 'new' then nil else super end + elsif match = matches_settings_method?(method_symbol) + delivery_settings[match[1].to_sym] = parameters[0] else super end @@ -441,7 +430,8 @@ module ActionMailer #:nodoc: ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| set_payload_for_mail(payload, mail) - mail.delivery_method delivery_method, delivery_settings + + mail.delivery_method delivery_method, get_delivery_settings(delivery_method) if @@perform_deliveries mail.deliver! self.deliveries << mail @@ -454,18 +444,6 @@ module ActionMailer #:nodoc: mail end - # Get the delivery settings set. This is set using the :smtp_settings, - # :sendmail_settings, :file_settings or :custom_setings - # options hashes. You can set :custom_settings if you are providing - # your own Custom Delivery Method and want to pass options to it. - def delivery_settings - if [:smtp, :sendmail, :file].include?(delivery_method) - instance_variable_get("@#{delivery_method}_settings") - else - @custom_settings - end - end - def template_root self.view_paths && self.view_paths.first end @@ -487,6 +465,16 @@ module ActionMailer #:nodoc: end private + + def get_delivery_settings(method) #:nodoc: + method.is_a?(Symbol) ? delivery_settings[method] : delivery_settings[:custom] + end + + def matches_settings_method?(method_name) #:nodoc: + method_name = method_name.to_s + delivery_method.is_a?(Symbol) ? method = delivery_method : method = :custom + /(file|sendmail|smtp)_settings$/.match(method_name) + end def matches_dynamic_method?(method_name) #:nodoc: method_name = method_name.to_s @@ -572,10 +560,6 @@ module ActionMailer #:nodoc: @mailer_name ||= self.class.mailer_name.dup @delivery_method = self.class.delivery_method - @smtp_settings = self.class.smtp_settings.dup - @sendmail_settings = self.class.sendmail_settings.dup - @file_settings = self.class.file_settings.dup - @custom_settings = self.class.custom_settings.dup @template ||= method_name @parts ||= [] -- cgit v1.2.3 From d201d39437c754fa38b14dd4168fab601399918e Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Tue, 19 Jan 2010 23:09:46 +1100 Subject: latest updates --- actionmailer/lib/action_mailer/base.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index e0a99fa00c..fd251ff223 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -354,6 +354,8 @@ module ActionMailer #:nodoc: # Alias controller_path to mailer_name so render :partial in views work. alias :controller_path :mailer_name + class_inheritable_accessor :delivery_method + class << self def mailer_name @@ -361,21 +363,17 @@ module ActionMailer #:nodoc: end attr_writer :mailer_name + # Mail uses the same defaults as Rails, except for the file delivery method + # save location so we just add this here. def delivery_settings - @delivery_settings ||= { :file => { :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" }, - :smtp => { :address => "localhost", - :port => 25, - :domain => 'localhost.localdomain', - :user_name => nil, - :password => nil, - :authentication => nil, - :enable_starttls_auto => true }, - :sendmail => { :location => '/usr/sbin/sendmail', - :arguments => '-i -t' } - } + @delivery_settings ||= {:file => {:location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"}} end - attr_writer :delivery_method + # Setup the default settings for Mail delivery + Mail.defaults do + method = ActionMailer::Base.delivery_method ||= :smtp + delivery_method method + end alias :controller_path :mailer_name -- cgit v1.2.3 From c8e2998701653df9035232778d60f18c4a07abb4 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Tue, 19 Jan 2010 23:36:49 +1100 Subject: First pass on fixing delivery method --- actionmailer/lib/action_mailer/base.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index fd251ff223..082560e695 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -354,7 +354,8 @@ module ActionMailer #:nodoc: # Alias controller_path to mailer_name so render :partial in views work. alias :controller_path :mailer_name - class_inheritable_accessor :delivery_method + superclass_delegating_accessor :delivery_method + self.delivery_method = :smtp class << self @@ -369,12 +370,6 @@ module ActionMailer #:nodoc: @delivery_settings ||= {:file => {:location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"}} end - # Setup the default settings for Mail delivery - Mail.defaults do - method = ActionMailer::Base.delivery_method ||= :smtp - delivery_method method - end - alias :controller_path :mailer_name def respond_to?(method_symbol, include_private = false) #:nodoc: @@ -390,6 +385,7 @@ module ActionMailer #:nodoc: else super end elsif match = matches_settings_method?(method_symbol) + # TODO Deprecation warning delivery_settings[match[1].to_sym] = parameters[0] else super @@ -463,7 +459,7 @@ module ActionMailer #:nodoc: end private - + def get_delivery_settings(method) #:nodoc: method.is_a?(Symbol) ? delivery_settings[method] : delivery_settings[:custom] end -- cgit v1.2.3 From c1848f9736d9a4a45181642106acecb6a83a45a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 19 Jan 2010 14:28:04 +0100 Subject: Get all tests passing. --- actionmailer/lib/action_mailer/base.rb | 36 ++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 082560e695..5ece35e69b 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -367,7 +367,29 @@ module ActionMailer #:nodoc: # Mail uses the same defaults as Rails, except for the file delivery method # save location so we just add this here. def delivery_settings - @delivery_settings ||= {:file => {:location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"}} + @@delivery_settings ||= begin + hash = Hash.new { |h,k| h[k] = {} } + hash[:file] = { + :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" + } + + hash[:smtp] = { + :address => "localhost", + :port => 25, + :domain => 'localhost.localdomain', + :user_name => nil, + :password => nil, + :authentication => nil, + :enable_starttls_auto => true + } + + hash[:sendmail] = { + :location => '/usr/sbin/sendmail', + :arguments => '-i -t' + } + + hash + end end alias :controller_path :mailer_name @@ -386,7 +408,11 @@ module ActionMailer #:nodoc: end elsif match = matches_settings_method?(method_symbol) # TODO Deprecation warning - delivery_settings[match[1].to_sym] = parameters[0] + if match[2] + delivery_settings[match[1].to_sym] = parameters[0] + else + delivery_settings[match[1].to_sym] + end else super end @@ -461,13 +487,11 @@ module ActionMailer #:nodoc: private def get_delivery_settings(method) #:nodoc: - method.is_a?(Symbol) ? delivery_settings[method] : delivery_settings[:custom] + delivery_settings[method] end def matches_settings_method?(method_name) #:nodoc: - method_name = method_name.to_s - delivery_method.is_a?(Symbol) ? method = delivery_method : method = :custom - /(file|sendmail|smtp)_settings$/.match(method_name) + /(\w+)_settings(=)?$/.match(method_name.to_s) end def matches_dynamic_method?(method_name) #:nodoc: -- cgit v1.2.3 From e10f51b6b7b6260824cc86085be49cae216cf06c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 19 Jan 2010 15:34:58 +0100 Subject: Refactor delivery methods. --- actionmailer/lib/action_mailer/base.rb | 81 +++------------------- actionmailer/lib/action_mailer/delivery_methods.rb | 71 +++++++++++++++++++ actionmailer/lib/action_mailer/deprecated_body.rb | 10 +-- 3 files changed, 88 insertions(+), 74 deletions(-) create mode 100644 actionmailer/lib/action_mailer/delivery_methods.rb (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 5ece35e69b..be6d93316f 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -264,6 +264,8 @@ module ActionMailer #:nodoc: helper ActionMailer::MailHelper include ActionMailer::DeprecatedBody + include ActionMailer::DeliveryMethods + private_class_method :new #:nodoc: @@raise_delivery_errors = true @@ -354,9 +356,6 @@ module ActionMailer #:nodoc: # Alias controller_path to mailer_name so render :partial in views work. alias :controller_path :mailer_name - superclass_delegating_accessor :delivery_method - self.delivery_method = :smtp - class << self def mailer_name @@ -364,38 +363,10 @@ module ActionMailer #:nodoc: end attr_writer :mailer_name - # Mail uses the same defaults as Rails, except for the file delivery method - # save location so we just add this here. - def delivery_settings - @@delivery_settings ||= begin - hash = Hash.new { |h,k| h[k] = {} } - hash[:file] = { - :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" - } - - hash[:smtp] = { - :address => "localhost", - :port => 25, - :domain => 'localhost.localdomain', - :user_name => nil, - :password => nil, - :authentication => nil, - :enable_starttls_auto => true - } - - hash[:sendmail] = { - :location => '/usr/sbin/sendmail', - :arguments => '-i -t' - } - - hash - end - end - alias :controller_path :mailer_name def respond_to?(method_symbol, include_private = false) #:nodoc: - matches_dynamic_method?(method_symbol) || matches_settings_method?(method_symbol) || super + matches_dynamic_method?(method_symbol) || super end def method_missing(method_symbol, *parameters) #:nodoc: @@ -406,13 +377,6 @@ module ActionMailer #:nodoc: when 'new' then nil else super end - elsif match = matches_settings_method?(method_symbol) - # TODO Deprecation warning - if match[2] - delivery_settings[match[1].to_sym] = parameters[0] - else - delivery_settings[match[1].to_sym] - end else super end @@ -447,11 +411,13 @@ module ActionMailer #:nodoc: raise "no mail object available for delivery!" unless mail begin - ActiveSupport::Notifications.instrument("action_mailer.deliver", - :mailer => self.name) do |payload| + ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| set_payload_for_mail(payload, mail) - - mail.delivery_method delivery_method, get_delivery_settings(delivery_method) + + # TODO Move me to the instance + mail.delivery_method delivery_methods[delivery_method], + delivery_settings[delivery_method] + if @@perform_deliveries mail.deliver! self.deliveries << mail @@ -486,25 +452,12 @@ module ActionMailer #:nodoc: private - def get_delivery_settings(method) #:nodoc: - delivery_settings[method] - end - - def matches_settings_method?(method_name) #:nodoc: - /(\w+)_settings(=)?$/.match(method_name.to_s) - end - def matches_dynamic_method?(method_name) #:nodoc: method_name = method_name.to_s /^(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name) end end - # Configure delivery method. Check ActionMailer::DeliveryMethod for more - # instructions. - superclass_delegating_reader :delivery_method - self.delivery_method = :smtp - # 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. @@ -534,20 +487,6 @@ module ActionMailer #:nodoc: part(params, &block) end - # Allow you to set assigns for your template: - # - # body :greetings => "Hi" - # - # Will make @greetings available in the template to be rendered. - def body(object=nil) - returning(super) do # Run deprecation hooks - if object.is_a?(Hash) - @assigns_set = true - object.each { |k, v| instance_variable_set(:"@#{k}", v) } - end - end - end - # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer # will be initialized according to the named method. If not, the mailer will # remain uninitialized (useful when you only need to invoke the "receive" @@ -591,6 +530,8 @@ module ActionMailer #:nodoc: # render_message "special_message" # render_message :template => "special_message" # render_message :inline => "<%= 'Hi!' %>" + # + # TODO Deprecate me def render_message(object) case object when String diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb new file mode 100644 index 0000000000..c8c4148353 --- /dev/null +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -0,0 +1,71 @@ +module ActionMailer + # This modules makes a DSL for adding delivery methods to ActionMailer + module DeliveryMethods + extend ActiveSupport::Concern + + included do + add_delivery_method :smtp, Mail::SMTP, + :address => "localhost", + :port => 25, + :domain => 'localhost.localdomain', + :user_name => nil, + :password => nil, + :authentication => nil, + :enable_starttls_auto => true + + add_delivery_method :file, Mail::FileDelivery, + :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" + + add_delivery_method :sendmail, Mail::Sendmail, + :location => '/usr/sbin/sendmail', + :arguments => '-i -t' + + add_delivery_method :test, Mail::TestMailer + + superclass_delegating_reader :delivery_method + self.delivery_method = :smtp + end + + module ClassMethods + def delivery_settings + @@delivery_settings ||= Hash.new { |h,k| h[k] = {} } + end + + def delivery_methods + @@delivery_methods ||= {} + end + + def delivery_method=(method) + raise ArgumentError, "Unknown delivery method #{method.inspect}" unless delivery_methods[method] + @delivery_method = method + end + + def add_delivery_method(symbol, klass, default_options={}) + self.delivery_methods[symbol] = klass + self.delivery_settings[symbol] = default_options + end + + def respond_to?(method_symbol, include_private = false) #:nodoc: + matches_settings_method?(method_symbol) || super + end + + protected + + def method_missing(method_symbol, *parameters) #:nodoc: + if match = matches_settings_method?(method_symbol) + if match[2] + delivery_settings[match[1].to_sym] = parameters[0] + else + delivery_settings[match[1].to_sym] + end + else + super + end + end + + def matches_settings_method?(method_name) #:nodoc: + /(#{delivery_methods.keys.join('|')})_settings(=)?$/.match(method_name.to_s) + end + end + end +end \ No newline at end of file diff --git a/actionmailer/lib/action_mailer/deprecated_body.rb b/actionmailer/lib/action_mailer/deprecated_body.rb index 5379b33a54..c82610014f 100644 --- a/actionmailer/lib/action_mailer/deprecated_body.rb +++ b/actionmailer/lib/action_mailer/deprecated_body.rb @@ -1,6 +1,6 @@ module ActionMailer # TODO Remove this module all together in a next release. Ensure that super - # hooks and @assigns_set in ActionMailer::Base are removed as well. + # hooks in ActionMailer::Base are removed as well. module DeprecatedBody extend ActionMailer::AdvAttrAccessor @@ -22,12 +22,14 @@ module ActionMailer end def create_parts - if String === @body && !defined?(@assigns_set) + if String === @body ActiveSupport::Deprecation.warn('body(String) is deprecated. To set the body with a text ' << 'call render(:text => "body")', caller[0,10]) self.response_body = @body - elsif self.response_body - @body = self.response_body + elsif @body.is_a?(Hash) && !@body.empty? + ActiveSupport::Deprecation.warn('body(Hash) is deprecated. Use instance variables to define ' << + 'assigns in your view', caller[0,10]) + @body.each { |k, v| instance_variable_set(:"@#{k}", v) } end end -- cgit v1.2.3 From 10c509fbfa70758ece26e8876d1c5c28f659f2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Wed, 20 Jan 2010 22:25:09 +1100 Subject: Moved old API into deprecated_api.rb in preparation for new Rails 3 Mailer API --- .../lib/action_mailer/adv_attr_accessor.rb | 2 + actionmailer/lib/action_mailer/base.rb | 156 +------------------- actionmailer/lib/action_mailer/deprecated_api.rb | 163 +++++++++++++++++++++ 3 files changed, 168 insertions(+), 153 deletions(-) create mode 100644 actionmailer/lib/action_mailer/deprecated_api.rb (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/adv_attr_accessor.rb b/actionmailer/lib/action_mailer/adv_attr_accessor.rb index be6b1feca9..91992ed839 100644 --- a/actionmailer/lib/action_mailer/adv_attr_accessor.rb +++ b/actionmailer/lib/action_mailer/adv_attr_accessor.rb @@ -1,6 +1,8 @@ module ActionMailer module AdvAttrAccessor #:nodoc: def adv_attr_accessor(*names) + + # TODO: ActiveSupport::Deprecation.warn() names.each do |name| ivar = "@#{name}" diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index e105beef63..9df482cdf6 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -263,9 +263,11 @@ module ActionMailer #:nodoc: helper ActionMailer::MailHelper include ActionMailer::DeprecatedBody + include ActionMailer::DeprecatedApi include ActionMailer::DeliveryMethods + private_class_method :new #:nodoc: @@raise_delivery_errors = true @@ -459,41 +461,13 @@ module ActionMailer #:nodoc: end end - # 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) - ActiveSupport::Deprecation.warn('Passing custom headers with :headers => {} is deprecated. ' << - 'Please just pass in custom headers directly.', caller[0,10]) - 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) - super # Run deprecation hooks - - params = { :content_type => params } if String === params - params = { :content_disposition => "attachment", - :content_transfer_encoding => "base64" }.merge(params) - - part(params, &block) - end - # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer # will be initialized according to the named method. If not, the mailer will # remain uninitialized (useful when you only need to invoke the "receive" # method, for instance). def initialize(method_name=nil, *args) super() + @mail = Mail.new process(method_name, *args) if method_name end @@ -521,129 +495,5 @@ module ActionMailer #:nodoc: self.class.deliver(mail) end - private - - # Render a message but does not set it as mail body. Useful for rendering - # data for part and attachments. - # - # Examples: - # - # render_message "special_message" - # render_message :template => "special_message" - # render_message :inline => "<%= 'Hi!' %>" - # - # TODO Deprecate me - def render_message(object) - case object - when String - render_to_body(:template => object) - else - render_to_body(object) - end - end - - # Set up the default values for the various instance variables of this - # mailer. Subclasses may override this method to provide different - # defaults. - def initialize_defaults(method_name) #:nodoc: - @charset ||= @@default_charset.dup - @content_type ||= @@default_content_type.dup - @implicit_parts_order ||= @@default_implicit_parts_order.dup - @mime_version ||= @@default_mime_version.dup if @@default_mime_version - - @mailer_name ||= self.class.mailer_name.dup - @delivery_method = self.class.delivery_method - @template ||= method_name - - @parts ||= [] - @headers ||= {} - @sent_on ||= Time.now - - super # Run deprecation hooks - end - - def create_parts #:nodoc: - super # Run deprecation hooks - - if String === response_body - @parts.unshift create_inline_part(response_body) - else - self.class.template_root.find_all(@template, {}, @mailer_name).each do |template| - @parts << create_inline_part(render_to_body(:_template => template), template.mime_type) - end - - if @parts.size > 1 - @content_type = "multipart/alternative" if @content_type !~ /^multipart/ - end - - # If this is a multipart e-mail add the mime_version if it is not - # already set. - @mime_version ||= "1.0" if !@parts.empty? - end - end - - def create_inline_part(body, mime_type=nil) #:nodoc: - ct = mime_type || "text/plain" - main_type, sub_type = split_content_type(ct.to_s) - - Mail::Part.new( - :content_type => [main_type, sub_type, {:charset => charset}], - :content_disposition => "inline", - :body => body - ) - end - - def create_mail #:nodoc: - m = Mail.new - - m.subject, = quote_any_if_necessary(charset, subject) - m.to, m.from = quote_any_address_if_necessary(charset, recipients, from) - m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil? - m.cc = quote_address_if_necessary(cc, charset) unless cc.nil? - m.reply_to = quote_address_if_necessary(reply_to, charset) unless reply_to.nil? - m.mime_version = mime_version unless mime_version.nil? - m.date = sent_on.to_time rescue sent_on if sent_on - - headers.each { |k, v| m[k] = v } - - real_content_type, ctype_attrs = parse_content_type - main_type, sub_type = split_content_type(real_content_type) - - if @parts.size == 1 && @parts.first.parts.empty? - m.content_type([main_type, sub_type, ctype_attrs]) - m.body = @parts.first.body.encoded - else - @parts.each do |p| - m.add_part(p) - end - - m.body.set_sort_order(@implicit_parts_order) - m.body.sort_parts! - - if real_content_type =~ /multipart/ - ctype_attrs.delete "charset" - m.content_type([main_type, sub_type, ctype_attrs]) - end - end - - m.content_transfer_encoding = '8bit' unless m.body.only_us_ascii? - - @mail = m - end - - def split_content_type(ct) #:nodoc: - ct.to_s.split("/") - end - - def parse_content_type(defaults=nil) #:nodoc: - if @content_type.blank? - [ nil, {} ] - else - ctype, *attrs = @content_type.split(/;\s*/) - attrs = attrs.inject({}) { |h,s| k,v = s.split(/\=/, 2); h[k] = v; h } - [ctype, {"charset" => @charset}.merge(attrs)] - end - end - end end diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb new file mode 100644 index 0000000000..3334b47987 --- /dev/null +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -0,0 +1,163 @@ +module ActionMailer + # TODO Remove this module all together in Rails 3.1. Ensure that super + # hooks in ActionMailer::Base are removed as well. + # + # Moved here to allow us to add the new Mail API + module DeprecatedApi + extend ActionMailer::AdvAttrAccessor + + # 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) + ActiveSupport::Deprecation.warn('Passing custom headers with :headers => {} is deprecated. ' << + 'Please just pass in custom headers directly.', caller[0,10]) + 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) + super # Run deprecation hooks + + params = { :content_type => params } if String === params + params = { :content_disposition => "attachment", + :content_transfer_encoding => "base64" }.merge(params) + + part(params, &block) + end + + private + + def create_mail #:nodoc: + m = @mail + + m.subject, = quote_any_if_necessary(charset, subject) + m.to, m.from = quote_any_address_if_necessary(charset, recipients, from) + m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil? + m.cc = quote_address_if_necessary(cc, charset) unless cc.nil? + m.reply_to = quote_address_if_necessary(reply_to, charset) unless reply_to.nil? + m.mime_version = mime_version unless mime_version.nil? + m.date = sent_on.to_time rescue sent_on if sent_on + + headers.each { |k, v| m[k] = v } + + real_content_type, ctype_attrs = parse_content_type + main_type, sub_type = split_content_type(real_content_type) + + if @parts.size == 1 && @parts.first.parts.empty? + m.content_type([main_type, sub_type, ctype_attrs]) + m.body = @parts.first.body.encoded + else + @parts.each do |p| + m.add_part(p) + end + + m.body.set_sort_order(@implicit_parts_order) + m.body.sort_parts! + + if real_content_type =~ /multipart/ + ctype_attrs.delete "charset" + m.content_type([main_type, sub_type, ctype_attrs]) + end + end + + m.content_transfer_encoding = '8bit' unless m.body.only_us_ascii? + + @mail + end + + # Render a message but does not set it as mail body. Useful for rendering + # data for part and attachments. + # + # Examples: + # + # render_message "special_message" + # render_message :template => "special_message" + # render_message :inline => "<%= 'Hi!' %>" + # + # TODO Deprecate me + def render_message(object) + case object + when String + render_to_body(:template => object) + else + render_to_body(object) + end + end + + # Set up the default values for the various instance variables of this + # mailer. Subclasses may override this method to provide different + # defaults. + def initialize_defaults(method_name) #:nodoc: + @charset ||= self.class.default_charset.dup + @content_type ||= self.class.default_content_type.dup + @implicit_parts_order ||= self.class.default_implicit_parts_order.dup + @mime_version ||= self.class.default_mime_version.dup if self.class.default_mime_version + + @mailer_name ||= self.class.mailer_name.dup + @delivery_method = self.class.delivery_method + @template ||= method_name + + @parts ||= [] + @headers ||= {} + @sent_on ||= Time.now + + super # Run deprecation hooks + end + + def create_parts #:nodoc: + super # Run deprecation hooks + + if String === response_body + @parts.unshift create_inline_part(response_body) + else + self.class.template_root.find_all(@template, {}, @mailer_name).each do |template| + @parts << create_inline_part(render_to_body(:_template => template), template.mime_type) + end + + if @parts.size > 1 + @content_type = "multipart/alternative" if @content_type !~ /^multipart/ + end + + # If this is a multipart e-mail add the mime_version if it is not + # already set. + @mime_version ||= "1.0" if !@parts.empty? + end + end + + def create_inline_part(body, mime_type=nil) #:nodoc: + ct = mime_type || "text/plain" + main_type, sub_type = split_content_type(ct.to_s) + + Mail::Part.new( + :content_type => [main_type, sub_type, {:charset => charset}], + :content_disposition => "inline", + :body => body + ) + end + + def split_content_type(ct) #:nodoc: + ct.to_s.split("/") + end + + def parse_content_type(defaults=nil) #:nodoc: + if @content_type.blank? + [ nil, {} ] + else + ctype, *attrs = @content_type.split(/;\s*/) + attrs = attrs.inject({}) { |h,s| k,v = s.split(/\=/, 2); h[k] = v; h } + [ctype, {"charset" => @charset}.merge(attrs)] + end + end + + end +end \ No newline at end of file -- cgit v1.2.3 From c34cfcc29f705c95c2218889cbec1898e008335d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Wed, 20 Jan 2010 23:46:59 +1100 Subject: Created mail method for new API --- actionmailer/lib/action_mailer/base.rb | 59 ++++++++++++++++++------ actionmailer/lib/action_mailer/deprecated_api.rb | 4 +- 2 files changed, 47 insertions(+), 16 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 9df482cdf6..8825bd35f9 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -297,7 +297,7 @@ module ActionMailer #:nodoc: @@default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] cattr_accessor :default_implicit_parts_order - @@protected_instance_variables = %w(@parts @mail) + @@protected_instance_variables = %w(@parts @message) cattr_reader :protected_instance_variables # Specify the BCC addresses for the message @@ -352,8 +352,8 @@ module ActionMailer #:nodoc: # location, you can use this to specify that location. adv_attr_accessor :mailer_name - # Expose the internal mail - attr_reader :mail + # Expose the internal Mail message + attr_reader :message # Alias controller_path to mailer_name so render :partial in views work. alias :controller_path :mailer_name @@ -374,7 +374,7 @@ module ActionMailer #:nodoc: def method_missing(method_symbol, *parameters) #:nodoc: if match = matches_dynamic_method?(method_symbol) case match[1] - when 'create' then new(match[2], *parameters).mail + when 'create' then new(match[2], *parameters).message when 'deliver' then new(match[2], *parameters).deliver! when 'new' then nil else super @@ -461,13 +461,40 @@ module ActionMailer #:nodoc: end end + def mail(headers = {}) + # Guard flag to prevent both the old and the new API from firing + # TODO - Move this @mail_was_called flag into deprecated_api.rb + @mail_was_called = true + + m = @message + + m.content_type ||= headers[:content_type] || @@default_content_type + m.charset ||= headers[:charset] || @@default_charset + m.mime_version ||= headers[:mime_version] || @@default_mime_version + + m.subject = quote_if_necessary(headers[:subject], m.charset) if headers[:subject] + m.to = quote_address_if_necessary(headers[:to], m.charset) if headers[:to] + m.from = quote_address_if_necessary(headers[:from], m.charset) if headers[:from] + m.cc = quote_address_if_necessary(headers[:cc], m.charset) if headers[:cc] + m.bcc = quote_address_if_necessary(headers[:bcc], m.charset) if headers[:bcc] + m.reply_to = quote_address_if_necessary(headers[:reply_to], m.charset) if headers[:reply_to] + m.mime_version = headers[:mime_version] if headers[:mime_version] + m.date = headers[:date] if headers[:date] + + m.body.set_sort_order(headers[:parts_order] || @@default_implicit_parts_order) + + # TODO: m.body.sort_parts! + m + end + # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer # will be initialized according to the named method. If not, the mailer will # remain uninitialized (useful when you only need to invoke the "receive" # method, for instance). def initialize(method_name=nil, *args) super() - @mail = Mail.new + @mail_was_called = false + @message = Mail.new process(method_name, *args) if method_name end @@ -475,23 +502,27 @@ module ActionMailer #:nodoc: # rendered and a new Mail object created. def process(method_name, *args) initialize_defaults(method_name) + super + + unless @mail_was_called + # Create e-mail parts + create_parts - # Create e-mail parts - create_parts + # Set the subject if not set yet + @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], + :default => method_name.humanize) - # Set the subject if not set yet - @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], - :default => method_name.humanize) - - # Build the mail object itself - create_mail + # Build the mail object itself + create_mail + end + @message end # Delivers a Mail object. By default, it delivers the cached mail # object (from the create! method). If no cached mail object exists, and # no alternate has been given as the parameter, this will fail. - def deliver!(mail = @mail) + def deliver!(mail = @message) self.class.deliver(mail) end diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 3334b47987..e11dd4c46a 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -38,7 +38,7 @@ module ActionMailer private def create_mail #:nodoc: - m = @mail + m = @message m.subject, = quote_any_if_necessary(charset, subject) m.to, m.from = quote_any_address_if_necessary(charset, recipients, from) @@ -72,7 +72,7 @@ module ActionMailer m.content_transfer_encoding = '8bit' unless m.body.only_us_ascii? - @mail + @message end # Render a message but does not set it as mail body. Useful for rendering -- cgit v1.2.3 From d3da87ce771845f99bbdc04d6d6587b22655b063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Thu, 21 Jan 2010 00:10:22 +1100 Subject: Mail method accepting all headers set via the hash --- actionmailer/lib/action_mailer/base.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 8825bd35f9..39ddafe7fe 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/class' +require "active_support/core_ext/module/delegation" require 'mail' require 'action_mailer/tmail_compat' @@ -267,7 +268,6 @@ module ActionMailer #:nodoc: include ActionMailer::DeliveryMethods - private_class_method :new #:nodoc: @@raise_delivery_errors = true @@ -355,6 +355,9 @@ module ActionMailer #:nodoc: # Expose the internal Mail message attr_reader :message + # Pass calls to headers and attachment to the Mail#Message instance + delegate :headers, :attachments, :to => :@message + # Alias controller_path to mailer_name so render :partial in views work. alias :controller_path :mailer_name @@ -478,7 +481,6 @@ module ActionMailer #:nodoc: m.cc = quote_address_if_necessary(headers[:cc], m.charset) if headers[:cc] m.bcc = quote_address_if_necessary(headers[:bcc], m.charset) if headers[:bcc] m.reply_to = quote_address_if_necessary(headers[:reply_to], m.charset) if headers[:reply_to] - m.mime_version = headers[:mime_version] if headers[:mime_version] m.date = headers[:date] if headers[:date] m.body.set_sort_order(headers[:parts_order] || @@default_implicit_parts_order) -- cgit v1.2.3 From 3829f9ecfd6fcd54edbc15f624ee3b68f6dae135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Thu, 21 Jan 2010 20:03:55 +1100 Subject: Adding tests for attachments['blah.rb'] = {} et al --- actionmailer/lib/action_mailer/deprecated_api.rb | 12 +++++++++++- actionmailer/lib/action_mailer/tmail_compat.rb | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index e11dd4c46a..d096ea6180 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -19,6 +19,7 @@ module ActionMailer end part = Mail::Part.new(params) + yield part if block_given? @parts << part end @@ -29,7 +30,16 @@ module ActionMailer super # Run deprecation hooks params = { :content_type => params } if String === params - params = { :content_disposition => "attachment", + + if filename = params.delete(:filename) + content_disposition = "attachment; filename=\"#{File.basename(filename)}\"" + else + content_disposition = "attachment" + end + + params[:content] = params.delete(:data) if params[:data] + + params = { :content_disposition => content_disposition, :content_transfer_encoding => "base64" }.merge(params) part(params, &block) diff --git a/actionmailer/lib/action_mailer/tmail_compat.rb b/actionmailer/lib/action_mailer/tmail_compat.rb index 2fd25ff145..94f21c7cf3 100644 --- a/actionmailer/lib/action_mailer/tmail_compat.rb +++ b/actionmailer/lib/action_mailer/tmail_compat.rb @@ -16,5 +16,10 @@ module Mail end end + def original_filename + STDERR.puts("Message#original_filename is deprecated, please call Message#filename.\n#{caller}") + filename + end + end end \ No newline at end of file -- cgit v1.2.3 From 12c001fec449864db64eca9ba3a477a7da30b2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 12:46:19 +1100 Subject: Updating deprecated API to sanitize old style attachments hash to work with new mail.attachments method --- actionmailer/lib/action_mailer/deprecated_api.rb | 45 ++++++++++++++++++------ 1 file changed, 35 insertions(+), 10 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index d096ea6180..19c94aee8d 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -30,23 +30,48 @@ module ActionMailer super # Run deprecation hooks params = { :content_type => params } if String === params - - if filename = params.delete(:filename) - content_disposition = "attachment; filename=\"#{File.basename(filename)}\"" + + if params[:filename] + params = normalize_file_hash(params) else - content_disposition = "attachment" + params = normalize_nonfile_hash(params) end - - params[:content] = params.delete(:data) if params[:data] - - params = { :content_disposition => content_disposition, - :content_transfer_encoding => "base64" }.merge(params) - part(params, &block) end private + def normalize_nonfile_hash(params) + content_disposition = "attachment;" + + mime_type = params.delete(:mime_type) + + if content_type = params.delete(:content_type) + content_type = "#{mime_type || content_type};" + end + + params[:body] = params.delete(:data) if params[:data] + + { :content_type => content_type, + :content_disposition => content_disposition }.merge(params) + end + + def normalize_file_hash(params) + filename = File.basename(params.delete(:filename)) + content_disposition = "attachment; filename=\"#{File.basename(filename)}\"" + + mime_type = params.delete(:mime_type) + + if (content_type = params.delete(:content_type)) && (content_type !~ /filename=/) + content_type = "#{mime_type || content_type}; filename=\"#{filename}\"" + end + + params[:body] = params.delete(:data) if params[:data] + + { :content_type => content_type, + :content_disposition => content_disposition }.merge(params) + end + def create_mail #:nodoc: m = @message -- cgit v1.2.3 From 77986f6bdb18414d730fa1ec686365dff26bb4fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 12:49:13 +1100 Subject: Added use of AS::Notifications for tmail_compat.rb --- actionmailer/lib/action_mailer/tmail_compat.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/tmail_compat.rb b/actionmailer/lib/action_mailer/tmail_compat.rb index 94f21c7cf3..da13aac677 100644 --- a/actionmailer/lib/action_mailer/tmail_compat.rb +++ b/actionmailer/lib/action_mailer/tmail_compat.rb @@ -2,14 +2,16 @@ module Mail class Message def set_content_type(*args) - STDERR.puts("Message#set_content_type is deprecated, please just call Message#content_type with the same arguments.\n#{caller}") + ActiveSupport::Deprecation.warn('Message#set_content_type is deprecated, please just call ' << + 'Message#content_type with the same arguments.', caller[0,10]) content_type(*args) end alias :old_transfer_encoding :transfer_encoding def transfer_encoding(value = nil) if value - STDERR.puts("Message#transfer_encoding is deprecated, please call Message#content_transfer_encoding with the same arguments.\n#{caller}") + ActiveSupport::Deprecation.warn('Message#transfer_encoding is deprecated, please call ' << + 'Message#content_transfer_encoding with the same arguments.', caller[0,10]) content_transfer_encoding(value) else old_transfer_encoding @@ -17,7 +19,8 @@ module Mail end def original_filename - STDERR.puts("Message#original_filename is deprecated, please call Message#filename.\n#{caller}") + ActiveSupport::Deprecation.warn('Message#original_filename is deprecated, ' << + 'please call Message#filename.', caller[0,10]) filename end -- cgit v1.2.3 From 90bbed233e14957376d6c1985202c495b3af6ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 12:51:07 +1100 Subject: Updating deprecated_body.rb to use :content instead of :data or :body in the params hash --- actionmailer/lib/action_mailer/deprecated_body.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/deprecated_body.rb b/actionmailer/lib/action_mailer/deprecated_body.rb index c82610014f..daaf145327 100644 --- a/actionmailer/lib/action_mailer/deprecated_body.rb +++ b/actionmailer/lib/action_mailer/deprecated_body.rb @@ -14,10 +14,15 @@ module ActionMailer end def attachment(params, &block) + if params[:data] + ActiveSupport::Deprecation.warn('attachment :data => "string" is deprecated. To set the body of an attachment ' << + 'please use :content instead, like attachment :content => "string"', caller[0,10]) + params[:content] = params.delete(:data) + end if params[:body] - ActiveSupport::Deprecation.warn('attachment :body => "string" is deprecated. To set the body of an attachment ' << - 'please use :data instead, like attachment :data => "string"', caller[0,10]) - params[:data] = params.delete(:body) + ActiveSupport::Deprecation.warn('attachment :data => "string" is deprecated. To set the body of an attachment ' << + 'please use :content instead, like attachment :content => "string"', caller[0,10]) + params[:content] = params.delete(:body) end end -- cgit v1.2.3 From 343ac48f45a433b2ef0dc67f9cd9d1245fb5ef2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 11:01:21 +0100 Subject: Moved deprecated_body.rb to deprecatead_api.rb --- actionmailer/lib/action_mailer/base.rb | 61 ---------- actionmailer/lib/action_mailer/deprecated_api.rb | 141 +++++++++++++++++----- actionmailer/lib/action_mailer/deprecated_body.rb | 53 -------- actionmailer/lib/action_mailer/mail_helper.rb | 5 + actionmailer/lib/action_mailer/tmail_compat.rb | 6 +- 5 files changed, 118 insertions(+), 148 deletions(-) delete mode 100644 actionmailer/lib/action_mailer/deprecated_body.rb (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 39ddafe7fe..0b3b44e326 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -263,7 +263,6 @@ module ActionMailer #:nodoc: include AbstractController::UrlFor helper ActionMailer::MailHelper - include ActionMailer::DeprecatedBody include ActionMailer::DeprecatedApi include ActionMailer::DeliveryMethods @@ -297,69 +296,11 @@ module ActionMailer #:nodoc: @@default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] cattr_accessor :default_implicit_parts_order - @@protected_instance_variables = %w(@parts @message) - cattr_reader :protected_instance_variables - - # Specify the BCC addresses for the message - adv_attr_accessor :bcc - - # Specify the CC addresses for the message. - adv_attr_accessor :cc - - # Specify the charset to use for the message. This defaults to the - # +default_charset+ specified for ActionMailer::Base. - adv_attr_accessor :charset - - # Specify the content type for the message. This defaults to text/plain - # in most cases, but can be automatically set in some situations. - adv_attr_accessor :content_type - - # Specify the from address for the message. - adv_attr_accessor :from - - # Specify the address (if different than the "from" address) to direct - # replies to this message. - adv_attr_accessor :reply_to - - # Specify additional headers to be added to the message. - adv_attr_accessor :headers - - # Specify the order in which parts should be sorted, based on content-type. - # This defaults to the value for the +default_implicit_parts_order+. - adv_attr_accessor :implicit_parts_order - - # Defaults to "1.0", but may be explicitly given if needed. - adv_attr_accessor :mime_version - - # The recipient addresses for the message, either as a string (for a single - # address) or an array (for multiple addresses). - adv_attr_accessor :recipients - - # The date on which the message was sent. If not set (the default), the - # header will be set by the delivery agent. - adv_attr_accessor :sent_on - - # Specify the subject of the message. - adv_attr_accessor :subject - - # Specify the template name to use for current message. This is the "base" - # template name, without the extension or directory, and may be used to - # have multiple mailer methods share the same template. - adv_attr_accessor :template - - # Override the mailer name, which defaults to an inflected version of the - # mailer's class name. If you want to use a template in a non-standard - # location, you can use this to specify that location. - adv_attr_accessor :mailer_name - # Expose the internal Mail message attr_reader :message # Pass calls to headers and attachment to the Mail#Message instance delegate :headers, :attachments, :to => :@message - - # Alias controller_path to mailer_name so render :partial in views work. - alias :controller_path :mailer_name class << self @@ -504,9 +445,7 @@ module ActionMailer #:nodoc: # rendered and a new Mail object created. def process(method_name, *args) initialize_defaults(method_name) - super - unless @mail_was_called # Create e-mail parts create_parts diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 19c94aee8d..32ea4162dc 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -3,8 +3,87 @@ module ActionMailer # hooks in ActionMailer::Base are removed as well. # # Moved here to allow us to add the new Mail API - module DeprecatedApi - extend ActionMailer::AdvAttrAccessor + module DeprecatedApi #:nodoc: + extend ActiveSupport::Concern + + included do + extend ActionMailer::AdvAttrAccessor + + @@protected_instance_variables = %w(@parts) + cattr_reader :protected_instance_variables + + # Specify the BCC addresses for the message + adv_attr_accessor :bcc + + # Specify the CC addresses for the message. + adv_attr_accessor :cc + + # Specify the charset to use for the message. This defaults to the + # +default_charset+ specified for ActionMailer::Base. + adv_attr_accessor :charset + + # Specify the content type for the message. This defaults to text/plain + # in most cases, but can be automatically set in some situations. + adv_attr_accessor :content_type + + # Specify the from address for the message. + adv_attr_accessor :from + + # Specify the address (if different than the "from" address) to direct + # replies to this message. + adv_attr_accessor :reply_to + + # Specify additional headers to be added to the message. + adv_attr_accessor :headers + + # Specify the order in which parts should be sorted, based on content-type. + # This defaults to the value for the +default_implicit_parts_order+. + adv_attr_accessor :implicit_parts_order + + # Defaults to "1.0", but may be explicitly given if needed. + adv_attr_accessor :mime_version + + # The recipient addresses for the message, either as a string (for a single + # address) or an array (for multiple addresses). + adv_attr_accessor :recipients + + # The date on which the message was sent. If not set (the default), the + # header will be set by the delivery agent. + adv_attr_accessor :sent_on + + # Specify the subject of the message. + adv_attr_accessor :subject + + # Specify the template name to use for current message. This is the "base" + # template name, without the extension or directory, and may be used to + # have multiple mailer methods share the same template. + adv_attr_accessor :template + + # Override the mailer name, which defaults to an inflected version of the + # mailer's class name. If you want to use a template in a non-standard + # location, you can use this to specify that location. + adv_attr_accessor :mailer_name + + # Define the body of the message. This is either a Hash (in which case it + # specifies the variables to pass to the template when it is rendered), + # or a string, in which case it specifies the actual text of the message. + adv_attr_accessor :body + + # Alias controller_path to mailer_name so render :partial in views work. + alias :controller_path :mailer_name + end + + def render(*args) + options = args.last.is_a?(Hash) ? args.last : {} + if options[:body] + ActiveSupport::Deprecation.warn(':body in render deprecated. Please call body ' << + 'with a hash instead', caller[0,1]) + + body options.delete(:body) + end + + super + end # 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, @@ -13,8 +92,6 @@ module ActionMailer params = {:content_type => params} if String === params if custom_headers = params.delete(:headers) - ActiveSupport::Deprecation.warn('Passing custom headers with :headers => {} is deprecated. ' << - 'Please just pass in custom headers directly.', caller[0,10]) params.merge!(custom_headers) end @@ -27,19 +104,38 @@ module ActionMailer # Add an attachment to a multipart message. This is simply a part with the # content-disposition set to "attachment". def attachment(params, &block) - super # Run deprecation hooks - params = { :content_type => params } if String === params + params[:content] ||= params.delete(:data) || params.delete(:body) + if params[:filename] params = normalize_file_hash(params) else params = normalize_nonfile_hash(params) end + part(params, &block) end - private + # Render a message but does not set it as mail body. Useful for rendering + # data for part and attachments. + # + # Examples: + # + # render_message "special_message" + # render_message :template => "special_message" + # render_message :inline => "<%= 'Hi!' %>" + # + def render_message(object) + case object + when String + render_to_body(:template => object) + else + render_to_body(object) + end + end + + private def normalize_nonfile_hash(params) content_disposition = "attachment;" @@ -109,25 +205,6 @@ module ActionMailer @message end - - # Render a message but does not set it as mail body. Useful for rendering - # data for part and attachments. - # - # Examples: - # - # render_message "special_message" - # render_message :template => "special_message" - # render_message :inline => "<%= 'Hi!' %>" - # - # TODO Deprecate me - def render_message(object) - case object - when String - render_to_body(:template => object) - else - render_to_body(object) - end - end # Set up the default values for the various instance variables of this # mailer. Subclasses may override this method to provide different @@ -139,18 +216,20 @@ module ActionMailer @mime_version ||= self.class.default_mime_version.dup if self.class.default_mime_version @mailer_name ||= self.class.mailer_name.dup - @delivery_method = self.class.delivery_method @template ||= method_name @parts ||= [] @headers ||= {} @sent_on ||= Time.now - - super # Run deprecation hooks + @body ||= {} end def create_parts #:nodoc: - super # Run deprecation hooks + if String === @body + self.response_body = @body + elsif @body.is_a?(Hash) && !@body.empty? + @body.each { |k, v| instance_variable_set(:"@#{k}", v) } + end if String === response_body @parts.unshift create_inline_part(response_body) @@ -179,7 +258,7 @@ module ActionMailer :body => body ) end - + def split_content_type(ct) #:nodoc: ct.to_s.split("/") end diff --git a/actionmailer/lib/action_mailer/deprecated_body.rb b/actionmailer/lib/action_mailer/deprecated_body.rb deleted file mode 100644 index daaf145327..0000000000 --- a/actionmailer/lib/action_mailer/deprecated_body.rb +++ /dev/null @@ -1,53 +0,0 @@ -module ActionMailer - # TODO Remove this module all together in a next release. Ensure that super - # hooks in ActionMailer::Base are removed as well. - module DeprecatedBody - extend ActionMailer::AdvAttrAccessor - - # Define the body of the message. This is either a Hash (in which case it - # specifies the variables to pass to the template when it is rendered), - # or a string, in which case it specifies the actual text of the message. - adv_attr_accessor :body - - def initialize_defaults(method_name) - @body ||= {} - end - - def attachment(params, &block) - if params[:data] - ActiveSupport::Deprecation.warn('attachment :data => "string" is deprecated. To set the body of an attachment ' << - 'please use :content instead, like attachment :content => "string"', caller[0,10]) - params[:content] = params.delete(:data) - end - if params[:body] - ActiveSupport::Deprecation.warn('attachment :data => "string" is deprecated. To set the body of an attachment ' << - 'please use :content instead, like attachment :content => "string"', caller[0,10]) - params[:content] = params.delete(:body) - end - end - - def create_parts - if String === @body - ActiveSupport::Deprecation.warn('body(String) is deprecated. To set the body with a text ' << - 'call render(:text => "body")', caller[0,10]) - self.response_body = @body - elsif @body.is_a?(Hash) && !@body.empty? - ActiveSupport::Deprecation.warn('body(Hash) is deprecated. Use instance variables to define ' << - 'assigns in your view', caller[0,10]) - @body.each { |k, v| instance_variable_set(:"@#{k}", v) } - end - end - - def render(*args) - options = args.last.is_a?(Hash) ? args.last : {} - if options[:body] - ActiveSupport::Deprecation.warn(':body in render deprecated. Please call body ' << - 'with a hash instead', caller[0,1]) - - body options.delete(:body) - end - - super - end - end -end diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb index 701dc34431..702c9ba8f7 100644 --- a/actionmailer/lib/action_mailer/mail_helper.rb +++ b/actionmailer/lib/action_mailer/mail_helper.rb @@ -20,5 +20,10 @@ module ActionMailer def mailer #:nodoc: @controller end + + # Access the message instance. + def message + @message + end end end diff --git a/actionmailer/lib/action_mailer/tmail_compat.rb b/actionmailer/lib/action_mailer/tmail_compat.rb index da13aac677..d78332c135 100644 --- a/actionmailer/lib/action_mailer/tmail_compat.rb +++ b/actionmailer/lib/action_mailer/tmail_compat.rb @@ -3,7 +3,7 @@ module Mail def set_content_type(*args) ActiveSupport::Deprecation.warn('Message#set_content_type is deprecated, please just call ' << - 'Message#content_type with the same arguments.', caller[0,10]) + 'Message#content_type with the same arguments', caller[0,10]) content_type(*args) end @@ -11,7 +11,7 @@ module Mail def transfer_encoding(value = nil) if value ActiveSupport::Deprecation.warn('Message#transfer_encoding is deprecated, please call ' << - 'Message#content_transfer_encoding with the same arguments.', caller[0,10]) + 'Message#content_transfer_encoding with the same arguments', caller[0,10]) content_transfer_encoding(value) else old_transfer_encoding @@ -20,7 +20,7 @@ module Mail def original_filename ActiveSupport::Deprecation.warn('Message#original_filename is deprecated, ' << - 'please call Message#filename.', caller[0,10]) + 'please call Message#filename', caller[0,10]) filename end -- cgit v1.2.3 From bb9d71ff9e537597ff4d5962e7870ad99001f605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 11:10:37 +0100 Subject: Move class methods to deprecated stuff. --- actionmailer/lib/action_mailer/base.rb | 53 +++--------------------- actionmailer/lib/action_mailer/deprecated_api.rb | 48 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 47 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 0b3b44e326..2c1de0e3b4 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -253,7 +253,6 @@ module ActionMailer #:nodoc: # +implicit_parts_order+. class Base < AbstractController::Base include Quoting - extend AdvAttrAccessor include AbstractController::Logger include AbstractController::Rendering @@ -311,23 +310,6 @@ module ActionMailer #:nodoc: alias :controller_path :mailer_name - def respond_to?(method_symbol, include_private = false) #:nodoc: - matches_dynamic_method?(method_symbol) || super - end - - def method_missing(method_symbol, *parameters) #:nodoc: - if match = matches_dynamic_method?(method_symbol) - case match[1] - when 'create' then new(match[2], *parameters).message - when 'deliver' then new(match[2], *parameters).deliver! - when 'new' then nil - else super - end - else - super - end - end - # Receives a raw email, parses it into an email object, decodes it, # instantiates a new mailer, and passes the email object to the mailer # object's +receive+ method. If you want your mailer to be able to @@ -357,7 +339,6 @@ module ActionMailer #:nodoc: raise "no mail object available for delivery!" unless mail ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| - self.set_payload_for_mail(payload, mail) mail.delivery_method delivery_methods[delivery_method], @@ -396,18 +377,11 @@ module ActionMailer #:nodoc: payload[:date] = mail.date payload[:mail] = mail.encoded end - - private - - def matches_dynamic_method?(method_name) #:nodoc: - method_name = method_name.to_s - /^(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name) - end end def mail(headers = {}) # Guard flag to prevent both the old and the new API from firing - # TODO - Move this @mail_was_called flag into deprecated_api.rb + # Should be removed when old API is deprecated @mail_was_called = true m = @message @@ -426,6 +400,11 @@ module ActionMailer #:nodoc: m.body.set_sort_order(headers[:parts_order] || @@default_implicit_parts_order) + # # Set the subject if not set yet + # @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], + # :default => method_name.humanize) + + # TODO: m.body.sort_parts! m end @@ -436,30 +415,10 @@ module ActionMailer #:nodoc: # method, for instance). def initialize(method_name=nil, *args) super() - @mail_was_called = false @message = Mail.new process(method_name, *args) if method_name end - # Process the mailer via the given +method_name+. The body will be - # rendered and a new Mail object created. - def process(method_name, *args) - initialize_defaults(method_name) - super - unless @mail_was_called - # Create e-mail parts - create_parts - - # Set the subject if not set yet - @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], - :default => method_name.humanize) - - # Build the mail object itself - create_mail - end - @message - end - # Delivers a Mail object. By default, it delivers the cached mail # object (from the create! method). If no cached mail object exists, and # no alternate has been given as the parameter, this will fail. diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 32ea4162dc..430b0cef4a 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -73,6 +73,37 @@ module ActionMailer alias :controller_path :mailer_name end + module ClassMethods + def respond_to?(method_symbol, include_private = false) #:nodoc: + matches_dynamic_method?(method_symbol) || super + end + + def method_missing(method_symbol, *parameters) #:nodoc: + if match = matches_dynamic_method?(method_symbol) + case match[1] + when 'create' then new(match[2], *parameters).message + when 'deliver' then new(match[2], *parameters).deliver! + when 'new' then nil + else super + end + else + super + end + end + + private + + def matches_dynamic_method?(method_name) #:nodoc: + method_name = method_name.to_s + /^(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name) + end + end + + def initialize(*) + super() + @mail_was_called = false + end + def render(*args) options = args.last.is_a?(Hash) ? args.last : {} if options[:body] @@ -85,6 +116,23 @@ module ActionMailer super end + def process(method_name, *args) + initialize_defaults(method_name) + super + unless @mail_was_called + # Create e-mail parts + create_parts + + # Set the subject if not set yet + @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], + :default => method_name.humanize) + + # Build the mail object itself + create_mail + end + end + + # 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. -- cgit v1.2.3 From b30eb39ff072ce95ccd5ce94ae08d116c23fd260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 11:57:54 +0100 Subject: Add more tests to new API. --- actionmailer/lib/action_mailer/base.rb | 38 +++++++++++++++--------- actionmailer/lib/action_mailer/deprecated_api.rb | 2 +- 2 files changed, 25 insertions(+), 15 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 2c1de0e3b4..d75bbe952f 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -277,14 +277,14 @@ module ActionMailer #:nodoc: @@deliveries = [] cattr_accessor :deliveries - @@default_charset = "utf-8" - cattr_accessor :default_charset + extlib_inheritable_accessor :default_charset + self.default_charset = "utf-8" - @@default_content_type = "text/plain" - cattr_accessor :default_content_type + extlib_inheritable_accessor :default_content_type + self.default_content_type = "text/plain" - @@default_mime_version = "1.0" - cattr_accessor :default_mime_version + extlib_inheritable_accessor :default_mime_version + self.default_mime_version = "1.0" # This specifies the order that the parts of a multipart email will be. Usually you put # text/plain at the top so someone without a MIME capable email reader can read the plain @@ -292,14 +292,24 @@ module ActionMailer #:nodoc: # # Any content type that is not listed here will be inserted in the order you add them to # the email after the content types you list here. - @@default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] - cattr_accessor :default_implicit_parts_order + extlib_inheritable_accessor :default_implicit_parts_order + self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] # Expose the internal Mail message attr_reader :message - # Pass calls to headers and attachment to the Mail#Message instance - delegate :headers, :attachments, :to => :@message + def headers(args=nil) + if args + ActiveSupport::Deprecation.warn "headers(Hash) is deprecated, please do headers[key] = value instead", caller + @headers = args + else + @message + end + end + + def attachments + @message.attachments + end class << self @@ -386,9 +396,9 @@ module ActionMailer #:nodoc: m = @message - m.content_type ||= headers[:content_type] || @@default_content_type - m.charset ||= headers[:charset] || @@default_charset - m.mime_version ||= headers[:mime_version] || @@default_mime_version + m.content_type ||= headers[:content_type] || self.class.default_content_type + m.charset ||= headers[:charset] || self.class.default_charset + m.mime_version ||= headers[:mime_version] || self.class.default_mime_version m.subject = quote_if_necessary(headers[:subject], m.charset) if headers[:subject] m.to = quote_address_if_necessary(headers[:to], m.charset) if headers[:to] @@ -398,7 +408,7 @@ module ActionMailer #:nodoc: m.reply_to = quote_address_if_necessary(headers[:reply_to], m.charset) if headers[:reply_to] m.date = headers[:date] if headers[:date] - m.body.set_sort_order(headers[:parts_order] || @@default_implicit_parts_order) + m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order) # # Set the subject if not set yet # @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 430b0cef4a..90e2aebf53 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -227,7 +227,7 @@ module ActionMailer m.mime_version = mime_version unless mime_version.nil? m.date = sent_on.to_time rescue sent_on if sent_on - headers.each { |k, v| m[k] = v } + @headers.each { |k, v| m[k] = v } real_content_type, ctype_attrs = parse_content_type main_type, sub_type = split_content_type(real_content_type) -- cgit v1.2.3 From dcb925369389fa98d1548b25504c8e3a07eaeea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 13:27:20 +0100 Subject: Add basic template rendering to new DSL. --- actionmailer/lib/action_mailer/base.rb | 69 +++++++++++++++++------- actionmailer/lib/action_mailer/deprecated_api.rb | 9 +--- actionmailer/lib/action_mailer/mail_helper.rb | 2 +- 3 files changed, 52 insertions(+), 28 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index d75bbe952f..1f432c2a80 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -280,6 +280,7 @@ module ActionMailer #:nodoc: extlib_inheritable_accessor :default_charset self.default_charset = "utf-8" + # TODO This should be used when calling render extlib_inheritable_accessor :default_content_type self.default_content_type = "text/plain" @@ -357,7 +358,7 @@ module ActionMailer #:nodoc: begin # TODO Move me to the instance if @@perform_deliveries - mail.deliver! + mail.deliver! self.deliveries << mail end rescue Exception => e # Net::SMTP errors or sendmail pipe errors @@ -393,32 +394,62 @@ module ActionMailer #:nodoc: # Guard flag to prevent both the old and the new API from firing # Should be removed when old API is deprecated @mail_was_called = true - m = @message - - m.content_type ||= headers[:content_type] || self.class.default_content_type - m.charset ||= headers[:charset] || self.class.default_charset - m.mime_version ||= headers[:mime_version] || self.class.default_mime_version - - m.subject = quote_if_necessary(headers[:subject], m.charset) if headers[:subject] - m.to = quote_address_if_necessary(headers[:to], m.charset) if headers[:to] - m.from = quote_address_if_necessary(headers[:from], m.charset) if headers[:from] - m.cc = quote_address_if_necessary(headers[:cc], m.charset) if headers[:cc] - m.bcc = quote_address_if_necessary(headers[:bcc], m.charset) if headers[:bcc] - m.reply_to = quote_address_if_necessary(headers[:reply_to], m.charset) if headers[:reply_to] - m.date = headers[:date] if headers[:date] - m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order) - - # # Set the subject if not set yet - # @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], - # :default => method_name.humanize) + # Get default subject from I18n if none is set + headers[:subject] ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, action_name], + :default => action_name.humanize) + + # Give preference to headers and fallbacks to the ones set in mail + headers[:content_type] ||= m.content_type + headers[:charset] ||= m.charset + headers[:mime_version] ||= m.mime_version + + m.content_type = headers[:content_type] || self.class.default_content_type.dup + m.charset = headers[:charset] || self.class.default_charset.dup + m.mime_version = headers[:mime_version] || self.class.default_mime_version.dup + + m.subject ||= quote_if_necessary(headers[:subject], m.charset) if headers[:subject] + m.to ||= quote_address_if_necessary(headers[:to], m.charset) if headers[:to] + m.from ||= quote_address_if_necessary(headers[:from], m.charset) if headers[:from] + m.cc ||= quote_address_if_necessary(headers[:cc], m.charset) if headers[:cc] + m.bcc ||= quote_address_if_necessary(headers[:bcc], m.charset) if headers[:bcc] + m.reply_to ||= quote_address_if_necessary(headers[:reply_to], m.charset) if headers[:reply_to] + m.date ||= headers[:date] if headers[:date] + + if block_given? + # Do something + else + # TODO Ensure that we don't need to pass I18n.locale as detail + templates = self.class.template_root.find_all(action_name, {}, mailer_name) + + if templates.size == 1 + unless headers[:content_type] + proper_charset = m.charset + m.content_type = templates[0].mime_type.to_s + m.charset = proper_charset + end + m.body = render_to_body(:_template => templates[0]) + else + templates.each do |template| + part = Mail::Part.new + part.content_type = template.mime_type.to_s + part.charset = m.charset + part.body = render_to_body(:_template => template) + end + end + end + m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup) # TODO: m.body.sort_parts! m end + def fill_in_part(part, template, charset) + + end + # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer # will be initialized according to the named method. If not, the mailer will # remain uninitialized (useful when you only need to invoke the "receive" diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 90e2aebf53..b2bb6a64aa 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -120,19 +120,12 @@ module ActionMailer initialize_defaults(method_name) super unless @mail_was_called - # Create e-mail parts create_parts - - # Set the subject if not set yet - @subject ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, method_name], - :default => method_name.humanize) - - # Build the mail object itself create_mail end + @message end - # 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. diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb index 702c9ba8f7..45ba6f0714 100644 --- a/actionmailer/lib/action_mailer/mail_helper.rb +++ b/actionmailer/lib/action_mailer/mail_helper.rb @@ -22,7 +22,7 @@ module ActionMailer end # Access the message instance. - def message + def message #:nodoc: @message end end -- cgit v1.2.3 From 1cd55928c6f638affeb5d89293f478817675d7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 13:56:06 +0100 Subject: First work on implicit multipart. --- actionmailer/lib/action_mailer/base.rb | 49 +++++++++++++++++----------------- 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 1f432c2a80..fb1dab7c39 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -401,21 +401,18 @@ module ActionMailer #:nodoc: :default => action_name.humanize) # Give preference to headers and fallbacks to the ones set in mail - headers[:content_type] ||= m.content_type - headers[:charset] ||= m.charset - headers[:mime_version] ||= m.mime_version - - m.content_type = headers[:content_type] || self.class.default_content_type.dup - m.charset = headers[:charset] || self.class.default_charset.dup - m.mime_version = headers[:mime_version] || self.class.default_mime_version.dup - - m.subject ||= quote_if_necessary(headers[:subject], m.charset) if headers[:subject] - m.to ||= quote_address_if_necessary(headers[:to], m.charset) if headers[:to] - m.from ||= quote_address_if_necessary(headers[:from], m.charset) if headers[:from] - m.cc ||= quote_address_if_necessary(headers[:cc], m.charset) if headers[:cc] - m.bcc ||= quote_address_if_necessary(headers[:bcc], m.charset) if headers[:bcc] - m.reply_to ||= quote_address_if_necessary(headers[:reply_to], m.charset) if headers[:reply_to] - m.date ||= headers[:date] if headers[:date] + content_type = headers[:content_type] || m.content_type + charset = headers[:charset] || m.charset + mime_version = headers[:mime_version] || m.mime_version + body = nil + + m.subject ||= quote_if_necessary(headers[:subject], charset) if headers[:subject] + m.to ||= quote_address_if_necessary(headers[:to], charset) if headers[:to] + m.from ||= quote_address_if_necessary(headers[:from], charset) if headers[:from] + m.cc ||= quote_address_if_necessary(headers[:cc], charset) if headers[:cc] + m.bcc ||= quote_address_if_necessary(headers[:bcc], charset) if headers[:bcc] + m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to] + m.date ||= headers[:date] if headers[:date] if block_given? # Do something @@ -424,25 +421,29 @@ module ActionMailer #:nodoc: templates = self.class.template_root.find_all(action_name, {}, mailer_name) if templates.size == 1 - unless headers[:content_type] - proper_charset = m.charset - m.content_type = templates[0].mime_type.to_s - m.charset = proper_charset - end + content_type ||= templates[0].mime_type.to_s m.body = render_to_body(:_template => templates[0]) else + content_type ||= "multipart/alternate" + templates.each do |template| part = Mail::Part.new part.content_type = template.mime_type.to_s - part.charset = m.charset + part.charset = charset part.body = render_to_body(:_template => template) + m.add_part(part) end end end + + m.content_type = content_type || self.class.default_content_type.dup + m.charset = charset || self.class.default_charset.dup + m.mime_version = mime_version || self.class.default_mime_version.dup + + # TODO Add me and test me + # m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup) + # m.body.sort_parts! - m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup) - - # TODO: m.body.sort_parts! m end -- cgit v1.2.3 From 951397b4a2143eee4b900356dab525aed99430ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Fri, 22 Jan 2010 14:38:41 +0100 Subject: Get implicit multipart and attachments working together. --- actionmailer/lib/action_mailer/base.rb | 50 ++++++++++++---------- actionmailer/lib/action_mailer/delivery_methods.rb | 2 + 2 files changed, 30 insertions(+), 22 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index fb1dab7c39..98e559154a 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -390,6 +390,7 @@ module ActionMailer #:nodoc: end end + # TODO Add new delivery method goodness def mail(headers = {}) # Guard flag to prevent both the old and the new API from firing # Should be removed when old API is deprecated @@ -402,9 +403,8 @@ module ActionMailer #:nodoc: # Give preference to headers and fallbacks to the ones set in mail content_type = headers[:content_type] || m.content_type - charset = headers[:charset] || m.charset - mime_version = headers[:mime_version] || m.mime_version - body = nil + charset = headers[:charset] || m.charset || self.class.default_charset.dup + mime_version = headers[:mime_version] || m.mime_version || self.class.default_mime_version.dup m.subject ||= quote_if_necessary(headers[:subject], charset) if headers[:subject] m.to ||= quote_address_if_necessary(headers[:to], charset) if headers[:to] @@ -420,35 +420,41 @@ module ActionMailer #:nodoc: # TODO Ensure that we don't need to pass I18n.locale as detail templates = self.class.template_root.find_all(action_name, {}, mailer_name) - if templates.size == 1 + if templates.size == 1 && !m.has_attachments? content_type ||= templates[0].mime_type.to_s m.body = render_to_body(:_template => templates[0]) + elsif templates.size > 1 && m.has_attachments? + container = Mail::Part.new + container.content_type = "multipart/alternate" + templates.each { |t| insert_part(container, t, charset) } + m.add_part(container) else - content_type ||= "multipart/alternate" - - templates.each do |template| - part = Mail::Part.new - part.content_type = template.mime_type.to_s - part.charset = charset - part.body = render_to_body(:_template => template) - m.add_part(part) - end + templates.each { |t| insert_part(m, t, charset) } end + + content_type ||= (m.has_attachments? ? "multipart/mixed" : "multipart/alternate") end - + + # Check if the content_type was not overwriten along the way and if so, + # fallback to default. m.content_type = content_type || self.class.default_content_type.dup - m.charset = charset || self.class.default_charset.dup - m.mime_version = mime_version || self.class.default_mime_version.dup - - # TODO Add me and test me - # m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup) - # m.body.sort_parts! + m.charset = charset + m.mime_version = mime_version + + unless m.parts.empty? + m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup) + m.body.sort_parts! + end m end - def fill_in_part(part, template, charset) - + def insert_part(container, template, charset) + part = Mail::Part.new + part.content_type = template.mime_type.to_s + part.charset = charset + part.body = render_to_body(:_template => template) + container.add_part(part) end # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb index c8c4148353..5883e446f2 100644 --- a/actionmailer/lib/action_mailer/delivery_methods.rb +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -27,6 +27,7 @@ module ActionMailer end module ClassMethods + # TODO Make me class inheritable def delivery_settings @@delivery_settings ||= Hash.new { |h,k| h[k] = {} } end @@ -51,6 +52,7 @@ module ActionMailer protected + # TODO Get rid of this method missing magic def method_missing(method_symbol, *parameters) #:nodoc: if match = matches_settings_method?(method_symbol) if match[2] -- cgit v1.2.3 From 5c3ef8c17dba23d000d301467b1a620811c940b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sat, 23 Jan 2010 10:24:19 +0100 Subject: Refactor subject with i18n. --- actionmailer/lib/action_mailer/base.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 98e559154a..94e20d4b63 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -398,8 +398,7 @@ module ActionMailer #:nodoc: m = @message # Get default subject from I18n if none is set - headers[:subject] ||= I18n.t(:subject, :scope => [:actionmailer, mailer_name, action_name], - :default => action_name.humanize) + headers[:subject] ||= default_subject # Give preference to headers and fallbacks to the ones set in mail content_type = headers[:content_type] || m.content_type @@ -418,7 +417,7 @@ module ActionMailer #:nodoc: # Do something else # TODO Ensure that we don't need to pass I18n.locale as detail - templates = self.class.template_root.find_all(action_name, {}, mailer_name) + templates = self.class.template_root.find_all(action_name, {}, self.class.mailer_name) if templates.size == 1 && !m.has_attachments? content_type ||= templates[0].mime_type.to_s @@ -449,6 +448,11 @@ module ActionMailer #:nodoc: m end + def default_subject + mailer_scope = self.class.mailer_name.gsub('/', '.') + I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize) + end + def insert_part(container, template, charset) part = Mail::Part.new part.content_type = template.mime_type.to_s -- cgit v1.2.3 From c6b16260fe3d1435848e78415bd0b40c10ad7424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sat, 23 Jan 2010 21:37:34 +1100 Subject: Added basic explicit multipart rendering and tests --- actionmailer/lib/action_mailer/base.rb | 28 ++++++++++++++++++++------- actionmailer/lib/action_mailer/mail_helper.rb | 2 +- 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 94e20d4b63..28e4c88b5a 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -297,8 +297,9 @@ module ActionMailer #:nodoc: self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] # Expose the internal Mail message + # TODO: Make this an _internal ivar? attr_reader :message - + def headers(args=nil) if args ActiveSupport::Deprecation.warn "headers(Hash) is deprecated, please do headers[key] = value instead", caller @@ -413,12 +414,25 @@ module ActionMailer #:nodoc: m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to] m.date ||= headers[:date] if headers[:date] - if block_given? - # Do something + if headers[:body] + templates = [ActionView::Template::Text.new(headers[:body], format_for_text)] + elsif block_given? + collector = ActionMailer::Collector.new(self, {:charset => charset}) do + render action_name + end + yield collector + + collector.responses.each do |response| + part = Mail::Part.new(response) + m.add_part(part) + end + else # TODO Ensure that we don't need to pass I18n.locale as detail templates = self.class.template_root.find_all(action_name, {}, self.class.mailer_name) - + end + + if templates if templates.size == 1 && !m.has_attachments? content_type ||= templates[0].mime_type.to_s m.body = render_to_body(:_template => templates[0]) @@ -430,17 +444,17 @@ module ActionMailer #:nodoc: else templates.each { |t| insert_part(m, t, charset) } end - - content_type ||= (m.has_attachments? ? "multipart/mixed" : "multipart/alternate") end + content_type ||= (m.has_attachments? ? "multipart/mixed" : "multipart/alternate") + # Check if the content_type was not overwriten along the way and if so, # fallback to default. m.content_type = content_type || self.class.default_content_type.dup m.charset = charset m.mime_version = mime_version - unless m.parts.empty? + if m.parts.present? && templates m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup) m.body.sort_parts! end diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb index 45ba6f0714..adba94cbef 100644 --- a/actionmailer/lib/action_mailer/mail_helper.rb +++ b/actionmailer/lib/action_mailer/mail_helper.rb @@ -18,7 +18,7 @@ module ActionMailer # Access the mailer instance. def mailer #:nodoc: - @controller + @_controller end # Access the message instance. -- cgit v1.2.3 From 5a19d24892b9d842ef7d27875eacecbbad71a9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sat, 23 Jan 2010 21:50:36 +1100 Subject: Adding collector to ActionMailer --- actionmailer/lib/action_mailer/collector.rb | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 actionmailer/lib/action_mailer/collector.rb (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/collector.rb b/actionmailer/lib/action_mailer/collector.rb new file mode 100644 index 0000000000..49c3f04bad --- /dev/null +++ b/actionmailer/lib/action_mailer/collector.rb @@ -0,0 +1,32 @@ +require 'abstract_controller/collector' + +module ActionMailer #:nodoc: + + class Collector + + include AbstractController::Collector + + attr_accessor :responses + + def initialize(context, options, &block) + @default_options = options + @default_render = block + @default_formats = context.formats + @context = context + @responses = [] + end + + def custom(mime, options={}, &block) + options = @default_options.merge(:content_type => mime.to_s).merge(options) + @context.formats = [mime.to_sym] + options[:body] = if block + block.call + else + @default_render.call + end + @responses << options + @context.formats = @default_formats + end + + end +end \ No newline at end of file -- cgit v1.2.3 From 6ba944608e527b8d7fc564a6e450e2d4c4355ddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sat, 23 Jan 2010 12:20:20 +0100 Subject: Make implicit and explicit templates pass through the same part creation process. --- actionmailer/lib/action_mailer/base.rb | 126 ++++++++++++++-------------- actionmailer/lib/action_mailer/collector.rb | 25 +++--- 2 files changed, 79 insertions(+), 72 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 28e4c88b5a..5e61b8693a 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -1,5 +1,5 @@ require 'active_support/core_ext/class' -require "active_support/core_ext/module/delegation" +require 'active_support/core_ext/module/delegation' require 'mail' require 'action_mailer/tmail_compat' @@ -391,70 +391,63 @@ module ActionMailer #:nodoc: end end + # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer + # will be initialized according to the named method. If not, the mailer will + # remain uninitialized (useful when you only need to invoke the "receive" + # method, for instance). + def initialize(method_name=nil, *args) + super() + @message = Mail.new + process(method_name, *args) if method_name + end + + # Delivers a Mail object. By default, it delivers the cached mail + # object (from the create! method). If no cached mail object exists, and + # no alternate has been given as the parameter, this will fail. + def deliver!(mail = @message) + self.class.deliver(mail) + end + # TODO Add new delivery method goodness def mail(headers = {}) # Guard flag to prevent both the old and the new API from firing # Should be removed when old API is deprecated @mail_was_called = true - m = @message - # Get default subject from I18n if none is set - headers[:subject] ||= default_subject + m, sort_parts = @message, true - # Give preference to headers and fallbacks to the ones set in mail + # Give preference to headers and fallback to the ones set in mail content_type = headers[:content_type] || m.content_type charset = headers[:charset] || m.charset || self.class.default_charset.dup mime_version = headers[:mime_version] || m.mime_version || self.class.default_mime_version.dup - m.subject ||= quote_if_necessary(headers[:subject], charset) if headers[:subject] - m.to ||= quote_address_if_necessary(headers[:to], charset) if headers[:to] - m.from ||= quote_address_if_necessary(headers[:from], charset) if headers[:from] - m.cc ||= quote_address_if_necessary(headers[:cc], charset) if headers[:cc] - m.bcc ||= quote_address_if_necessary(headers[:bcc], charset) if headers[:bcc] - m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to] - m.date ||= headers[:date] if headers[:date] + headers[:subject] ||= default_subject + quote_fields(m, headers, charset) - if headers[:body] - templates = [ActionView::Template::Text.new(headers[:body], format_for_text)] + responses = if headers[:body] + [ { :body => headers[:body], :content_type => self.class.default_content_type.dup } ] elsif block_given? - collector = ActionMailer::Collector.new(self, {:charset => charset}) do - render action_name - end - yield collector - - collector.responses.each do |response| - part = Mail::Part.new(response) - m.add_part(part) - end - + sort_parts = false + collector = ActionMailer::Collector.new(self) { render(action_name) } + yield(collector) + collector.responses else # TODO Ensure that we don't need to pass I18n.locale as detail templates = self.class.template_root.find_all(action_name, {}, self.class.mailer_name) - end - - if templates - if templates.size == 1 && !m.has_attachments? - content_type ||= templates[0].mime_type.to_s - m.body = render_to_body(:_template => templates[0]) - elsif templates.size > 1 && m.has_attachments? - container = Mail::Part.new - container.content_type = "multipart/alternate" - templates.each { |t| insert_part(container, t, charset) } - m.add_part(container) - else - templates.each { |t| insert_part(m, t, charset) } + + templates.map do |template| + { :body => render_to_body(:_template => template), + :content_type => template.mime_type.to_s } end end - content_type ||= (m.has_attachments? ? "multipart/mixed" : "multipart/alternate") + content_type ||= create_parts_from_responses(m, responses, charset) - # Check if the content_type was not overwriten along the way and if so, - # fallback to default. - m.content_type = content_type || self.class.default_content_type.dup + m.content_type = content_type m.charset = charset m.mime_version = mime_version - if m.parts.present? && templates + if sort_parts && m.parts.present? m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup) m.body.sort_parts! end @@ -462,34 +455,43 @@ module ActionMailer #:nodoc: m end - def default_subject + protected + + def default_subject #:nodoc: mailer_scope = self.class.mailer_name.gsub('/', '.') I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize) end - def insert_part(container, template, charset) - part = Mail::Part.new - part.content_type = template.mime_type.to_s - part.charset = charset - part.body = render_to_body(:_template => template) - container.add_part(part) + def quote_fields(m, headers, charset) #:nodoc: + m.subject ||= quote_if_necessary(headers[:subject], charset) if headers[:subject] + m.to ||= quote_address_if_necessary(headers[:to], charset) if headers[:to] + m.from ||= quote_address_if_necessary(headers[:from], charset) if headers[:from] + m.cc ||= quote_address_if_necessary(headers[:cc], charset) if headers[:cc] + m.bcc ||= quote_address_if_necessary(headers[:bcc], charset) if headers[:bcc] + m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to] + m.date ||= headers[:date] if headers[:date] end - # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer - # will be initialized according to the named method. If not, the mailer will - # remain uninitialized (useful when you only need to invoke the "receive" - # method, for instance). - def initialize(method_name=nil, *args) - super() - @message = Mail.new - process(method_name, *args) if method_name + def create_parts_from_responses(m, responses, charset) #:nodoc: + if responses.size == 1 && !m.has_attachments? + m.body = responses[0][:body] + return responses[0][:content_type] + elsif responses.size > 1 && m.has_attachments? + container = Mail::Part.new + container.content_type = "multipart/alternate" + responses.each { |r| insert_part(container, r, charset) } + m.add_part(container) + else + responses.each { |r| insert_part(m, r, charset) } + end + + m.has_attachments? ? "multipart/mixed" : "multipart/alternate" end - # Delivers a Mail object. By default, it delivers the cached mail - # object (from the create! method). If no cached mail object exists, and - # no alternate has been given as the parameter, this will fail. - def deliver!(mail = @message) - self.class.deliver(mail) + def insert_part(container, response, charset) #:nodoc: + response[:charset] ||= charset + part = Mail::Part.new(response) + container.add_part(part) end end diff --git a/actionmailer/lib/action_mailer/collector.rb b/actionmailer/lib/action_mailer/collector.rb index 49c3f04bad..0891b6f123 100644 --- a/actionmailer/lib/action_mailer/collector.rb +++ b/actionmailer/lib/action_mailer/collector.rb @@ -1,23 +1,29 @@ require 'abstract_controller/collector' +require 'active_support/core_ext/hash/reverse_merge' +require 'active_support/core_ext/array/extract_options' module ActionMailer #:nodoc: - class Collector - include AbstractController::Collector - - attr_accessor :responses + attr_reader :responses - def initialize(context, options, &block) - @default_options = options - @default_render = block - @default_formats = context.formats + def initialize(context, &block) @context = context @responses = [] + @default_render = block + @default_formats = context.formats + end + + # TODO Test me + def any(*args, &block) + options = args.extract_options! + raise "You have to supply at least one format" if args.empty? + args.each { |type| send(type, options, &block) } end + alias :all :any def custom(mime, options={}, &block) - options = @default_options.merge(:content_type => mime.to_s).merge(options) + options.reverse_merge!(:content_type => mime.to_s) @context.formats = [mime.to_sym] options[:body] = if block block.call @@ -27,6 +33,5 @@ module ActionMailer #:nodoc: @responses << options @context.formats = @default_formats end - end end \ No newline at end of file -- cgit v1.2.3 From c985a0ee3d9ae279fd02814ca60782e2f93215e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sat, 23 Jan 2010 12:46:33 +0100 Subject: Add some tests to collector with templates and any. --- actionmailer/lib/action_mailer/base.rb | 2 +- actionmailer/lib/action_mailer/collector.rb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 5e61b8693a..4ab3761807 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -2,6 +2,7 @@ require 'active_support/core_ext/class' require 'active_support/core_ext/module/delegation' require 'mail' require 'action_mailer/tmail_compat' +require 'action_mailer/collector' module ActionMailer #:nodoc: # Action Mailer allows you to send email from your application using a mailer model and views. @@ -442,7 +443,6 @@ module ActionMailer #:nodoc: end content_type ||= create_parts_from_responses(m, responses, charset) - m.content_type = content_type m.charset = charset m.mime_version = mime_version diff --git a/actionmailer/lib/action_mailer/collector.rb b/actionmailer/lib/action_mailer/collector.rb index 0891b6f123..5431efccfe 100644 --- a/actionmailer/lib/action_mailer/collector.rb +++ b/actionmailer/lib/action_mailer/collector.rb @@ -14,11 +14,10 @@ module ActionMailer #:nodoc: @default_formats = context.formats end - # TODO Test me def any(*args, &block) options = args.extract_options! raise "You have to supply at least one format" if args.empty? - args.each { |type| send(type, options, &block) } + args.each { |type| send(type, options.dup, &block) } end alias :all :any -- cgit v1.2.3 From e7e4ed48df3048f32d94e398e99d0048a66ba67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 09:34:50 +1100 Subject: Set sort order for explicit parts from the collector's template sequence --- actionmailer/lib/action_mailer/base.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 4ab3761807..e95643ca44 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -415,7 +415,7 @@ module ActionMailer #:nodoc: # Should be removed when old API is deprecated @mail_was_called = true - m, sort_parts = @message, true + m = @message # Give preference to headers and fallback to the ones set in mail content_type = headers[:content_type] || m.content_type @@ -425,12 +425,16 @@ module ActionMailer #:nodoc: headers[:subject] ||= default_subject quote_fields(m, headers, charset) + sort_order = headers[:parts_order] || self.class.default_implicit_parts_order.dup + responses = if headers[:body] [ { :body => headers[:body], :content_type => self.class.default_content_type.dup } ] elsif block_given? - sort_parts = false collector = ActionMailer::Collector.new(self) { render(action_name) } yield(collector) + # Collect the sort order of the parts from the collector as Mail will always + # sort parts on encode into a "sane" sequence. + sort_order = collector.responses.map { |r| r[:content_type] } collector.responses else # TODO Ensure that we don't need to pass I18n.locale as detail @@ -447,8 +451,8 @@ module ActionMailer #:nodoc: m.charset = charset m.mime_version = mime_version - if sort_parts && m.parts.present? - m.body.set_sort_order(headers[:parts_order] || self.class.default_implicit_parts_order.dup) + if m.multipart? + m.body.set_sort_order(sort_order) m.body.sort_parts! end -- cgit v1.2.3 From 258ca148004eaa63740ab2186338b3ac872b8187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 11:15:42 +1100 Subject: Delegated ActionMailer::Base.deliveries to Mail.deliveries, added callback support in Mail to call ActionMailer on delivery, moved deliver to deprecated API in preparation for new API --- actionmailer/lib/action_mailer/base.rb | 56 ++++++++---------------- actionmailer/lib/action_mailer/deprecated_api.rb | 32 ++++++++++++++ actionmailer/lib/action_mailer/test_case.rb | 2 +- 3 files changed, 51 insertions(+), 39 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index e95643ca44..c1dc415728 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -275,8 +275,16 @@ module ActionMailer #:nodoc: @@perform_deliveries = true cattr_accessor :perform_deliveries - @@deliveries = [] - cattr_accessor :deliveries + # Provides a list of emails that have been delivered by Mail + def self.deliveries + Mail.deliveries + end + + # Allows you to over write the default deliveries store from an array to some + # other object. If you just want to clear the store, call Mail.deliveries.clear. + def self.deliveries=(val) + Mail.deliveries = val + end extlib_inheritable_accessor :default_charset self.default_charset = "utf-8" @@ -342,35 +350,6 @@ module ActionMailer #:nodoc: end end - # Deliver the given mail object directly. This can be used to deliver - # a preconstructed mail object, like: - # - # email = MyMailer.create_some_mail(parameters) - # email.set_some_obscure_header "frobnicate" - # MyMailer.deliver(email) - def deliver(mail) - raise "no mail object available for delivery!" unless mail - - ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| - self.set_payload_for_mail(payload, mail) - - mail.delivery_method delivery_methods[delivery_method], - delivery_settings[delivery_method] - - begin - # TODO Move me to the instance - if @@perform_deliveries - mail.deliver! - self.deliveries << mail - end - rescue Exception => e # Net::SMTP errors or sendmail pipe errors - raise e if raise_delivery_errors - end - end - - mail - end - def template_root self.view_paths && self.view_paths.first end @@ -380,6 +359,12 @@ module ActionMailer #:nodoc: self.view_paths = ActionView::Base.process_view_paths(root) end + def delivered_email(mail) + ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| + self.set_payload_for_mail(payload, mail) + end + end + def set_payload_for_mail(payload, mail) #:nodoc: payload[:message_id] = mail.message_id payload[:subject] = mail.subject @@ -402,13 +387,6 @@ module ActionMailer #:nodoc: process(method_name, *args) if method_name end - # Delivers a Mail object. By default, it delivers the cached mail - # object (from the create! method). If no cached mail object exists, and - # no alternate has been given as the parameter, this will fail. - def deliver!(mail = @message) - self.class.deliver(mail) - end - # TODO Add new delivery method goodness def mail(headers = {}) # Guard flag to prevent both the old and the new API from firing @@ -417,6 +395,8 @@ module ActionMailer #:nodoc: m = @message + m.register_for_delivery_notification(self.class) + # Give preference to headers and fallback to the ones set in mail content_type = headers[:content_type] || m.content_type charset = headers[:charset] || m.charset || self.class.default_charset.dup diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index b2bb6a64aa..a9ebd70f86 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -71,9 +71,34 @@ module ActionMailer # Alias controller_path to mailer_name so render :partial in views work. alias :controller_path :mailer_name + end module ClassMethods + + # Deliver the given mail object directly. This can be used to deliver + # a preconstructed mail object, like: + # + # email = MyMailer.create_some_mail(parameters) + # email.set_some_obscure_header "frobnicate" + # MyMailer.deliver(email) + def deliver(mail) + raise "no mail object available for delivery!" unless mail + + ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| + self.set_payload_for_mail(payload, mail) + + mail.delivery_method delivery_methods[delivery_method], + delivery_settings[delivery_method] + + mail.raise_delivery_errors = raise_delivery_errors + mail.perform_deliveries = perform_deliveries + mail.deliver + end + + mail + end + def respond_to?(method_symbol, include_private = false) #:nodoc: matches_dynamic_method?(method_symbol) || super end @@ -104,6 +129,13 @@ module ActionMailer @mail_was_called = false end + # Delivers a Mail object. By default, it delivers the cached mail + # object (from the create! method). If no cached mail object exists, and + # no alternate has been given as the parameter, this will fail. + def deliver!(mail = @message) + self.class.deliver(mail) + end + def render(*args) options = args.last.is_a?(Hash) ? args.last : {} if options[:body] diff --git a/actionmailer/lib/action_mailer/test_case.rb b/actionmailer/lib/action_mailer/test_case.rb index 0ca4f5494e..7c4033a125 100644 --- a/actionmailer/lib/action_mailer/test_case.rb +++ b/actionmailer/lib/action_mailer/test_case.rb @@ -37,7 +37,7 @@ module ActionMailer def initialize_test_deliveries ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] + ActionMailer::Base.deliveries.clear end def set_expected_mail -- cgit v1.2.3 From afc758297c553de467b0c434330cb703d62795f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 12:30:13 +1100 Subject: Moving AS::Notifications call to one location in base --- actionmailer/lib/action_mailer/deprecated_api.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index a9ebd70f86..a2fa481d0e 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -83,19 +83,17 @@ module ActionMailer # email.set_some_obscure_header "frobnicate" # MyMailer.deliver(email) def deliver(mail) + return if @mail_was_called raise "no mail object available for delivery!" unless mail - ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| - self.set_payload_for_mail(payload, mail) + mail.register_for_delivery_notification(self) - mail.delivery_method delivery_methods[delivery_method], - delivery_settings[delivery_method] - - mail.raise_delivery_errors = raise_delivery_errors - mail.perform_deliveries = perform_deliveries - mail.deliver - end + mail.delivery_method delivery_methods[delivery_method], + delivery_settings[delivery_method] + mail.raise_delivery_errors = raise_delivery_errors + mail.perform_deliveries = perform_deliveries + mail.deliver mail end -- cgit v1.2.3 From 73a9000402b5766e660dbf3489f5289c21c3f472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 20:38:53 +1100 Subject: Adding failing tests for calling just the action, instead of :create_action_name and :deliver_action_name --- actionmailer/lib/action_mailer/base.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index c1dc415728..e7eb6bffcd 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -387,7 +387,8 @@ module ActionMailer #:nodoc: process(method_name, *args) if method_name end - # TODO Add new delivery method goodness + # TODO: Clean this up and refactor before Rails 3.0 release. + # This works for now, but not neat def mail(headers = {}) # Guard flag to prevent both the old and the new API from firing # Should be removed when old API is deprecated @@ -446,6 +447,7 @@ module ActionMailer #:nodoc: I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize) end + # TODO: Move this into Mail def quote_fields(m, headers, charset) #:nodoc: m.subject ||= quote_if_necessary(headers[:subject], charset) if headers[:subject] m.to ||= quote_address_if_necessary(headers[:to], charset) if headers[:to] -- cgit v1.2.3 From 7409b734841c8bd691006634dd072212aa905cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 16:37:28 +0100 Subject: Some refactoring. --- actionmailer/lib/action_mailer/base.rb | 164 ++++++++++++--------- actionmailer/lib/action_mailer/delivery_methods.rb | 92 ++++++------ actionmailer/lib/action_mailer/deprecated_api.rb | 66 ++++----- actionmailer/lib/action_mailer/tmail_compat.rb | 6 +- 4 files changed, 164 insertions(+), 164 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index e7eb6bffcd..b881611cfb 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -263,33 +263,42 @@ module ActionMailer #:nodoc: include AbstractController::UrlFor helper ActionMailer::MailHelper + include ActionMailer::DeprecatedApi + extend ActionMailer::DeliveryMethods + + add_delivery_method :smtp, Mail::SMTP, + :address => "localhost", + :port => 25, + :domain => 'localhost.localdomain', + :user_name => nil, + :password => nil, + :authentication => nil, + :enable_starttls_auto => true + + add_delivery_method :file, Mail::FileDelivery, + :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" + + add_delivery_method :sendmail, Mail::Sendmail, + :location => '/usr/sbin/sendmail', + :arguments => '-i -t' - include ActionMailer::DeliveryMethods + add_delivery_method :test, Mail::TestMailer + + superclass_delegating_reader :delivery_method + self.delivery_method = :smtp private_class_method :new #:nodoc: - @@raise_delivery_errors = true cattr_accessor :raise_delivery_errors + @@raise_delivery_errors = true - @@perform_deliveries = true cattr_accessor :perform_deliveries - - # Provides a list of emails that have been delivered by Mail - def self.deliveries - Mail.deliveries - end - - # Allows you to over write the default deliveries store from an array to some - # other object. If you just want to clear the store, call Mail.deliveries.clear. - def self.deliveries=(val) - Mail.deliveries = val - end + @@perform_deliveries = true extlib_inheritable_accessor :default_charset self.default_charset = "utf-8" - # TODO This should be used when calling render extlib_inheritable_accessor :default_content_type self.default_content_type = "text/plain" @@ -305,24 +314,9 @@ module ActionMailer #:nodoc: extlib_inheritable_accessor :default_implicit_parts_order self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] - # Expose the internal Mail message - # TODO: Make this an _internal ivar? - attr_reader :message - - def headers(args=nil) - if args - ActiveSupport::Deprecation.warn "headers(Hash) is deprecated, please do headers[key] = value instead", caller - @headers = args - else - @message - end - end - - def attachments - @message.attachments - end - class << self + # Provides a list of emails that have been delivered by Mail + delegate :deliveries, :deliveries=, :to => Mail def mailer_name @mailer_name ||= name.underscore @@ -359,6 +353,7 @@ module ActionMailer #:nodoc: self.view_paths = ActionView::Base.process_view_paths(root) end + # TODO The delivery should happen inside the instrument block def delivered_email(mail) ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| self.set_payload_for_mail(payload, mail) @@ -377,66 +372,63 @@ module ActionMailer #:nodoc: end end + attr_internal :message + # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer # will be initialized according to the named method. If not, the mailer will # remain uninitialized (useful when you only need to invoke the "receive" # method, for instance). def initialize(method_name=nil, *args) super() - @message = Mail.new + @_message = Mail.new process(method_name, *args) if method_name end - # TODO: Clean this up and refactor before Rails 3.0 release. - # This works for now, but not neat - def mail(headers = {}) - # Guard flag to prevent both the old and the new API from firing - # Should be removed when old API is deprecated - @mail_was_called = true + def headers(args=nil) + if args + ActiveSupport::Deprecation.warn "headers(Hash) is deprecated, please do headers[key] = value instead", caller[0,2] + @headers = args + else + @_message + end + end - m = @message + def attachments + @_message.attachments + end - m.register_for_delivery_notification(self.class) + def mail(headers={}, &block) + # Guard flag to prevent both the old and the new API from firing + # Should be removed when old API is removed + @mail_was_called = true + m = @_message # Give preference to headers and fallback to the ones set in mail content_type = headers[:content_type] || m.content_type charset = headers[:charset] || m.charset || self.class.default_charset.dup mime_version = headers[:mime_version] || m.mime_version || self.class.default_mime_version.dup + # Set subjects and fields quotings headers[:subject] ||= default_subject - quote_fields(m, headers, charset) - - sort_order = headers[:parts_order] || self.class.default_implicit_parts_order.dup - - responses = if headers[:body] - [ { :body => headers[:body], :content_type => self.class.default_content_type.dup } ] - elsif block_given? - collector = ActionMailer::Collector.new(self) { render(action_name) } - yield(collector) - # Collect the sort order of the parts from the collector as Mail will always - # sort parts on encode into a "sane" sequence. - sort_order = collector.responses.map { |r| r[:content_type] } - collector.responses - else - # TODO Ensure that we don't need to pass I18n.locale as detail - templates = self.class.template_root.find_all(action_name, {}, self.class.mailer_name) - - templates.map do |template| - { :body => render_to_body(:_template => template), - :content_type => template.mime_type.to_s } - end - end + quote_fields!(headers, charset) + # Render the templates and blocks + responses, sort_order = collect_responses_and_sort_order(headers, &block) content_type ||= create_parts_from_responses(m, responses, charset) + + # Tidy up content type, charset, mime version and sort order m.content_type = content_type m.charset = charset m.mime_version = mime_version + sort_order = headers[:parts_order] || sort_order || self.class.default_implicit_parts_order.dup if m.multipart? m.body.set_sort_order(sort_order) m.body.sort_parts! end + # Finaly set delivery behavior configured in class + wrap_delivery_behavior!(headers[:delivery_method]) m end @@ -448,14 +440,44 @@ module ActionMailer #:nodoc: end # TODO: Move this into Mail - def quote_fields(m, headers, charset) #:nodoc: - m.subject ||= quote_if_necessary(headers[:subject], charset) if headers[:subject] - m.to ||= quote_address_if_necessary(headers[:to], charset) if headers[:to] - m.from ||= quote_address_if_necessary(headers[:from], charset) if headers[:from] - m.cc ||= quote_address_if_necessary(headers[:cc], charset) if headers[:cc] - m.bcc ||= quote_address_if_necessary(headers[:bcc], charset) if headers[:bcc] - m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to] - m.date ||= headers[:date] if headers[:date] + def quote_fields!(headers, charset) #:nodoc: + m = @_message + m.subject ||= quote_if_necessary(headers[:subject], charset) if headers[:subject] + m.to ||= quote_address_if_necessary(headers[:to], charset) if headers[:to] + m.from ||= quote_address_if_necessary(headers[:from], charset) if headers[:from] + m.cc ||= quote_address_if_necessary(headers[:cc], charset) if headers[:cc] + m.bcc ||= quote_address_if_necessary(headers[:bcc], charset) if headers[:bcc] + m.reply_to ||= quote_address_if_necessary(headers[:reply_to], charset) if headers[:reply_to] + m.date ||= headers[:date] if headers[:date] + end + + def collect_responses_and_sort_order(headers) #:nodoc: + responses, sort_order = [], nil + + if block_given? + collector = ActionMailer::Collector.new(self) { render(action_name) } + yield(collector) + sort_order = collector.responses.map { |r| r[:content_type] } + responses = collector.responses + elsif headers[:body] + responses << { + :body => headers[:body], + :content_type => self.class.default_content_type.dup + } + else + self.class.template_root.find_all(action_name, {}, self.class.mailer_name).each do |template| + responses << { + :body => render_to_body(:_template => template), + :content_type => template.mime_type.to_s + } + end + end + + [responses, sort_order] + end + + def wrap_delivery_behavior!(method=nil) #:nodoc: + self.class.wrap_delivery_behavior(@_message, method) end def create_parts_from_responses(m, responses, charset) #:nodoc: diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb index 5883e446f2..16b84d4118 100644 --- a/actionmailer/lib/action_mailer/delivery_methods.rb +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -1,73 +1,63 @@ module ActionMailer # This modules makes a DSL for adding delivery methods to ActionMailer module DeliveryMethods - extend ActiveSupport::Concern - - included do - add_delivery_method :smtp, Mail::SMTP, - :address => "localhost", - :port => 25, - :domain => 'localhost.localdomain', - :user_name => nil, - :password => nil, - :authentication => nil, - :enable_starttls_auto => true - - add_delivery_method :file, Mail::FileDelivery, - :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" + # TODO Make me class inheritable + def delivery_settings + @@delivery_settings ||= Hash.new { |h,k| h[k] = {} } + end - add_delivery_method :sendmail, Mail::Sendmail, - :location => '/usr/sbin/sendmail', - :arguments => '-i -t' + def delivery_methods + @@delivery_methods ||= {} + end - add_delivery_method :test, Mail::TestMailer + def delivery_method=(method) + raise ArgumentError, "Unknown delivery method #{method.inspect}" unless delivery_methods[method] + @delivery_method = method + end - superclass_delegating_reader :delivery_method - self.delivery_method = :smtp + def add_delivery_method(symbol, klass, default_options={}) + self.delivery_methods[symbol] = klass + self.delivery_settings[symbol] = default_options end - module ClassMethods - # TODO Make me class inheritable - def delivery_settings - @@delivery_settings ||= Hash.new { |h,k| h[k] = {} } - end + def wrap_delivery_behavior(mail, method=nil) + method ||= delivery_method - def delivery_methods - @@delivery_methods ||= {} - end + mail.register_for_delivery_notification(self) - def delivery_method=(method) - raise ArgumentError, "Unknown delivery method #{method.inspect}" unless delivery_methods[method] - @delivery_method = method + if method.is_a?(Symbol) + mail.delivery_method(delivery_methods[method], + delivery_settings[method]) + else + mail.delivery_method(method) end - def add_delivery_method(symbol, klass, default_options={}) - self.delivery_methods[symbol] = klass - self.delivery_settings[symbol] = default_options - end + mail.perform_deliveries = perform_deliveries + mail.raise_delivery_errors = raise_delivery_errors + end - def respond_to?(method_symbol, include_private = false) #:nodoc: - matches_settings_method?(method_symbol) || super - end - protected + def respond_to?(method_symbol, include_private = false) #:nodoc: + matches_settings_method?(method_symbol) || super + end - # TODO Get rid of this method missing magic - def method_missing(method_symbol, *parameters) #:nodoc: - if match = matches_settings_method?(method_symbol) - if match[2] - delivery_settings[match[1].to_sym] = parameters[0] - else - delivery_settings[match[1].to_sym] - end + protected + + # TODO Get rid of this method missing magic + def method_missing(method_symbol, *parameters) #:nodoc: + if match = matches_settings_method?(method_symbol) + if match[2] + delivery_settings[match[1].to_sym] = parameters[0] else - super + delivery_settings[match[1].to_sym] end + else + super end + end - def matches_settings_method?(method_name) #:nodoc: - /(#{delivery_methods.keys.join('|')})_settings(=)?$/.match(method_name.to_s) - end + def matches_settings_method?(method_name) #:nodoc: + /(#{delivery_methods.keys.join('|')})_settings(=)?$/.match(method_name.to_s) end end end \ No newline at end of file diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index a2fa481d0e..f969584a17 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -71,7 +71,6 @@ module ActionMailer # Alias controller_path to mailer_name so render :partial in views work. alias :controller_path :mailer_name - end module ClassMethods @@ -82,17 +81,14 @@ module ActionMailer # email = MyMailer.create_some_mail(parameters) # email.set_some_obscure_header "frobnicate" # MyMailer.deliver(email) - def deliver(mail) - return if @mail_was_called - raise "no mail object available for delivery!" unless mail - - mail.register_for_delivery_notification(self) - - mail.delivery_method delivery_methods[delivery_method], - delivery_settings[delivery_method] + def deliver(mail, show_warning=true) + if show_warning + ActiveSupport::Deprecation.warn "ActionMailer::Base.deliver is deprecated, just call " << + "deliver in the instance instead", caller + end - mail.raise_delivery_errors = raise_delivery_errors - mail.perform_deliveries = perform_deliveries + raise "no mail object available for delivery!" unless mail + wrap_delivery_behavior(mail) mail.deliver mail end @@ -122,23 +118,19 @@ module ActionMailer end end - def initialize(*) - super() - @mail_was_called = false - end - # Delivers a Mail object. By default, it delivers the cached mail # object (from the create! method). If no cached mail object exists, and # no alternate has been given as the parameter, this will fail. - def deliver!(mail = @message) - self.class.deliver(mail) + def deliver!(mail = @_message) + self.class.deliver(mail, false) end + alias :deliver :deliver! def render(*args) options = args.last.is_a?(Hash) ? args.last : {} if options[:body] - ActiveSupport::Deprecation.warn(':body in render deprecated. Please call body ' << - 'with a hash instead', caller[0,1]) + ActiveSupport::Deprecation.warn(':body in render deprecated. Please use instance ' << + 'variables as assigns instead', caller[0,1]) body options.delete(:body) end @@ -153,7 +145,7 @@ module ActionMailer create_parts create_mail end - @message + @_message end # Add a part to a multipart message, with the given content-type. The @@ -175,6 +167,7 @@ module ActionMailer # Add an attachment to a multipart message. This is simply a part with the # content-disposition set to "attachment". def attachment(params, &block) + ActiveSupport::Deprecation.warn "attachment is deprecated, please use the attachments API instead", caller[0,2] params = { :content_type => params } if String === params params[:content] ||= params.delete(:data) || params.delete(:body) @@ -197,13 +190,9 @@ module ActionMailer # render_message :template => "special_message" # render_message :inline => "<%= 'Hi!' %>" # - def render_message(object) - case object - when String - render_to_body(:template => object) - else - render_to_body(object) - end + def render_message(*args) + ActiveSupport::Deprecation.warn "render_message is deprecated, use render instead", caller[0,2] + render(*args) end private @@ -240,14 +229,12 @@ module ActionMailer end def create_mail #:nodoc: - m = @message - - m.subject, = quote_any_if_necessary(charset, subject) - m.to, m.from = quote_any_address_if_necessary(charset, recipients, from) - m.bcc = quote_address_if_necessary(bcc, charset) unless bcc.nil? - m.cc = quote_address_if_necessary(cc, charset) unless cc.nil? - m.reply_to = quote_address_if_necessary(reply_to, charset) unless reply_to.nil? - m.mime_version = mime_version unless mime_version.nil? + m = @_message + + quote_fields!({:subject => subject, :to => recipients, :from => from, + :bcc => bcc, :cc => cc, :reply_to => reply_to}, charset) + + m.mime_version = mime_version unless mime_version.nil? m.date = sent_on.to_time rescue sent_on if sent_on @headers.each { |k, v| m[k] = v } @@ -274,7 +261,7 @@ module ActionMailer m.content_transfer_encoding = '8bit' unless m.body.only_us_ascii? - @message + @_message end # Set up the default values for the various instance variables of this @@ -286,8 +273,9 @@ module ActionMailer @implicit_parts_order ||= self.class.default_implicit_parts_order.dup @mime_version ||= self.class.default_mime_version.dup if self.class.default_mime_version - @mailer_name ||= self.class.mailer_name.dup - @template ||= method_name + @mailer_name ||= self.class.mailer_name.dup + @template ||= method_name + @mail_was_called = false @parts ||= [] @headers ||= {} diff --git a/actionmailer/lib/action_mailer/tmail_compat.rb b/actionmailer/lib/action_mailer/tmail_compat.rb index d78332c135..c6efdc53b6 100644 --- a/actionmailer/lib/action_mailer/tmail_compat.rb +++ b/actionmailer/lib/action_mailer/tmail_compat.rb @@ -3,7 +3,7 @@ module Mail def set_content_type(*args) ActiveSupport::Deprecation.warn('Message#set_content_type is deprecated, please just call ' << - 'Message#content_type with the same arguments', caller[0,10]) + 'Message#content_type with the same arguments', caller[0,2]) content_type(*args) end @@ -11,7 +11,7 @@ module Mail def transfer_encoding(value = nil) if value ActiveSupport::Deprecation.warn('Message#transfer_encoding is deprecated, please call ' << - 'Message#content_transfer_encoding with the same arguments', caller[0,10]) + 'Message#content_transfer_encoding with the same arguments', caller[0,2]) content_transfer_encoding(value) else old_transfer_encoding @@ -20,7 +20,7 @@ module Mail def original_filename ActiveSupport::Deprecation.warn('Message#original_filename is deprecated, ' << - 'please call Message#filename', caller[0,10]) + 'please call Message#filename', caller[0,2]) filename end -- cgit v1.2.3 From f30d73bab4c676b187276797ac2a6dc89132c43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 17:31:18 +0100 Subject: Add new class delivery method API. --- actionmailer/lib/action_mailer/base.rb | 18 ++++++++++++++++- actionmailer/lib/action_mailer/deprecated_api.rb | 25 ++++++++++++++---------- 2 files changed, 32 insertions(+), 11 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index b881611cfb..8e30c54c49 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -253,6 +253,8 @@ 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 < AbstractController::Base + abstract! + include Quoting include AbstractController::Logger @@ -264,8 +266,8 @@ module ActionMailer #:nodoc: helper ActionMailer::MailHelper - include ActionMailer::DeprecatedApi extend ActionMailer::DeliveryMethods + include ActionMailer::DeprecatedApi add_delivery_method :smtp, Mail::SMTP, :address => "localhost", @@ -370,6 +372,20 @@ module ActionMailer #:nodoc: payload[:date] = mail.date payload[:mail] = mail.encoded end + + def respond_to?(method, *args) + super || action_methods.include?(method.to_s) + end + + protected + + def method_missing(method, *args) + if action_methods.include?(method.to_s) + new(method, *args).message + else + super + end + end end attr_internal :message diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index f969584a17..f36b1befd6 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -1,8 +1,8 @@ module ActionMailer - # TODO Remove this module all together in Rails 3.1. Ensure that super - # hooks in ActionMailer::Base are removed as well. - # - # Moved here to allow us to add the new Mail API + # Part of this API is deprecated and is going to be removed in Rails 3.1 (just check + # the methods which give you a warning). + # All the rest will be deprecated after 3.1 release instead, this allows a smoother + # migration path. module DeprecatedApi #:nodoc: extend ActiveSupport::Concern @@ -83,8 +83,8 @@ module ActionMailer # MyMailer.deliver(email) def deliver(mail, show_warning=true) if show_warning - ActiveSupport::Deprecation.warn "ActionMailer::Base.deliver is deprecated, just call " << - "deliver in the instance instead", caller + ActiveSupport::Deprecation.warn "#{self}.deliver is deprecated, call " << + "deliver in the mailer instance instead", caller[0,2] end raise "no mail object available for delivery!" unless mail @@ -100,9 +100,14 @@ module ActionMailer def method_missing(method_symbol, *parameters) #:nodoc: if match = matches_dynamic_method?(method_symbol) case match[1] - when 'create' then new(match[2], *parameters).message - when 'deliver' then new(match[2], *parameters).deliver! - when 'new' then nil + when 'create' + ActiveSupport::Deprecation.warn "#{self}.create_#{match[2]} is deprecated, " << + "use #{self}.#{match[2]} instead", caller[0,2] + new(match[2], *parameters).message + when 'deliver' + ActiveSupport::Deprecation.warn "#{self}.deliver_#{match[2]} is deprecated, " << + "use #{self}.#{match[2]}.deliver instead", caller[0,2] + new(match[2], *parameters).deliver else super end else @@ -167,7 +172,6 @@ module ActionMailer # Add an attachment to a multipart message. This is simply a part with the # content-disposition set to "attachment". def attachment(params, &block) - ActiveSupport::Deprecation.warn "attachment is deprecated, please use the attachments API instead", caller[0,2] params = { :content_type => params } if String === params params[:content] ||= params.delete(:data) || params.delete(:body) @@ -259,6 +263,7 @@ module ActionMailer end end + wrap_delivery_behavior! m.content_transfer_encoding = '8bit' unless m.body.only_us_ascii? @_message -- cgit v1.2.3 From 5dead5bb88cf04b039df8a1a51ffdb18d0443efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 17:49:44 +0100 Subject: Maintain old_api and deprecated_api in different files. --- actionmailer/lib/action_mailer/base.rb | 1 + actionmailer/lib/action_mailer/deprecated_api.rb | 252 +---------------------- actionmailer/lib/action_mailer/old_api.rb | 250 ++++++++++++++++++++++ 3 files changed, 260 insertions(+), 243 deletions(-) create mode 100644 actionmailer/lib/action_mailer/old_api.rb (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 8e30c54c49..77b9ac4a97 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -267,6 +267,7 @@ module ActionMailer #:nodoc: helper ActionMailer::MailHelper extend ActionMailer::DeliveryMethods + include ActionMailer::OldApi include ActionMailer::DeprecatedApi add_delivery_method :smtp, Mail::SMTP, diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index f36b1befd6..530a9c1922 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -1,78 +1,10 @@ module ActionMailer - # Part of this API is deprecated and is going to be removed in Rails 3.1 (just check - # the methods which give you a warning). - # All the rest will be deprecated after 3.1 release instead, this allows a smoother - # migration path. + # This is the API which is deprecated and is going to be removed on Rails 3.1 release. + # Part of the old API will be deprecated after 3.1, for a smoother deprecation process. + # Chech those in OldApi instead. module DeprecatedApi #:nodoc: extend ActiveSupport::Concern - included do - extend ActionMailer::AdvAttrAccessor - - @@protected_instance_variables = %w(@parts) - cattr_reader :protected_instance_variables - - # Specify the BCC addresses for the message - adv_attr_accessor :bcc - - # Specify the CC addresses for the message. - adv_attr_accessor :cc - - # Specify the charset to use for the message. This defaults to the - # +default_charset+ specified for ActionMailer::Base. - adv_attr_accessor :charset - - # Specify the content type for the message. This defaults to text/plain - # in most cases, but can be automatically set in some situations. - adv_attr_accessor :content_type - - # Specify the from address for the message. - adv_attr_accessor :from - - # Specify the address (if different than the "from" address) to direct - # replies to this message. - adv_attr_accessor :reply_to - - # Specify additional headers to be added to the message. - adv_attr_accessor :headers - - # Specify the order in which parts should be sorted, based on content-type. - # This defaults to the value for the +default_implicit_parts_order+. - adv_attr_accessor :implicit_parts_order - - # Defaults to "1.0", but may be explicitly given if needed. - adv_attr_accessor :mime_version - - # The recipient addresses for the message, either as a string (for a single - # address) or an array (for multiple addresses). - adv_attr_accessor :recipients - - # The date on which the message was sent. If not set (the default), the - # header will be set by the delivery agent. - adv_attr_accessor :sent_on - - # Specify the subject of the message. - adv_attr_accessor :subject - - # Specify the template name to use for current message. This is the "base" - # template name, without the extension or directory, and may be used to - # have multiple mailer methods share the same template. - adv_attr_accessor :template - - # Override the mailer name, which defaults to an inflected version of the - # mailer's class name. If you want to use a template in a non-standard - # location, you can use this to specify that location. - adv_attr_accessor :mailer_name - - # Define the body of the message. This is either a Hash (in which case it - # specifies the variables to pass to the template when it is rendered), - # or a string, in which case it specifies the actual text of the message. - adv_attr_accessor :body - - # Alias controller_path to mailer_name so render :partial in views work. - alias :controller_path :mailer_name - end - module ClassMethods # Deliver the given mail object directly. This can be used to deliver @@ -107,7 +39,7 @@ module ActionMailer when 'deliver' ActiveSupport::Deprecation.warn "#{self}.deliver_#{match[2]} is deprecated, " << "use #{self}.#{match[2]}.deliver instead", caller[0,2] - new(match[2], *parameters).deliver + new(match[2], *parameters).message.deliver else super end else @@ -127,6 +59,8 @@ module ActionMailer # object (from the create! method). If no cached mail object exists, and # no alternate has been given as the parameter, this will fail. def deliver!(mail = @_message) + ActiveSupport::Deprecation.warn "Calling deliver in the AM::Base object is deprecated, " << + "please call deliver in the Mail instance", caller[0,2] self.class.deliver(mail, false) end alias :deliver :deliver! @@ -143,48 +77,6 @@ module ActionMailer super end - def process(method_name, *args) - initialize_defaults(method_name) - super - unless @mail_was_called - create_parts - create_mail - end - @_message - end - - # 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) - 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] ||= params.delete(:data) || params.delete(:body) - - if params[:filename] - params = normalize_file_hash(params) - else - params = normalize_nonfile_hash(params) - end - - part(params, &block) - end - # Render a message but does not set it as mail body. Useful for rendering # data for part and attachments. # @@ -201,140 +93,14 @@ module ActionMailer private - def normalize_nonfile_hash(params) - content_disposition = "attachment;" - - mime_type = params.delete(:mime_type) - - if content_type = params.delete(:content_type) - content_type = "#{mime_type || content_type};" - end - - params[:body] = params.delete(:data) if params[:data] - - { :content_type => content_type, - :content_disposition => content_disposition }.merge(params) - end - - def normalize_file_hash(params) - filename = File.basename(params.delete(:filename)) - content_disposition = "attachment; filename=\"#{File.basename(filename)}\"" - - mime_type = params.delete(:mime_type) - - if (content_type = params.delete(:content_type)) && (content_type !~ /filename=/) - content_type = "#{mime_type || content_type}; filename=\"#{filename}\"" - end - - params[:body] = params.delete(:data) if params[:data] - - { :content_type => content_type, - :content_disposition => content_disposition }.merge(params) - end - - def create_mail #:nodoc: - m = @_message - - quote_fields!({:subject => subject, :to => recipients, :from => from, - :bcc => bcc, :cc => cc, :reply_to => reply_to}, charset) - - m.mime_version = mime_version unless mime_version.nil? - m.date = sent_on.to_time rescue sent_on if sent_on - - @headers.each { |k, v| m[k] = v } - - real_content_type, ctype_attrs = parse_content_type - main_type, sub_type = split_content_type(real_content_type) - - if @parts.size == 1 && @parts.first.parts.empty? - m.content_type([main_type, sub_type, ctype_attrs]) - m.body = @parts.first.body.encoded - else - @parts.each do |p| - m.add_part(p) - end - - m.body.set_sort_order(@implicit_parts_order) - m.body.sort_parts! - - if real_content_type =~ /multipart/ - ctype_attrs.delete "charset" - m.content_type([main_type, sub_type, ctype_attrs]) - end - end - - wrap_delivery_behavior! - m.content_transfer_encoding = '8bit' unless m.body.only_us_ascii? - - @_message - end - - # Set up the default values for the various instance variables of this - # mailer. Subclasses may override this method to provide different - # defaults. - def initialize_defaults(method_name) #:nodoc: - @charset ||= self.class.default_charset.dup - @content_type ||= self.class.default_content_type.dup - @implicit_parts_order ||= self.class.default_implicit_parts_order.dup - @mime_version ||= self.class.default_mime_version.dup if self.class.default_mime_version - - @mailer_name ||= self.class.mailer_name.dup - @template ||= method_name - @mail_was_called = false - - @parts ||= [] - @headers ||= {} - @sent_on ||= Time.now - @body ||= {} - end def create_parts #:nodoc: - if String === @body - self.response_body = @body - elsif @body.is_a?(Hash) && !@body.empty? + if @body.is_a?(Hash) && !@body.empty? + ActiveSupport::Deprecation.warn "Giving a hash to body is deprecated, please use instance variables instead", caller[0,2] @body.each { |k, v| instance_variable_set(:"@#{k}", v) } end - if String === response_body - @parts.unshift create_inline_part(response_body) - else - self.class.template_root.find_all(@template, {}, @mailer_name).each do |template| - @parts << create_inline_part(render_to_body(:_template => template), template.mime_type) - end - - if @parts.size > 1 - @content_type = "multipart/alternative" if @content_type !~ /^multipart/ - end - - # If this is a multipart e-mail add the mime_version if it is not - # already set. - @mime_version ||= "1.0" if !@parts.empty? - end - end - - def create_inline_part(body, mime_type=nil) #:nodoc: - ct = mime_type || "text/plain" - main_type, sub_type = split_content_type(ct.to_s) - - Mail::Part.new( - :content_type => [main_type, sub_type, {:charset => charset}], - :content_disposition => "inline", - :body => body - ) - end - - def split_content_type(ct) #:nodoc: - ct.to_s.split("/") - end - - def parse_content_type(defaults=nil) #:nodoc: - if @content_type.blank? - [ nil, {} ] - else - ctype, *attrs = @content_type.split(/;\s*/) - attrs = attrs.inject({}) { |h,s| k,v = s.split(/\=/, 2); h[k] = v; h } - [ctype, {"charset" => @charset}.merge(attrs)] - end + super end end diff --git a/actionmailer/lib/action_mailer/old_api.rb b/actionmailer/lib/action_mailer/old_api.rb new file mode 100644 index 0000000000..204dc473e8 --- /dev/null +++ b/actionmailer/lib/action_mailer/old_api.rb @@ -0,0 +1,250 @@ +module ActionMailer + module OldApi + extend ActiveSupport::Concern + + included do + extend ActionMailer::AdvAttrAccessor + + @@protected_instance_variables = %w(@parts) + cattr_reader :protected_instance_variables + + # Specify the BCC addresses for the message + adv_attr_accessor :bcc + + # Specify the CC addresses for the message. + adv_attr_accessor :cc + + # Specify the charset to use for the message. This defaults to the + # +default_charset+ specified for ActionMailer::Base. + adv_attr_accessor :charset + + # Specify the content type for the message. This defaults to text/plain + # in most cases, but can be automatically set in some situations. + adv_attr_accessor :content_type + + # Specify the from address for the message. + adv_attr_accessor :from + + # Specify the address (if different than the "from" address) to direct + # replies to this message. + adv_attr_accessor :reply_to + + # Specify additional headers to be added to the message. + adv_attr_accessor :headers + + # Specify the order in which parts should be sorted, based on content-type. + # This defaults to the value for the +default_implicit_parts_order+. + adv_attr_accessor :implicit_parts_order + + # Defaults to "1.0", but may be explicitly given if needed. + adv_attr_accessor :mime_version + + # The recipient addresses for the message, either as a string (for a single + # address) or an array (for multiple addresses). + adv_attr_accessor :recipients + + # The date on which the message was sent. If not set (the default), the + # header will be set by the delivery agent. + adv_attr_accessor :sent_on + + # Specify the subject of the message. + adv_attr_accessor :subject + + # Specify the template name to use for current message. This is the "base" + # template name, without the extension or directory, and may be used to + # have multiple mailer methods share the same template. + adv_attr_accessor :template + + # Override the mailer name, which defaults to an inflected version of the + # mailer's class name. If you want to use a template in a non-standard + # location, you can use this to specify that location. + adv_attr_accessor :mailer_name + + # Define the body of the message. This is either a Hash (in which case it + # specifies the variables to pass to the template when it is rendered), + # or a string, in which case it specifies the actual text of the message. + adv_attr_accessor :body + + # Alias controller_path to mailer_name so render :partial in views work. + alias :controller_path :mailer_name + end + + def process(method_name, *args) + initialize_defaults(method_name) + super + unless @mail_was_called + create_parts + create_mail + end + @_message + end + + # 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) + 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] ||= params.delete(:data) || params.delete(:body) + + if params[:filename] + params = normalize_file_hash(params) + else + params = normalize_nonfile_hash(params) + end + + part(params, &block) + end + + protected + + def normalize_nonfile_hash(params) + content_disposition = "attachment;" + + mime_type = params.delete(:mime_type) + + if content_type = params.delete(:content_type) + content_type = "#{mime_type || content_type};" + end + + params[:body] = params.delete(:data) if params[:data] + + { :content_type => content_type, + :content_disposition => content_disposition }.merge(params) + end + + def normalize_file_hash(params) + filename = File.basename(params.delete(:filename)) + content_disposition = "attachment; filename=\"#{File.basename(filename)}\"" + + mime_type = params.delete(:mime_type) + + if (content_type = params.delete(:content_type)) && (content_type !~ /filename=/) + content_type = "#{mime_type || content_type}; filename=\"#{filename}\"" + end + + params[:body] = params.delete(:data) if params[:data] + + { :content_type => content_type, + :content_disposition => content_disposition }.merge(params) + end + + def create_mail #:nodoc: + m = @_message + + quote_fields!({:subject => subject, :to => recipients, :from => from, + :bcc => bcc, :cc => cc, :reply_to => reply_to}, charset) + + m.mime_version = mime_version unless mime_version.nil? + m.date = sent_on.to_time rescue sent_on if sent_on + + @headers.each { |k, v| m[k] = v } + + real_content_type, ctype_attrs = parse_content_type + main_type, sub_type = split_content_type(real_content_type) + + if @parts.size == 1 && @parts.first.parts.empty? + m.content_type([main_type, sub_type, ctype_attrs]) + m.body = @parts.first.body.encoded + else + @parts.each do |p| + m.add_part(p) + end + + m.body.set_sort_order(@implicit_parts_order) + m.body.sort_parts! + + if real_content_type =~ /multipart/ + ctype_attrs.delete "charset" + m.content_type([main_type, sub_type, ctype_attrs]) + end + end + + wrap_delivery_behavior! + m.content_transfer_encoding = '8bit' unless m.body.only_us_ascii? + + @_message + end + + # Set up the default values for the various instance variables of this + # mailer. Subclasses may override this method to provide different + # defaults. + def initialize_defaults(method_name) #:nodoc: + @charset ||= self.class.default_charset.dup + @content_type ||= self.class.default_content_type.dup + @implicit_parts_order ||= self.class.default_implicit_parts_order.dup + @mime_version ||= self.class.default_mime_version.dup if self.class.default_mime_version + + @mailer_name ||= self.class.mailer_name.dup + @template ||= method_name + @mail_was_called = false + + @parts ||= [] + @headers ||= {} + @sent_on ||= Time.now + @body ||= {} + end + + def create_parts #:nodoc: + if String === @body + self.response_body = @body + end + + if String === response_body + @parts.unshift create_inline_part(response_body) + else + self.class.template_root.find_all(@template, {}, @mailer_name).each do |template| + @parts << create_inline_part(render_to_body(:_template => template), template.mime_type) + end + + if @parts.size > 1 + @content_type = "multipart/alternative" if @content_type !~ /^multipart/ + end + + # If this is a multipart e-mail add the mime_version if it is not + # already set. + @mime_version ||= "1.0" if !@parts.empty? + end + end + + def create_inline_part(body, mime_type=nil) #:nodoc: + ct = mime_type || "text/plain" + main_type, sub_type = split_content_type(ct.to_s) + + Mail::Part.new( + :content_type => [main_type, sub_type, {:charset => charset}], + :content_disposition => "inline", + :body => body + ) + end + + def split_content_type(ct) #:nodoc: + ct.to_s.split("/") + end + + def parse_content_type(defaults=nil) #:nodoc: + if @content_type.blank? + [ nil, {} ] + else + ctype, *attrs = @content_type.split(/;\s*/) + attrs = attrs.inject({}) { |h,s| k,v = s.split(/\=/, 2); h[k] = v; h } + [ctype, {"charset" => @charset}.merge(attrs)] + end + end + end +end \ No newline at end of file -- cgit v1.2.3 From 0d931fecbb1132abb71a83bb91435812f2012d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 18:11:57 +0100 Subject: Finish cleaning up delivery methods implementation. --- actionmailer/lib/action_mailer/base.rb | 29 +------ actionmailer/lib/action_mailer/delivery_methods.rb | 96 ++++++++++++---------- 2 files changed, 55 insertions(+), 70 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 77b9ac4a97..ad3a9c3c6d 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -253,10 +253,9 @@ 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 < AbstractController::Base + include DeliveryMethods, Quoting abstract! - include Quoting - include AbstractController::Logger include AbstractController::Rendering include AbstractController::LocalizedCache @@ -266,31 +265,9 @@ module ActionMailer #:nodoc: helper ActionMailer::MailHelper - extend ActionMailer::DeliveryMethods include ActionMailer::OldApi include ActionMailer::DeprecatedApi - add_delivery_method :smtp, Mail::SMTP, - :address => "localhost", - :port => 25, - :domain => 'localhost.localdomain', - :user_name => nil, - :password => nil, - :authentication => nil, - :enable_starttls_auto => true - - add_delivery_method :file, Mail::FileDelivery, - :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" - - add_delivery_method :sendmail, Mail::Sendmail, - :location => '/usr/sbin/sendmail', - :arguments => '-i -t' - - add_delivery_method :test, Mail::TestMailer - - superclass_delegating_reader :delivery_method - self.delivery_method = :smtp - private_class_method :new #:nodoc: cattr_accessor :raise_delivery_errors @@ -493,10 +470,6 @@ module ActionMailer #:nodoc: [responses, sort_order] end - def wrap_delivery_behavior!(method=nil) #:nodoc: - self.class.wrap_delivery_behavior(@_message, method) - end - def create_parts_from_responses(m, responses, charset) #:nodoc: if responses.size == 1 && !m.has_attachments? m.body = responses[0][:body] diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb index 16b84d4118..38325e512f 100644 --- a/actionmailer/lib/action_mailer/delivery_methods.rb +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -1,63 +1,75 @@ +require 'tmpdir' + module ActionMailer - # This modules makes a DSL for adding delivery methods to ActionMailer + # Provides a DSL for adding delivery methods to ActionMailer. module DeliveryMethods - # TODO Make me class inheritable - def delivery_settings - @@delivery_settings ||= Hash.new { |h,k| h[k] = {} } - end + extend ActiveSupport::Concern - def delivery_methods - @@delivery_methods ||= {} - end + included do + extlib_inheritable_accessor :delivery_methods, :delivery_method, + :instance_writer => false - def delivery_method=(method) - raise ArgumentError, "Unknown delivery method #{method.inspect}" unless delivery_methods[method] - @delivery_method = method - end + self.delivery_methods = {} + self.delivery_method = :smtp - def add_delivery_method(symbol, klass, default_options={}) - self.delivery_methods[symbol] = klass - self.delivery_settings[symbol] = default_options - end + add_delivery_method :smtp, Mail::SMTP, + :address => "localhost", + :port => 25, + :domain => 'localhost.localdomain', + :user_name => nil, + :password => nil, + :authentication => nil, + :enable_starttls_auto => true - def wrap_delivery_behavior(mail, method=nil) - method ||= delivery_method + add_delivery_method :file, Mail::FileDelivery, + :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails" - mail.register_for_delivery_notification(self) + add_delivery_method :sendmail, Mail::Sendmail, + :location => '/usr/sbin/sendmail', + :arguments => '-i -t' - if method.is_a?(Symbol) - mail.delivery_method(delivery_methods[method], - delivery_settings[method]) - else - mail.delivery_method(method) - end - - mail.perform_deliveries = perform_deliveries - mail.raise_delivery_errors = raise_delivery_errors + add_delivery_method :test, Mail::TestMailer end + module ClassMethods + # Adds a new delivery method through the given class using the given symbol + # as alias and the default options supplied: + # + # Example: + # + # add_delivery_method :sendmail, Mail::Sendmail, + # :location => '/usr/sbin/sendmail', + # :arguments => '-i -t' + # + def add_delivery_method(symbol, klass, default_options={}) + unless respond_to?(:"#{symbol}_settings") + extlib_inheritable_accessor(:"#{symbol}_settings", :instance_writer => false) + end - def respond_to?(method_symbol, include_private = false) #:nodoc: - matches_settings_method?(method_symbol) || super - end + send(:"#{symbol}_settings=", default_options) + self.delivery_methods[symbol.to_sym] = klass + end - protected + def wrap_delivery_behavior(mail, method=delivery_method) #:nodoc: + mail.register_for_delivery_notification(self) - # TODO Get rid of this method missing magic - def method_missing(method_symbol, *parameters) #:nodoc: - if match = matches_settings_method?(method_symbol) - if match[2] - delivery_settings[match[1].to_sym] = parameters[0] + if method.is_a?(Symbol) + if klass = delivery_methods[method.to_sym] + mail.delivery_method(klass, send(:"#{method}_settings")) + else + raise "Invalid delivery method #{method.inspect}" + end else - delivery_settings[match[1].to_sym] + mail.delivery_method(method) end - else - super + + mail.perform_deliveries = perform_deliveries + mail.raise_delivery_errors = raise_delivery_errors end end - def matches_settings_method?(method_name) #:nodoc: - /(#{delivery_methods.keys.join('|')})_settings(=)?$/.match(method_name.to_s) + def wrap_delivery_behavior!(*args) #:nodoc: + self.class.wrap_delivery_behavior(message, *args) end end end \ No newline at end of file -- cgit v1.2.3 From 99f960a3d73b62a957988bbee0906264f35afc2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 18:40:04 +0100 Subject: Handle some TODOs and deprecations. --- .../lib/action_mailer/adv_attr_accessor.rb | 2 -- actionmailer/lib/action_mailer/base.rb | 39 +++++++++++----------- actionmailer/lib/action_mailer/deprecated_api.rb | 21 +++++++----- actionmailer/lib/action_mailer/old_api.rb | 16 ++++----- actionmailer/lib/action_mailer/test_helper.rb | 1 - 5 files changed, 41 insertions(+), 38 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/adv_attr_accessor.rb b/actionmailer/lib/action_mailer/adv_attr_accessor.rb index 91992ed839..be6b1feca9 100644 --- a/actionmailer/lib/action_mailer/adv_attr_accessor.rb +++ b/actionmailer/lib/action_mailer/adv_attr_accessor.rb @@ -1,8 +1,6 @@ module ActionMailer module AdvAttrAccessor #:nodoc: def adv_attr_accessor(*names) - - # TODO: ActiveSupport::Deprecation.warn() names.each do |name| ivar = "@#{name}" diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index ad3a9c3c6d..7e984124b7 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -302,7 +302,6 @@ module ActionMailer #:nodoc: @mailer_name ||= name.underscore end attr_writer :mailer_name - alias :controller_path :mailer_name # Receives a raw email, parses it into an email object, decodes it, @@ -324,23 +323,21 @@ module ActionMailer #:nodoc: end end - def template_root - self.view_paths && self.view_paths.first - end - - # Should template root overwrite the whole view_paths? - def template_root=(root) - self.view_paths = ActionView::Base.process_view_paths(root) - end - # TODO The delivery should happen inside the instrument block def delivered_email(mail) - ActiveSupport::Notifications.instrument("action_mailer.deliver", :mailer => self.name) do |payload| + ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload| self.set_payload_for_mail(payload, mail) end end + def respond_to?(method, *args) #:nodoc: + super || action_methods.include?(method.to_s) + end + + protected + def set_payload_for_mail(payload, mail) #:nodoc: + payload[:mailer] = self.name payload[:message_id] = mail.message_id payload[:subject] = mail.subject payload[:to] = mail.to @@ -351,13 +348,7 @@ module ActionMailer #:nodoc: payload[:mail] = mail.encoded end - def respond_to?(method, *args) - super || action_methods.include?(method.to_s) - end - - protected - - def method_missing(method, *args) + def method_missing(method, *args) #:nodoc: if action_methods.include?(method.to_s) new(method, *args).message else @@ -459,7 +450,7 @@ module ActionMailer #:nodoc: :content_type => self.class.default_content_type.dup } else - self.class.template_root.find_all(action_name, {}, self.class.mailer_name).each do |template| + each_template do |template| responses << { :body => render_to_body(:_template => template), :content_type => template.mime_type.to_s @@ -470,6 +461,16 @@ module ActionMailer #:nodoc: [responses, sort_order] end + def each_template(&block) #:nodoc: + self.class.view_paths.each do |load_paths| + templates = load_paths.find_all(action_name, {}, self.class.mailer_name) + unless templates.empty? + templates.each(&block) + return + end + end + end + def create_parts_from_responses(m, responses, charset) #:nodoc: if responses.size == 1 && !m.has_attachments? m.body = responses[0][:body] diff --git a/actionmailer/lib/action_mailer/deprecated_api.rb b/actionmailer/lib/action_mailer/deprecated_api.rb index 530a9c1922..0eb8d85676 100644 --- a/actionmailer/lib/action_mailer/deprecated_api.rb +++ b/actionmailer/lib/action_mailer/deprecated_api.rb @@ -25,11 +25,20 @@ module ActionMailer mail end - def respond_to?(method_symbol, include_private = false) #:nodoc: + def template_root + self.view_paths && self.view_paths.first + end + + def template_root=(root) + ActiveSupport::Deprecation.warn "template_root= is deprecated, use view_paths.unshift instead", caller[0,2] + self.view_paths = ActionView::Base.process_view_paths(root) + end + + def respond_to?(method_symbol, include_private = false) matches_dynamic_method?(method_symbol) || super end - def method_missing(method_symbol, *parameters) #:nodoc: + def method_missing(method_symbol, *parameters) if match = matches_dynamic_method?(method_symbol) case match[1] when 'create' @@ -49,7 +58,7 @@ module ActionMailer private - def matches_dynamic_method?(method_name) #:nodoc: + def matches_dynamic_method?(method_name) method_name = method_name.to_s /^(create|deliver)_([_a-z]\w*)/.match(method_name) || /^(new)$/.match(method_name) end @@ -70,10 +79,8 @@ module ActionMailer if options[:body] ActiveSupport::Deprecation.warn(':body in render deprecated. Please use instance ' << 'variables as assigns instead', caller[0,1]) - body options.delete(:body) end - super end @@ -93,13 +100,11 @@ module ActionMailer private - - def create_parts #:nodoc: + def create_parts if @body.is_a?(Hash) && !@body.empty? ActiveSupport::Deprecation.warn "Giving a hash to body is deprecated, please use instance variables instead", caller[0,2] @body.each { |k, v| instance_variable_set(:"@#{k}", v) } end - super end diff --git a/actionmailer/lib/action_mailer/old_api.rb b/actionmailer/lib/action_mailer/old_api.rb index 204dc473e8..f5b077ab98 100644 --- a/actionmailer/lib/action_mailer/old_api.rb +++ b/actionmailer/lib/action_mailer/old_api.rb @@ -1,5 +1,5 @@ module ActionMailer - module OldApi + module OldApi #:nodoc: extend ActiveSupport::Concern included do @@ -144,7 +144,7 @@ module ActionMailer :content_disposition => content_disposition }.merge(params) end - def create_mail #:nodoc: + def create_mail m = @_message quote_fields!({:subject => subject, :to => recipients, :from => from, @@ -184,7 +184,7 @@ module ActionMailer # Set up the default values for the various instance variables of this # mailer. Subclasses may override this method to provide different # defaults. - def initialize_defaults(method_name) #:nodoc: + def initialize_defaults(method_name) @charset ||= self.class.default_charset.dup @content_type ||= self.class.default_content_type.dup @implicit_parts_order ||= self.class.default_implicit_parts_order.dup @@ -200,7 +200,7 @@ module ActionMailer @body ||= {} end - def create_parts #:nodoc: + def create_parts if String === @body self.response_body = @body end @@ -208,7 +208,7 @@ module ActionMailer if String === response_body @parts.unshift create_inline_part(response_body) else - self.class.template_root.find_all(@template, {}, @mailer_name).each do |template| + self.class.view_paths.first.find_all(@template, {}, @mailer_name).each do |template| @parts << create_inline_part(render_to_body(:_template => template), template.mime_type) end @@ -222,7 +222,7 @@ module ActionMailer end end - def create_inline_part(body, mime_type=nil) #:nodoc: + def create_inline_part(body, mime_type=nil) ct = mime_type || "text/plain" main_type, sub_type = split_content_type(ct.to_s) @@ -233,11 +233,11 @@ module ActionMailer ) end - def split_content_type(ct) #:nodoc: + def split_content_type(ct) ct.to_s.split("/") end - def parse_content_type(defaults=nil) #:nodoc: + def parse_content_type(defaults=nil) if @content_type.blank? [ nil, {} ] else diff --git a/actionmailer/lib/action_mailer/test_helper.rb b/actionmailer/lib/action_mailer/test_helper.rb index f234c0248c..3a1612442f 100644 --- a/actionmailer/lib/action_mailer/test_helper.rb +++ b/actionmailer/lib/action_mailer/test_helper.rb @@ -58,7 +58,6 @@ module ActionMailer end end -# TODO: Deprecate this module Test module Unit class TestCase -- cgit v1.2.3 From bd96614101262e0ad0cc176ed8e2d95a5c17936b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 19:36:42 +0100 Subject: Move old tests to a specific folder and add some delivery method tests. --- actionmailer/lib/action_mailer/base.rb | 10 +--------- actionmailer/lib/action_mailer/delivery_methods.rb | 16 ++++++++++++++-- actionmailer/lib/action_mailer/mail_helper.rb | 2 +- 3 files changed, 16 insertions(+), 12 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 7e984124b7..1b81cbf5af 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -256,6 +256,7 @@ module ActionMailer #:nodoc: include DeliveryMethods, Quoting abstract! + # TODO Add some sanity tests for the included modules include AbstractController::Logger include AbstractController::Rendering include AbstractController::LocalizedCache @@ -270,12 +271,6 @@ module ActionMailer #:nodoc: private_class_method :new #:nodoc: - cattr_accessor :raise_delivery_errors - @@raise_delivery_errors = true - - cattr_accessor :perform_deliveries - @@perform_deliveries = true - extlib_inheritable_accessor :default_charset self.default_charset = "utf-8" @@ -295,9 +290,6 @@ module ActionMailer #:nodoc: self.default_implicit_parts_order = [ "text/plain", "text/enriched", "text/html" ] class << self - # Provides a list of emails that have been delivered by Mail - delegate :deliveries, :deliveries=, :to => Mail - def mailer_name @mailer_name ||= name.underscore end diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb index 38325e512f..21909d5d57 100644 --- a/actionmailer/lib/action_mailer/delivery_methods.rb +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -1,7 +1,8 @@ require 'tmpdir' module ActionMailer - # Provides a DSL for adding delivery methods to ActionMailer. + # This modules handles everything related to the delivery, from registering new + # delivery methods to configuring the mail object to be send. module DeliveryMethods extend ActiveSupport::Concern @@ -9,6 +10,13 @@ module ActionMailer extlib_inheritable_accessor :delivery_methods, :delivery_method, :instance_writer => false + # Do not make this inheritable, because we always want it to propagate + cattr_accessor :raise_delivery_errors + self.raise_delivery_errors = true + + cattr_accessor :perform_deliveries + self.perform_deliveries = true + self.delivery_methods = {} self.delivery_method = :smtp @@ -32,6 +40,9 @@ module ActionMailer end module ClassMethods + # Provides a list of emails that have been delivered by Mail + delegate :deliveries, :deliveries=, :to => Mail + # Adds a new delivery method through the given class using the given symbol # as alias and the default options supplied: # @@ -50,7 +61,8 @@ module ActionMailer self.delivery_methods[symbol.to_sym] = klass end - def wrap_delivery_behavior(mail, method=delivery_method) #:nodoc: + def wrap_delivery_behavior(mail, method=nil) #:nodoc: + method ||= self.delivery_method mail.register_for_delivery_notification(self) if method.is_a?(Symbol) diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb index adba94cbef..01da954b03 100644 --- a/actionmailer/lib/action_mailer/mail_helper.rb +++ b/actionmailer/lib/action_mailer/mail_helper.rb @@ -23,7 +23,7 @@ module ActionMailer # Access the message instance. def message #:nodoc: - @message + @_message end end end -- cgit v1.2.3 From a74a655648618a6ed568b9b4ef3a17a8970e7774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 19:52:44 +0100 Subject: Add tests to mail helper. --- actionmailer/lib/action_mailer/base.rb | 1 - actionmailer/lib/action_mailer/mail_helper.rb | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 1b81cbf5af..7101bfbb70 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -256,7 +256,6 @@ module ActionMailer #:nodoc: include DeliveryMethods, Quoting abstract! - # TODO Add some sanity tests for the included modules include AbstractController::Logger include AbstractController::Rendering include AbstractController::LocalizedCache diff --git a/actionmailer/lib/action_mailer/mail_helper.rb b/actionmailer/lib/action_mailer/mail_helper.rb index 01da954b03..ab5c3469b2 100644 --- a/actionmailer/lib/action_mailer/mail_helper.rb +++ b/actionmailer/lib/action_mailer/mail_helper.rb @@ -17,12 +17,12 @@ module ActionMailer end # Access the mailer instance. - def mailer #:nodoc: + def mailer @_controller end # Access the message instance. - def message #:nodoc: + def message @_message end end -- cgit v1.2.3 From 0ece244feec236f57fb2f55ea564409f25475923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Sun, 24 Jan 2010 23:59:12 +0100 Subject: Ensure implicit multipart templates with locale works as expected. --- actionmailer/lib/action_mailer/base.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 7101bfbb70..84d50fa1f4 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -1,5 +1,8 @@ require 'active_support/core_ext/class' +require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/array/uniq_by' require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/string/inflections' require 'mail' require 'action_mailer/tmail_compat' require 'action_mailer/collector' @@ -455,6 +458,8 @@ module ActionMailer #:nodoc: def each_template(&block) #:nodoc: self.class.view_paths.each do |load_paths| templates = load_paths.find_all(action_name, {}, self.class.mailer_name) + templates = templates.uniq_by { |t| t.details[:formats] } + unless templates.empty? templates.each(&block) return -- cgit v1.2.3 From e1c131863897390d04bd5515765236590747f2c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Mon, 25 Jan 2010 00:37:12 +0100 Subject: Added delivers_from. --- actionmailer/lib/action_mailer/base.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 84d50fa1f4..552fd7ccb8 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -273,6 +273,9 @@ module ActionMailer #:nodoc: private_class_method :new #:nodoc: + extlib_inheritable_accessor :default_from + self.default_from = nil + extlib_inheritable_accessor :default_charset self.default_charset = "utf-8" @@ -298,6 +301,12 @@ module ActionMailer #:nodoc: attr_writer :mailer_name alias :controller_path :mailer_name + # Sets who is the default sender for the e-mail + def delivers_from(value = nil) + self.default_from = value if value + self.default_from + end + # Receives a raw email, parses it into an email object, decodes it, # instantiates a new mailer, and passes the email object to the mailer # object's +receive+ method. If you want your mailer to be able to @@ -318,7 +327,7 @@ module ActionMailer #:nodoc: end # TODO The delivery should happen inside the instrument block - def delivered_email(mail) + def delivered_email(mail) #:nodoc: ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload| self.set_payload_for_mail(payload, mail) end @@ -387,8 +396,9 @@ module ActionMailer #:nodoc: charset = headers[:charset] || m.charset || self.class.default_charset.dup mime_version = headers[:mime_version] || m.mime_version || self.class.default_mime_version.dup - # Set subjects and fields quotings + # Set fields quotings headers[:subject] ||= default_subject + headers[:from] ||= self.class.default_from.dup quote_fields!(headers, charset) # Render the templates and blocks -- cgit v1.2.3 From e4a989e9d99b9860859ad14b5a6fca62a4009cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Mon, 25 Jan 2010 13:39:48 +1100 Subject: Added delivery_handler method to mail and implemented in ActionMailer to deliver inside of instrumentation --- actionmailer/lib/action_mailer/base.rb | 13 +++++++++++-- actionmailer/lib/action_mailer/delivery_methods.rb | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 552fd7ccb8..4e5e1bbb29 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -326,10 +326,19 @@ module ActionMailer #:nodoc: end end - # TODO The delivery should happen inside the instrument block - def delivered_email(mail) #:nodoc: + def deliver_mail(mail) #:nodoc: ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload| self.set_payload_for_mail(payload, mail) + + if mail.perform_deliveries + begin + mail.deliver! + rescue Exception => e + raise e if mail.raise_delivery_errors + end + Mail.deliveries << mail + end + end end diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb index 21909d5d57..7a6b410932 100644 --- a/actionmailer/lib/action_mailer/delivery_methods.rb +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -63,7 +63,7 @@ module ActionMailer def wrap_delivery_behavior(mail, method=nil) #:nodoc: method ||= self.delivery_method - mail.register_for_delivery_notification(self) + mail.delivery_handler = self if method.is_a?(Symbol) if klass = delivery_methods[method.to_sym] -- cgit v1.2.3 From ace74974cf3575bbd3bf7ff4d8a83c3100fd22a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Mon, 25 Jan 2010 21:47:03 +1100 Subject: Got AM working with Mail yield on delivery_handler and updated tests --- actionmailer/lib/action_mailer/base.rb | 9 +-------- actionmailer/lib/action_mailer/delivery_methods.rb | 4 ++-- 2 files changed, 3 insertions(+), 10 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 4e5e1bbb29..47d0ffaf68 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -330,14 +330,7 @@ module ActionMailer #:nodoc: ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload| self.set_payload_for_mail(payload, mail) - if mail.perform_deliveries - begin - mail.deliver! - rescue Exception => e - raise e if mail.raise_delivery_errors - end - Mail.deliveries << mail - end + yield # Let Mail do the delivery actions end end diff --git a/actionmailer/lib/action_mailer/delivery_methods.rb b/actionmailer/lib/action_mailer/delivery_methods.rb index 7a6b410932..34bfe6000a 100644 --- a/actionmailer/lib/action_mailer/delivery_methods.rb +++ b/actionmailer/lib/action_mailer/delivery_methods.rb @@ -40,8 +40,8 @@ module ActionMailer end module ClassMethods - # Provides a list of emails that have been delivered by Mail - delegate :deliveries, :deliveries=, :to => Mail + # Provides a list of emails that have been delivered by Mail::TestMailer + delegate :deliveries, :deliveries=, :to => Mail::TestMailer # Adds a new delivery method through the given class using the given symbol # as alias and the default options supplied: -- cgit v1.2.3 From 4a6eba3232fec13892f36fc4730bb2deef342fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Mon, 25 Jan 2010 23:46:09 +1100 Subject: Added initial documentation for the new API --- actionmailer/lib/action_mailer/base.rb | 257 ++++++++++++++++++++------------- 1 file changed, 159 insertions(+), 98 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 47d0ffaf68..51a8c07a28 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -16,46 +16,57 @@ module ActionMailer #:nodoc: # # $ script/generate mailer Notifier # - # The generated model inherits from ActionMailer::Base. Emails are defined by creating methods within the model which are then - # used to set variables to be used in the mail template, to change options on the mail, or - # to add attachments. + # The generated model inherits from ActionMailer::Base. Emails are defined by creating methods + # within the model which are then used to set variables to be used in the mail template, to + # change options on the mail, or to add attachments. # # Examples: # # class Notifier < ActionMailer::Base - # def signup_notification(recipient) - # recipients recipient.email_address_with_name - # bcc ["bcc@example.com", "Order Watcher "] - # from "system@example.com" - # subject "New account information" - # body :account => recipient + # delivers_from 'system@example.com' + # + # def welcome(recipient) + # @account = recipient + # mail { :to => recipient.email_address_with_name, + # :bcc => ["bcc@example.com", "Order Watcher "], + # :subject => "New account information" } + # end # end - # end - # - # Mailer methods have the following configuration methods available. - # - # * recipients - Takes one or more email addresses. These addresses are where your email will be delivered to. Sets the To: header. - # * subject - The subject of your email. Sets the Subject: header. - # * from - Who the email you are sending is from. Sets the From: header. - # * cc - Takes one or more email addresses. These addresses will receive a carbon copy of your email. Sets the Cc: header. - # * bcc - Takes one or more email addresses. These addresses will receive a blind carbon copy of your email. Sets the Bcc: header. - # * reply_to - Takes one or more email addresses. These addresses will be listed as the default recipients when replying to your email. Sets the Reply-To: header. - # * sent_on - The date on which the message was sent. If not set, the header will be set by the delivery agent. - # * content_type - Specify the content type of the message. Defaults to text/plain. - # * headers - Specify additional headers to be set for the message, e.g. headers 'X-Mail-Count' => 107370. - # - # When a headers 'return-path' is specified, that value will be used as the 'envelope from' - # address. Setting this is useful when you want delivery notifications sent to a different address than - # the one in from. - # + # + # Within the mailer method, you have access to the following methods: + # + # * attachments[]= - Allows you to add attachments to your email in an intuitive + # manner; attachments['filename.png'] = File.read('path/to/filename.png') + # * headers[]= - Allows you to specify non standard headers in your email such + # as headers['X-No-Spam'] = 'True' + # * mail - Allows you to specify your email to send. + # + # The hash passed to the mail method allows you to specify the most used headers in an email + # message, such as Subject, To, From, Cc, Bcc, + # Reply-To and Date. See the ActionMailer#mail method for more details. + # + # If you need other headers not listed above, use the headers['name'] = value method. + # + # The mail method, if not passed a block, will inspect your views and send all the views with + # the same name as the method, so the above action would send the +welcome.plain.erb+ view file + # as well as the +welcome.html.erb+ view file in a +multipart/alternate+ email. + # + # If you want to explicitly render only certain templates, pass a block: + # + # mail(:to => user.emai) do |format| + # format.text + # format.enriched, {:content_type => 'text/rtf'} + # format.html + # end # # = Mailer views # - # Like Action Controller, each mailer class has a corresponding view directory - # in which each method of the class looks for a template with its name. - # To define a template to be used with a mailing, create an .erb file with the same name as the method - # in your mailer model. For example, in the mailer defined above, the template at - # app/views/notifier/signup_notification.erb would be used to generate the email. + # Like Action Controller, each mailer class has a corresponding view directory in which each + # method of the class looks for a template with its name. + # + # To define a template to be used with a mailing, create an .erb file with the same + # name as the method in your mailer model. For example, in the mailer defined above, the template at + # app/views/notifier/signup_notification.text.erb would be used to generate the email. # # Variables defined in the model are accessible as instance variables in the view. # @@ -111,54 +122,13 @@ module ActionMailer #:nodoc: # Once a mailer action and template are defined, you can deliver your message or create it and save it # for delivery later: # - # Notifier.deliver_signup_notification(david) # sends the email - # mail = Notifier.create_signup_notification(david) # => a tmail object - # Notifier.deliver(mail) - # - # You never instantiate your mailer class. Rather, your delivery instance - # methods are automatically wrapped in class methods that start with the word - # deliver_ followed by the name of the mailer method that you would - # like to deliver. The signup_notification method defined above is - # delivered by invoking Notifier.deliver_signup_notification. + # Notifier.welcome(david).deliver # sends the email + # mail = Notifier.welcome(david) # => a Mail::Message object + # mail.deliver # sends the email # + # You never instantiate your mailer class. Rather, you just call the method on the class itself. # - # = HTML email - # - # To send mail as HTML, make sure your view (the .erb file) generates HTML and - # set the content type to html. - # - # class MyMailer < ActionMailer::Base - # def signup_notification(recipient) - # recipients recipient.email_address_with_name - # subject "New account information" - # from "system@example.com" - # body :account => recipient - # content_type "text/html" - # end - # end - # - # - # = Multipart email - # - # You can explicitly specify multipart messages: - # - # class ApplicationMailer < ActionMailer::Base - # def signup_notification(recipient) - # recipients recipient.email_address_with_name - # subject "New account information" - # from "system@example.com" - # content_type "multipart/alternative" - # body :account => recipient - # - # part :content_type => "text/html", - # :data => render_message("signup-as-html") - # - # part "text/plain" do |p| - # p.body = render_message("signup-as-plain") - # p.content_transfer_encoding = "base64" - # end - # end - # end + # = Multipart Emails # # Multipart messages can also be used implicitly because Action Mailer will automatically # detect and use multipart templates, where each template is named after the name of the action, followed @@ -170,11 +140,10 @@ module ActionMailer #:nodoc: # * signup_notification.text.xml.builder # * signup_notification.text.x-yaml.erb # - # Each would be rendered and added as a separate part to the message, - # with the corresponding content type. The content type for the entire - # message is automatically set to multipart/alternative, which indicates - # that the email contains multiple different representations of the same email - # body. The same body hash is passed to each template. + # Each would be rendered and added as a separate part to the message, with the corresponding content + # type. The content type for the entire message is automatically set to multipart/alternative, + # which indicates that the email contains multiple different representations of the same email + # body. The same instance variables defined in the action are passed to all email templates. # # Implicit template rendering is not performed if any attachments or parts have been added to the email. # This means that you'll have to manually add each part to the email and set the content type of the email @@ -182,31 +151,31 @@ module ActionMailer #:nodoc: # # = Attachments # - # Attachments can be added by using the +attachment+ method. - # - # Example: + # You can see above how to make a multipart HTML / Text email, to send attachments is just + # as easy: # # class ApplicationMailer < ActionMailer::Base - # # attachments - # def signup_notification(recipient) - # recipients recipient.email_address_with_name - # subject "New account information" - # from "system@example.com" - # - # attachment :content_type => "image/jpeg", - # :body => File.read("an-image.jpg") - # - # attachment "application/pdf" do |a| - # a.body = generate_your_pdf_here() - # end + # def welcome(recipient) + # attachments['free_book.pdf'] = { :data => File.read('path/to/file.pdf') } + # mail(:to => recipient, :subject => "New account information") # end # end + # + # Which will (if it had both a .text.erb and .html.erb tempalte in the view + # directory), send a complete multipart/mixed email with two parts, the first part being + # a multipart/alternate with the text and HTML email parts inside, and the second being + # a application/pdf with a Base64 encoded copy of the file.pdf book with the filename + # +free_book.pdf+. # # # = Configuration options # # These options are specified on the class level, like ActionMailer::Base.template_root = "/my/templates" # + # * delivers_from - Pass this the address that then defaults as the +from+ address on all the + # emails sent. Can be overridden on a per mail basis by passing :from => 'another@address' in + # the +mail+ method. + # # * template_root - Determines the base from which template references will be made. # # * logger - the logger is used for generating information on the mailing run if available. @@ -326,6 +295,9 @@ module ActionMailer #:nodoc: end end + # Delivers a mail object. This is actually called by the Mail::Message object + # itself through a call back when you call :deliver on the Mail::Message, + # calling +deliver_mail+ directly and passing an Mail::Message will do nothing. def deliver_mail(mail) #:nodoc: ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload| self.set_payload_for_mail(payload, mail) @@ -374,6 +346,14 @@ module ActionMailer #:nodoc: process(method_name, *args) if method_name end + # Allows you to pass random and unusual headers to the new +Mail::Message+ object + # which will add them to itself. + # + # headers['X-Special-Domain-Specific-Header'] = "SecretValue" + # + # The resulting Mail::Message will have the following in it's header: + # + # X-Special-Domain-Specific-Header: SecretValue def headers(args=nil) if args ActiveSupport::Deprecation.warn "headers(Hash) is deprecated, please do headers[key] = value instead", caller[0,2] @@ -383,10 +363,91 @@ module ActionMailer #:nodoc: end end + # Allows you to add attachments to an email, like so: + # + # mail.attachments['filename.jpg'] = File.read('/path/to/filename.jpg') + # + # If you do this, then Mail will take the file name and work out the mime type + # set the Content-Type, Content-Disposition, Content-Transfer-Encoding and + # base64 encode the contents of the attachment all for you. + # + # You can also specify overrides if you want by passing a hash instead of a string: + # + # mail.attachments['filename.jpg'] = {:mime_type => 'application/x-gzip', + # :content => File.read('/path/to/filename.jpg')} + # + # If you want to use a different encoding than Base64, you can pass an encoding in, + # but then it is up to you to pass in the content pre-encoded, and don't expect + # Mail to know how to decode this data: + # + # file_content = SpecialEncode(File.read('/path/to/filename.jpg')) + # mail.attachments['filename.jpg'] = {:mime_type => 'application/x-gzip', + # :encoding => 'SpecialEncoding', + # :content => file_content } + # + # You can also search for specific attachments: + # + # # By Filename + # mail.attachments['filename.jpg'] #=> Mail::Part object or nil + # + # # or by index + # mail.attachments[0] #=> Mail::Part (first attachment) + # def attachments @_message.attachments end + # The main method that creates the message and renders the email templates. There are + # two ways to call this method, with a block, or without a block. + # + # Both methods accept a headers hash. This hash allows you to specify the most used headers + # in an email message, these are: + # + # * :subject - The subject of the message, if this is omitted, ActionMailer will + # ask the Rails I18n class for a translated :subject in the scope of + # [:actionmailer, mailer_scope, action_name] or if this is missing, will translate the + # humanized version of the action_name + # * :to - Who the message is destined for, can be a string of addresses, or an array + # of addresses. + # * :from - Who the message is from, if missing, will use the :delivers_from + # value in the class (if it exists) + # * :cc - Who you would like to Carbon-Copy on this email, can be a string of addresses, + # or an array of addresses. + # * :bcc - Who you would like to Blind-Carbon-Copy on this email, can be a string of + # addresses, or an array of addresses. + # * :reply_to - Who to set the Reply-To header of the email to. + # * :date - The date to say the email was sent on. + # + # If you need other headers not listed above, use the headers['name'] = value method. + # + # When a :return_path is specified, that value will be used as the 'envelope from' + # address for the Mail message. Setting this is useful when you want delivery notifications + # sent to a different address than the one in :from. Mail will actually use the + # :return_path in preference to the :sender in preference to the :from + # field for the 'envelope from' value. + # + # If you do not pass a block to the +mail+ method, it will find all templates in the + # template path that match the method name that it is being called from, it will then + # create parts for each of these templates intelligently, making educated guesses + # on correct content type and sequence, and return a fully prepared Mail::Message + # ready to call :deliver on to send. + # + # If you do pass a block, you can render specific templates of your choice: + # + # mail(:to => 'mikel@test.lindsaar.net') do |format| + # format.text + # format.html + # end + # + # You can even render text directly without using a template: + # + # mail(:to => 'mikel@test.lindsaar.net') do |format| + # format.text { render :text => "Hello Mikel!" } + # format.html { render :text => "

Hello Mikel!

" } + # end + # + # Which will render a multipart/alternate email with text/plain and + # text/html parts. def mail(headers={}, &block) # Guard flag to prevent both the old and the new API from firing # Should be removed when old API is removed -- cgit v1.2.3 From f14390091c40149dcd0982ada097be5bcbb37003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Tue, 26 Jan 2010 00:09:18 +1100 Subject: We don't support enriched yet --- actionmailer/lib/action_mailer/base.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 51a8c07a28..6c70ce8998 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -55,7 +55,6 @@ module ActionMailer #:nodoc: # # mail(:to => user.emai) do |format| # format.text - # format.enriched, {:content_type => 'text/rtf'} # format.html # end # -- cgit v1.2.3 From 6589976533b7a6850390ed5d6526ca719e56c5ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Tue, 26 Jan 2010 01:43:41 +0100 Subject: Remove old files, add some information to docs and improve test suite. --- actionmailer/lib/action_mailer/base.rb | 48 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 14 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 6c70ce8998..65d1ba47d9 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -27,9 +27,8 @@ module ActionMailer #:nodoc: # # def welcome(recipient) # @account = recipient - # mail { :to => recipient.email_address_with_name, - # :bcc => ["bcc@example.com", "Order Watcher "], - # :subject => "New account information" } + # mail(:to => recipient.email_address_with_name, + # :bcc => ["bcc@example.com", "Order Watcher "]) # end # end # @@ -37,13 +36,15 @@ module ActionMailer #:nodoc: # # * attachments[]= - Allows you to add attachments to your email in an intuitive # manner; attachments['filename.png'] = File.read('path/to/filename.png') + # # * headers[]= - Allows you to specify non standard headers in your email such # as headers['X-No-Spam'] = 'True' + # # * mail - Allows you to specify your email to send. # # The hash passed to the mail method allows you to specify the most used headers in an email # message, such as Subject, To, From, Cc, Bcc, - # Reply-To and Date. See the ActionMailer#mail method for more details. + # Reply-To and Date. See the ActionMailer#mail method for more details. # # If you need other headers not listed above, use the headers['name'] = value method. # @@ -58,6 +59,20 @@ module ActionMailer #:nodoc: # format.html # end # + # The block syntax is useful if also need to specify information specific to a part: + # + # mail(:to => user.emai) do |format| + # format.text(:content_transfer_encoding => "base64") + # format.html + # end + # + # Or even to renderize a special view: + # + # mail(:to => user.emai) do |format| + # format.text + # format.html { render "some_other_template" } + # end + # # = Mailer views # # Like Action Controller, each mailer class has a corresponding view directory in which each @@ -79,9 +94,9 @@ module ActionMailer #:nodoc: # You got a new note! # <%= truncate(@note.body, 25) %> # - # If you need to access the subject, from or the recipients in the view, you can do that through mailer object: + # If you need to access the subject, from or the recipients in the view, you can do that through message object: # - # You got a new note from <%= mailer.from %>! + # You got a new note from <%= message.from %>! # <%= truncate(@note.body, 25) %> # # @@ -137,7 +152,7 @@ module ActionMailer #:nodoc: # * signup_notification.text.plain.erb # * signup_notification.text.html.erb # * signup_notification.text.xml.builder - # * signup_notification.text.x-yaml.erb + # * signup_notification.text.yaml.erb # # Each would be rendered and added as a separate part to the message, with the corresponding content # type. The content type for the entire message is automatically set to multipart/alternative, @@ -174,8 +189,6 @@ module ActionMailer #:nodoc: # * delivers_from - Pass this the address that then defaults as the +from+ address on all the # emails sent. Can be overridden on a per mail basis by passing :from => 'another@address' in # the +mail+ method. - # - # * template_root - Determines the base from which template references will be made. # # * logger - the logger is used for generating information on the mailing run if available. # Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers. @@ -300,9 +313,7 @@ module ActionMailer #:nodoc: def deliver_mail(mail) #:nodoc: ActiveSupport::Notifications.instrument("action_mailer.deliver") do |payload| self.set_payload_for_mail(payload, mail) - yield # Let Mail do the delivery actions - end end @@ -399,7 +410,7 @@ module ActionMailer #:nodoc: # The main method that creates the message and renders the email templates. There are # two ways to call this method, with a block, or without a block. # - # Both methods accept a headers hash. This hash allows you to specify the most used headers + # Both methods accept a headers hash. This hash allows you to specify the most used headers # in an email message, these are: # # * :subject - The subject of the message, if this is omitted, ActionMailer will @@ -419,7 +430,7 @@ module ActionMailer #:nodoc: # # If you need other headers not listed above, use the headers['name'] = value method. # - # When a :return_path is specified, that value will be used as the 'envelope from' + # When a :return_path is specified as header, that value will be used as the 'envelope from' # address for the Mail message. Setting this is useful when you want delivery notifications # sent to a different address than the one in :from. Mail will actually use the # :return_path in preference to the :sender in preference to the :from @@ -447,6 +458,14 @@ module ActionMailer #:nodoc: # # Which will render a multipart/alternate email with text/plain and # text/html parts. + # + # The block syntax also allows you to customize the part headers if desired: + # + # mail(:to => 'mikel@test.lindsaar.net') do |format| + # format.text(:content_transfer_encoding => "base64") + # format.html + # end + # def mail(headers={}, &block) # Guard flag to prevent both the old and the new API from firing # Should be removed when old API is removed @@ -541,7 +560,8 @@ module ActionMailer #:nodoc: def create_parts_from_responses(m, responses, charset) #:nodoc: if responses.size == 1 && !m.has_attachments? - m.body = responses[0][:body] + headers = responses[0] + headers.each { |k,v| m[k] = v } return responses[0][:content_type] elsif responses.size > 1 && m.has_attachments? container = Mail::Part.new -- cgit v1.2.3 From 74a5889abef1212d373ea994f1c93daedee8932c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim=20and=20Mikel=20Lindsaar?= Date: Tue, 26 Jan 2010 11:49:32 +1100 Subject: Refactor content type setting, added tests to ensure boundary exists on multipart and fixed typo --- actionmailer/lib/action_mailer/base.rb | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'actionmailer/lib/action_mailer') diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 6c70ce8998..782e9d2c46 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -49,7 +49,7 @@ module ActionMailer #:nodoc: # # The mail method, if not passed a block, will inspect your views and send all the views with # the same name as the method, so the above action would send the +welcome.plain.erb+ view file - # as well as the +welcome.html.erb+ view file in a +multipart/alternate+ email. + # as well as the +welcome.html.erb+ view file in a +multipart/alternative+ email. # # If you want to explicitly render only certain templates, pass a block: # @@ -162,7 +162,7 @@ module ActionMailer #:nodoc: # # Which will (if it had both a .text.erb and .html.erb tempalte in the view # directory), send a complete multipart/mixed email with two parts, the first part being - # a multipart/alternate with the text and HTML email parts inside, and the second being + # a multipart/alternative with the text and HTML email parts inside, and the second being # a application/pdf with a Base64 encoded copy of the file.pdf book with the filename # +free_book.pdf+. # @@ -445,7 +445,7 @@ module ActionMailer #:nodoc: # format.html { render :text => "

Hello Mikel!

" } # end # - # Which will render a multipart/alternate email with text/plain and + # Which will render a multipart/alternative email with text/plain and # text/html parts. def mail(headers={}, &block) # Guard flag to prevent both the old and the new API from firing @@ -465,10 +465,11 @@ module ActionMailer #:nodoc: # Render the templates and blocks responses, sort_order = collect_responses_and_sort_order(headers, &block) - content_type ||= create_parts_from_responses(m, responses, charset) + + create_parts_from_responses(m, responses, charset) # Tidy up content type, charset, mime version and sort order - m.content_type = content_type + m.content_type = set_content_type(m, content_type) m.charset = charset m.mime_version = mime_version sort_order = headers[:parts_order] || sort_order || self.class.default_implicit_parts_order.dup @@ -485,6 +486,20 @@ module ActionMailer #:nodoc: protected + def set_content_type(m, user_content_type) + params = m.content_type_parameters || {} + case + when user_content_type.present? + user_content_type + when m.has_attachments? + ["multipart", "mixed", params] + when m.multipart? + ["multipart", "alternative", params] + else + self.class.default_content_type.dup + end + end + def default_subject #:nodoc: mailer_scope = self.class.mailer_name.gsub('/', '.') I18n.t(:subject, :scope => [:actionmailer, mailer_scope, action_name], :default => action_name.humanize) @@ -543,16 +558,14 @@ module ActionMailer #:nodoc: if responses.size == 1 && !m.has_attachments? m.body = responses[0][:body] return responses[0][:content_type] - elsif responses.size > 1 && m.has_attachments? + elsif responses.size > 1 && m.has_attachments? container = Mail::Part.new - container.content_type = "multipart/alternate" + container.content_type = "multipart/alternative" responses.each { |r| insert_part(container, r, charset) } m.add_part(container) else responses.each { |r| insert_part(m, r, charset) } end - - m.has_attachments? ? "multipart/mixed" : "multipart/alternate" end def insert_part(container, response, charset) #:nodoc: -- cgit v1.2.3