Class: Maildis::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/maildis/dispatcher.rb

Class Method Summary collapse

Class Method Details

.dispatch(options = {}) ⇒ Object



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
# File 'lib/maildis/dispatcher.rb', line 10

def dispatch(options = {})
  result = {sent: [], not_sent: []}
  init_logger options[:logger]

  options[:recipients].each do |recipient|
    begin
      Pony.mail({
        to: recipient.to_email,
        from: options[:sender].to_email,
        subject: options[:subject],
        html_body: TemplateRenderer.render(options[:templates][:html], recipient.merge_fields),
        body: TemplateRenderer.render(options[:templates][:plain], recipient.merge_fields),
        via: :smtp,
        via_options: {address: options[:server].host,
                      port: options[:server].port,
                      user_name: options[:server].username,
                      password: options[:server].password}
      })
      info "Sent: #{recipient.to_email}"
      result[:sent] << recipient
    rescue => e
      error "Error: #{recipient.to_email} | #{e.message}"
      result[:not_sent] << {recipient: recipient, reason: e.message}
    end
  end
  result
end

.error(msg) ⇒ Object



40
# File 'lib/maildis/dispatcher.rb', line 40

def error(msg); @@logger.error msg if @@logger; end

.info(msg) ⇒ Object



39
# File 'lib/maildis/dispatcher.rb', line 39

def info(msg); @@logger.info msg if @@logger; end

.init_logger(logger = nil) ⇒ Object



38
# File 'lib/maildis/dispatcher.rb', line 38

def init_logger(logger = nil); @@logger ||= logger; end