lib/action_mailer/base.rb in actionmailer-1.3.6 vs lib/action_mailer/base.rb in actionmailer-2.0.0
- old
+ new
@@ -50,13 +50,13 @@
#
# = Mailer views
#
# Like ActionController, 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 <tt>.rhtml</tt> file with the same name as the method
+ # To define a template to be used with a mailing, create an <tt>.erb</tt> file with the same name as the method
# in your mailer model. For example, in the mailer defined above, the template at
- # <tt>app/views/notifier/signup_notification.rhtml</tt> would be used to generate the email.
+ # <tt>app/views/notifier/signup_notification.erb</tt> would be used to generate the email.
#
# Variables defined in the model are accessible as instance variables in the view.
#
# Emails by default are sent in plain text, so a sample view for our model example might look like this:
#
@@ -101,11 +101,11 @@
# delivered by invoking <tt>Notifier.deliver_signup_notification</tt>.
#
#
# = HTML email
#
- # To send mail as HTML, make sure your view (the <tt>.rhtml</tt> file) generates HTML and
+ # To send mail as HTML, make sure your view (the <tt>.erb</tt> file) generates HTML and
# set the content type to html.
#
# class MyMailer < ActionMailer::Base
# def signup_notification(recipient)
# recipients recipient.email_address_with_name
@@ -140,14 +140,14 @@
# Multipart messages can also be used implicitly because ActionMailer will automatically
# detect and use multipart templates, where each template is named after the name of the action, followed
# by the content type. Each such detected template will be added as separate part to the message.
#
# For example, if the following templates existed:
- # * signup_notification.text.plain.rhtml
- # * signup_notification.text.html.rhtml
- # * signup_notification.text.xml.rxml
- # * signup_notification.text.x-yaml.rhtml
+ # * signup_notification.text.plain.erb
+ # * signup_notification.text.html.erb
+ # * 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 same body hash is passed to
# each template.
#
@@ -217,21 +217,20 @@
# ["text/html", "text/enriched", "text/plain"]. Items that appear first in the array have higher priority in the mail client
# and appear last in the mime encoded message. You can also pick a different order from inside a method with
# <tt>@implicit_parts_order</tt>.
class Base
include AdvAttrAccessor, PartContainer
- include ActionController::UrlWriter
+ include ActionController::UrlWriter if Object.const_defined?(:ActionController)
- # Action Mailer subclasses should be reloaded by the dispatcher in Rails
- # when Dependencies.mechanism = :load.
- include Reloadable::Deprecated
-
private_class_method :new #:nodoc:
class_inheritable_accessor :template_root
cattr_accessor :logger
+ cattr_accessor :template_extensions
+ @@template_extensions = ['erb', 'builder', 'rhtml', 'rxml']
+
@@smtp_settings = {
:address => "localhost",
:port => 25,
:domain => 'localhost.localdomain',
:user_name => nil,
@@ -247,12 +246,12 @@
cattr_accessor :sendmail_settings
@@raise_delivery_errors = true
cattr_accessor :raise_delivery_errors
- @@delivery_method = :smtp
- cattr_accessor :delivery_method
+ superclass_delegating_accessor :delivery_method
+ self.delivery_method = :smtp
@@perform_deliveries = true
cattr_accessor :perform_deliveries
@@deliveries = []
@@ -297,15 +296,10 @@
# 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
- # 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
-
# 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).
@@ -321,14 +315,39 @@
# 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.
+ def mailer_name(value = nil)
+ if value
+ self.mailer_name = value
+ else
+ self.class.mailer_name
+ end
+ end
+
+ def mailer_name=(value)
+ self.class.mailer_name = value
+ end
+
# The mail object instance referenced by this mailer.
attr_reader :mail
class << self
+ attr_writer :mailer_name
+
+ def mailer_name
+ @mailer_name ||= name.underscore
+ end
+
+ # for ActionView compatibility
+ alias_method :controller_name, :mailer_name
+ alias_method :controller_path, :mailer_name
+
def method_missing(method_symbol, *parameters)#:nodoc:
case method_symbol.id2name
when /^create_([_a-z]\w*)/ then new($1, *parameters).mail
when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver!
when "new" then nil
@@ -361,22 +380,21 @@
# email.set_some_obscure_header "frobnicate"
# MyMailer.deliver(email)
def deliver(mail)
new.deliver!(mail)
end
-
- # Server Settings is the old name for <tt>smtp_settings</tt>
- def server_settings
- smtp_settings
+
+ # Register a template extension so mailer templates written in a
+ # templating language other than rhtml or rxml are supported.
+ # To use this, include in your template-language plugin's init
+ # code or on a per-application basis, this can be invoked from
+ # config/environment.rb:
+ #
+ # ActionMailer::Base.register_template_extension('haml')
+ def register_template_extension(extension)
+ template_extensions << extension
end
- deprecate :server_settings=>"It's now named smtp_settings"
-
- def server_settings=(settings)
- ActiveSupport::Deprecation.warn("server_settings has been renamed smtp_settings, this warning will be removed with rails 2.0", caller)
- self.smtp_settings=settings
- 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"
@@ -387,24 +405,24 @@
# Initialize the mailer via the given +method_name+. The body will be
# rendered and a new TMail::Mail object created.
def create!(method_name, *parameters) #:nodoc:
initialize_defaults(method_name)
- send(method_name, *parameters)
+ __send__(method_name, *parameters)
# If an explicit, textual body has not been set, we check assumptions.
unless String === @body
# First, we look to see if there are any likely templates that match,
# which include the content-type in their file name (i.e.,
- # "the_template_file.text.html.rhtml", etc.). Only do this if parts
+ # "the_template_file.text.html.erb", etc.). Only do this if parts
# have not already been specified manually.
if @parts.empty?
templates = Dir.glob("#{template_path}/#{@template}.*")
templates.each do |path|
- # TODO: don't hardcode rhtml|rxml
basename = File.basename(path)
- next unless md = /^([^\.]+)\.([^\.]+\.[^\.]+)\.(rhtml|rxml)$/.match(basename)
+ template_regex = Regexp.new("^([^\\\.]+)\\\.([^\\\.]+\\\.[^\\\.]+)\\\.(" + template_extensions.join('|') + ")$")
+ next unless md = template_regex.match(basename)
template_name = basename
content_type = md.captures[1].gsub('.', '/')
@parts << Part.new(:content_type => content_type,
:disposition => "inline", :charset => charset,
:body => render_message(template_name, @body))
@@ -446,11 +464,11 @@
def deliver!(mail = @mail)
raise "no mail object available for delivery!" unless mail
logger.info "Sent mail:\n #{mail.encoded}" unless logger.nil?
begin
- send("perform_delivery_#{delivery_method}", mail) if perform_deliveries
+ __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
rescue Exception => e # Net::SMTP errors or sendmail pipe errors
raise e if raise_delivery_errors
end
return mail
@@ -476,18 +494,21 @@
render :file => method_name, :body => body
end
def render(opts)
body = opts.delete(:body)
+ if opts[:file] && opts[:file] !~ /\//
+ opts[:file] = "#{mailer_name}/#{opts[:file]}"
+ end
initialize_template_class(body).render(opts)
end
def template_path
"#{template_root}/#{mailer_name}"
end
def initialize_template_class(assigns)
- ActionView::Base.new(template_path, assigns, self)
+ ActionView::Base.new([template_root], assigns, self)
end
def sort_parts(parts, order = [])
order = order.collect { |s| s.downcase }