delivery_methods.rb 2.8 KB
Newer Older
1 2
require 'tmpdir'

J
José Valim 已提交
3
module ActionMailer
4
  # This module handles everything related to mail delivery, from registering new
5
  # delivery methods to configuring the mail object to be sent.
J
José Valim 已提交
6
  module DeliveryMethods
7
    extend ActiveSupport::Concern
J
José Valim 已提交
8

9
    included do
J
Jeremy Kemper 已提交
10
      class_attribute :delivery_methods, :delivery_method
J
José Valim 已提交
11

12 13 14 15 16 17 18
      # Do not make this inheritable, because we always want it to propagate
      cattr_accessor :raise_delivery_errors
      self.raise_delivery_errors = true

      cattr_accessor :perform_deliveries
      self.perform_deliveries = true

J
Jeremy Kemper 已提交
19
      self.delivery_methods = {}.freeze
20
      self.delivery_method  = :smtp
J
José Valim 已提交
21

22 23 24 25 26 27 28 29
      add_delivery_method :smtp, Mail::SMTP,
        :address              => "localhost",
        :port                 => 25,
        :domain               => 'localhost.localdomain',
        :user_name            => nil,
        :password             => nil,
        :authentication       => nil,
        :enable_starttls_auto => true
J
José Valim 已提交
30

31 32
      add_delivery_method :file, Mail::FileDelivery,
        :location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"
J
José Valim 已提交
33

34 35 36
      add_delivery_method :sendmail, Mail::Sendmail,
        :location   => '/usr/sbin/sendmail',
        :arguments  => '-i -t'
J
José Valim 已提交
37

38
      add_delivery_method :test, Mail::TestMailer
39
    end
J
José Valim 已提交
40

41
    module ClassMethods
42 43
      # Provides a list of emails that have been delivered by Mail::TestMailer
      delegate :deliveries, :deliveries=, :to => Mail::TestMailer
44

45 46 47 48
      # Adds a new delivery method through the given class using the given symbol
      # as alias and the default options supplied:
      #
      # Example:
49
      #
50 51 52
      #   add_delivery_method :sendmail, Mail::Sendmail,
      #     :location   => '/usr/sbin/sendmail',
      #     :arguments  => '-i -t'
53
      #
54
      def add_delivery_method(symbol, klass, default_options={})
J
Jeremy Kemper 已提交
55
        class_attribute(:"#{symbol}_settings") unless respond_to?(:"#{symbol}_settings")
56
        send(:"#{symbol}_settings=", default_options)
J
Jeremy Kemper 已提交
57
        self.delivery_methods = delivery_methods.merge(symbol.to_sym => klass).freeze
58
      end
J
José Valim 已提交
59

60 61
      def wrap_delivery_behavior(mail, method=nil) #:nodoc:
        method ||= self.delivery_method
62
        mail.delivery_handler = self
63

64 65 66 67
        case method
        when NilClass
          raise "Delivery method cannot be nil"
        when Symbol
68 69 70 71 72
          if klass = delivery_methods[method.to_sym]
            mail.delivery_method(klass, send(:"#{method}_settings"))
          else
            raise "Invalid delivery method #{method.inspect}"
          end
J
José Valim 已提交
73
        else
74
          mail.delivery_method(method)
J
José Valim 已提交
75
        end
76 77 78

        mail.perform_deliveries    = perform_deliveries
        mail.raise_delivery_errors = raise_delivery_errors
J
José Valim 已提交
79
      end
80
    end
J
José Valim 已提交
81

82 83
    def wrap_delivery_behavior!(*args) #:nodoc:
      self.class.wrap_delivery_behavior(message, *args)
J
José Valim 已提交
84 85
    end
  end
J
Jeremy Kemper 已提交
86
end