aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/lib/action_mailer
diff options
context:
space:
mode:
Diffstat (limited to 'actionmailer/lib/action_mailer')
-rw-r--r--actionmailer/lib/action_mailer/base.rb73
-rw-r--r--actionmailer/lib/action_mailer/delivery_method.rb56
-rw-r--r--actionmailer/lib/action_mailer/delivery_method/file.rb21
-rw-r--r--actionmailer/lib/action_mailer/delivery_method/sendmail.rb22
-rw-r--r--actionmailer/lib/action_mailer/delivery_method/smtp.rb30
-rw-r--r--actionmailer/lib/action_mailer/delivery_method/test.rb12
-rw-r--r--actionmailer/lib/action_mailer/delivery_methods.rb71
-rw-r--r--actionmailer/lib/action_mailer/deprecated_body.rb10
8 files changed, 106 insertions, 189 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index 356861b591..e105beef63 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
@@ -355,20 +357,13 @@ 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 delivery_method=(method_name)
- @delivery_method = ActionMailer::DeliveryMethod.lookup_method(method_name)
- end
+ alias :controller_path :mailer_name
def respond_to?(method_symbol, include_private = false) #:nodoc:
matches_dynamic_method?(method_symbol) || super
@@ -413,7 +408,27 @@ 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
+
+ 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
@@ -444,11 +459,6 @@ module ActionMailer #:nodoc:
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.
@@ -478,20 +488,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"
@@ -522,21 +518,7 @@ module ActionMailer #:nodoc:
# object (from the <tt>create!</tt> method). If no cached mail object exists, and
# no alternate has been given as the parameter, this will fail.
def deliver!(mail = @mail)
- raise "no mail object available for delivery!" unless mail
-
- ActiveSupport::Notifications.instrument("action_mailer.deliver",
- :template => template, :mailer => self.class.name) do |payload|
-
- self.class.set_payload_for_mail(payload, mail)
-
- begin
- self.delivery_method.perform_delivery(mail) if perform_deliveries
- rescue Exception => e # Net::SMTP errors or sendmail pipe errors
- raise e if raise_delivery_errors
- end
- end
-
- mail
+ self.class.deliver(mail)
end
private
@@ -549,6 +531,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
@@ -568,6 +552,7 @@ 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
@template ||= method_name
@parts ||= []
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
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