Class: Jackal::Mail::Smtp

Inherits:
Callback
  • Object
show all
Defined in:
lib/jackal-mail/smtp.rb

Overview

Send mail via SMTP

Instance Method Summary collapse

Instance Method Details

#deliver(payload) ⇒ Object

Deliver mail notification via smtp based on payload contents

Parameters:

  • payload (Smash)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jackal-mail/smtp.rb', line 54

def deliver(payload)
  payload_conf = payload[:data][:notification_email]
  begin
    via_options = payload_conf.fetch(:via_options, config.fetch(:via_options, {}))
    via_options = Hash[
      via_options.map do |k,v|
        [k.to_sym, v]
      end
    ]
    args = {
      :to => [payload_conf[:destination]].flatten(1).map{|d| d[:email]},
      :from => payload_conf[:origin][:email],
      :subject => payload_conf[:subject],
      :via => :smtp,
      :via_options => via_options
    }
    if(payload_conf[:attachments])
      args[:attachments] = Hash[*payload_conf[:attachments].map{|k,v|[k.to_s,v]}.flatten(1)]
    end
    message_key = payload_conf[:html] ? :html_body : :body
    args[message_key] = payload_conf[:message]
    if(args[:via_options][:authentication])
      args[:via_options][:authentication] = args[:via_options][:authentication].to_s.to_sym
    end
    if(bcc = config.get(:bcc))
      args[:bcc] = bcc
    end
    result = Pony.mail(args)
    payload.set(:data, :mail, :result, result)
    debug "Pony delivery result: #{result.inspect}"
  rescue => e
    error "Delivery failed: #{e.class} - #{e}"
    debug "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
  end
end

#execute(message) ⇒ Object

Process message and send email

Parameters:

  • message (Carnivore::Message)


44
45
46
47
48
49
# File 'lib/jackal-mail/smtp.rb', line 44

def execute(message)
  failure_wrap(message) do |payload|
    deliver(payload)
    job_completed(:mail, payload, message)
  end
end

#setup(*_) ⇒ Object

Setup the callback



27
28
29
# File 'lib/jackal-mail/smtp.rb', line 27

def setup(*_)
  require 'pony'
end

#valid?(message) ⇒ Truthy, Falsey

Check validity of message

Parameters:

  • message (Carnivore::Message)

Returns:

  • (Truthy, Falsey)


35
36
37
38
39
# File 'lib/jackal-mail/smtp.rb', line 35

def valid?(message)
  super do |payload|
    payload.get(:data, :notification_email)
  end
end