aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailer/lib/action_mailer/delivery_methods.rb
blob: c8c4148353c4fb7bbb37bc8fd79b12d73a598d1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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