1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionmailer/lib/action_mailer/delivery_methods.rb

83 lines
2.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "tmpdir"
module ActionMailer
# This module handles everything related to mail delivery, from registering
# new delivery methods to configuring the mail object to be sent.
2010-01-19 09:34:58 -05:00
module DeliveryMethods
extend ActiveSupport::Concern
2010-01-19 09:34:58 -05:00
included do
# Do not make this inheritable, because we always want it to propagate
cattr_accessor :raise_delivery_errors, default: true
cattr_accessor :perform_deliveries, default: true
cattr_accessor :deliver_later_queue_name, default: :mailers
class_attribute :delivery_methods, default: {}.freeze
class_attribute :delivery_method, default: :smtp
2010-01-19 09:34:58 -05:00
add_delivery_method :smtp, Mail::SMTP,
address: "localhost",
port: 25,
domain: "localhost.localdomain",
user_name: nil,
password: nil,
authentication: nil,
enable_starttls_auto: true
2010-01-19 09:34:58 -05:00
add_delivery_method :file, Mail::FileDelivery,
location: defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
2010-01-19 09:34:58 -05:00
add_delivery_method :sendmail, Mail::Sendmail,
location: "/usr/sbin/sendmail",
arguments: "-i"
2010-01-19 09:34:58 -05:00
add_delivery_method :test, Mail::TestMailer
2010-01-24 10:37:28 -05:00
end
2010-01-19 09:34:58 -05:00
# Helpers for creating and wrapping delivery behavior, used by DeliveryMethods.
module ClassMethods
# 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.
#
# add_delivery_method :sendmail, Mail::Sendmail,
# location: '/usr/sbin/sendmail',
# arguments: '-i'
def add_delivery_method(symbol, klass, default_options = {})
2010-01-31 21:32:28 -05:00
class_attribute(:"#{symbol}_settings") unless respond_to?(:"#{symbol}_settings")
public_send(:"#{symbol}_settings=", default_options)
2010-01-31 21:32:28 -05:00
self.delivery_methods = delivery_methods.merge(symbol.to_sym => klass).freeze
end
2010-01-19 09:34:58 -05:00
def wrap_delivery_behavior(mail, method = nil, options = nil) # :nodoc:
method ||= delivery_method
mail.delivery_handler = self
2010-01-24 10:37:28 -05:00
2010-01-26 05:46:42 -05:00
case method
when NilClass
raise "Delivery method cannot be nil"
when Symbol
if klass = delivery_methods[method]
mail.delivery_method(klass, (send(:"#{method}_settings") || {}).merge(options || {}))
else
raise "Invalid delivery method #{method.inspect}"
end
2010-01-19 09:34:58 -05:00
else
mail.delivery_method(method)
2010-01-19 09:34:58 -05:00
end
mail.perform_deliveries = perform_deliveries
mail.raise_delivery_errors = raise_delivery_errors
2010-01-19 09:34:58 -05:00
end
2010-01-24 10:37:28 -05:00
end
2010-01-19 09:34:58 -05:00
def wrap_delivery_behavior!(*args) # :nodoc:
self.class.wrap_delivery_behavior(message, *args)
2010-01-19 09:34:58 -05:00
end
end
2010-01-31 21:32:28 -05:00
end