Class: EmailDirect::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/emaildirect/mailer.rb

Overview

Implements a Mailer class that can be used by Mail to send using the relay functionality

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Mailer

Returns a new instance of Mailer.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
# File 'lib/emaildirect/mailer.rb', line 6

def initialize(values = {})
  self.settings = { :category_id          => nil,
                    :options              => {},
                    :logger               => defined?(::Rails) && ::Rails.logger,
                    :log_level            => :debug
                  }.merge!(values)
  raise ArgumentError, 'Category ID is required' unless settings[:category_id]
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



15
16
17
# File 'lib/emaildirect/mailer.rb', line 15

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/emaildirect/mailer.rb', line 21

def deliver!(mail)
  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

  options = request_data mail

  # Can only send to a max of 50 addresses at a time
  destinations.each_slice(50).each do |destination_slice|
    options[:Recipients] = []
    destination_slice.each do |destination|
      options[:Recipients] <<  { :ToEmail => destination }
    end
    response = RelaySend::Email.new(settings[:category_id]).send(options)
    settings[:logger].send(settings[:log_level], "EmailDirect: #{response.inspect}") if settings[:logger]
  end
end

#new(*args) ⇒ Object



17
18
19
# File 'lib/emaildirect/mailer.rb', line 17

def new(*args)
  self
end