Sha256: b3c9cc911ba4cfe330aae498669375211924e9ccbb0ea585ce00808f12310b4d

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

module ArMailerAWS
  class Mailer

    attr_accessor :email_class

    def initialize(options={})
      self.email_class = options[:email_class] || ArMailerAWS.email_class.constantize
    end

    def deliver!(mail)
      envelope_from, destinations, message = check_params(mail)

      destinations.each do |destination|
        self.email_class.create! mail: message, to: destination, from: envelope_from
      end
    end

    private

    def check_params(mail)
      envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
      if envelope_from.blank?
        raise ArgumentError.new('A sender (Return-Path, Sender or From) required to send a message')
      end

      destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
      if destinations.blank?
        raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message')
      end

      message ||= mail.encoded if mail.respond_to?(:encoded)
      if message.blank?
        raise ArgumentError.new('A encoded content is required to send a message')
      end

      [envelope_from, destinations, message]
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ar_mailer_aws-0.1.0 lib/ar_mailer_aws/mailer.rb
ar_mailer_aws-0.0.4 lib/ar_mailer_aws/mailer.rb
ar_mailer_aws-0.0.3 lib/ar_mailer_aws/mailer.rb
ar_mailer_aws-0.0.2 lib/ar_mailer_aws/mailer.rb
ar_mailer_aws-0.0.1 lib/ar_mailer_aws/mailer.rb