2017-07-23 11:17:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:01:31 -04:00
|
|
|
require "tmpdir"
|
2010-01-24 12:11:57 -05:00
|
|
|
|
2013-05-19 11:36:06 -04:00
|
|
|
module ActionMailer
|
2012-09-17 20:18:56 -04:00
|
|
|
# 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
|
2010-01-24 12:11:57 -05:00
|
|
|
extend ActiveSupport::Concern
|
2010-01-19 09:34:58 -05:00
|
|
|
|
2010-01-24 12:11:57 -05:00
|
|
|
included do
|
2010-01-24 13:36:42 -05:00
|
|
|
# Do not make this inheritable, because we always want it to propagate
|
2017-05-31 05:16:20 -04:00
|
|
|
cattr_accessor :raise_delivery_errors, default: true
|
|
|
|
cattr_accessor :perform_deliveries, default: true
|
|
|
|
cattr_accessor :deliver_later_queue_name, default: :mailers
|
2015-01-18 17:34:01 -05:00
|
|
|
|
2017-05-29 12:01:50 -04:00
|
|
|
class_attribute :delivery_methods, default: {}.freeze
|
|
|
|
class_attribute :delivery_method, default: :smtp
|
2010-01-19 09:34:58 -05:00
|
|
|
|
2010-01-24 12:11:57 -05:00
|
|
|
add_delivery_method :smtp, Mail::SMTP,
|
2012-10-07 13:54:14 -04:00
|
|
|
address: "localhost",
|
|
|
|
port: 25,
|
2016-08-06 13:01:31 -04:00
|
|
|
domain: "localhost.localdomain",
|
2012-10-07 13:54:14 -04:00
|
|
|
user_name: nil,
|
|
|
|
password: nil,
|
|
|
|
authentication: nil,
|
|
|
|
enable_starttls_auto: true
|
2010-01-19 09:34:58 -05:00
|
|
|
|
2010-01-24 12:11:57 -05:00
|
|
|
add_delivery_method :file, Mail::FileDelivery,
|
2012-10-07 13:54:14 -04:00
|
|
|
location: defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
|
2010-01-19 09:34:58 -05:00
|
|
|
|
2010-01-24 12:11:57 -05:00
|
|
|
add_delivery_method :sendmail, Mail::Sendmail,
|
2016-08-06 13:01:31 -04:00
|
|
|
location: "/usr/sbin/sendmail",
|
|
|
|
arguments: "-i"
|
2010-01-19 09:34:58 -05:00
|
|
|
|
2010-01-24 12:11:57 -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
|
|
|
|
2013-05-15 00:19:38 -04:00
|
|
|
# Helpers for creating and wrapping delivery behavior, used by DeliveryMethods.
|
2010-01-24 12:11:57 -05:00
|
|
|
module ClassMethods
|
2010-01-25 05:47:03 -05:00
|
|
|
# Provides a list of emails that have been delivered by Mail::TestMailer
|
2012-10-07 13:54:14 -04:00
|
|
|
delegate :deliveries, :deliveries=, to: Mail::TestMailer
|
2010-01-24 13:36:42 -05:00
|
|
|
|
2012-09-17 20:18:56 -04:00
|
|
|
# Adds a new delivery method through the given class using the given
|
|
|
|
# symbol as alias and the default options supplied.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-24 12:11:57 -05:00
|
|
|
# add_delivery_method :sendmail, Mail::Sendmail,
|
2012-09-17 20:18:56 -04:00
|
|
|
# location: '/usr/sbin/sendmail',
|
2016-07-06 05:53:40 -04:00
|
|
|
# arguments: '-i'
|
2016-10-28 23:05:58 -04:00
|
|
|
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")
|
2010-01-24 12:11:57 -05:00
|
|
|
send(:"#{symbol}_settings=", default_options)
|
2010-01-31 21:32:28 -05:00
|
|
|
self.delivery_methods = delivery_methods.merge(symbol.to_sym => klass).freeze
|
2010-01-24 12:11:57 -05:00
|
|
|
end
|
2010-01-19 09:34:58 -05:00
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
def wrap_delivery_behavior(mail, method = nil, options = nil) # :nodoc:
|
2017-01-05 03:20:57 -05:00
|
|
|
method ||= delivery_method
|
2010-01-24 21:39:48 -05:00
|
|
|
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
|
2012-05-03 08:11:31 -04:00
|
|
|
if klass = delivery_methods[method]
|
2013-07-25 15:00:56 -04:00
|
|
|
mail.delivery_method(klass, (send(:"#{method}_settings") || {}).merge(options || {}))
|
2010-01-24 12:11:57 -05:00
|
|
|
else
|
|
|
|
raise "Invalid delivery method #{method.inspect}"
|
|
|
|
end
|
2010-01-19 09:34:58 -05:00
|
|
|
else
|
2010-01-24 12:11:57 -05:00
|
|
|
mail.delivery_method(method)
|
2010-01-19 09:34:58 -05:00
|
|
|
end
|
2010-01-24 12:11:57 -05:00
|
|
|
|
2012-08-07 12:59:31 -04:00
|
|
|
mail.perform_deliveries = perform_deliveries
|
2010-01-24 12:11:57 -05:00
|
|
|
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
|
|
|
|
2012-09-17 20:18:56 -04:00
|
|
|
def wrap_delivery_behavior!(*args) # :nodoc:
|
2010-01-24 12:11:57 -05:00
|
|
|
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
|