Class: EmailNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/notifiers/email_notifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ EmailNotifier

Returns a new instance of EmailNotifier.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/notifiers/email_notifier.rb', line 7

def initialize(opts={})
  @opts = opts
  @from = opts['from'] || "Your sentient shell <[email protected]>"
  @to = opts['to'] || "Puny human"
  @subject = opts['subject'] || "Job Complete"
  @server = opts['server'] || 'localhost'
  @port = opts['port'] || 25
  @username = opts['username'] || nil
  @password = opts['password'] || nil
  @method = opts['method'] || :plain
  @to_addresses = opts['to_address'].to_a
  @from_address = opts['from_address'] || ''
  @tls = opts['tls'] || false

end

Instance Method Details

#notify(message, title, data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/notifiers/email_notifier.rb', line 23

def notify(message, title, data)

  details = message.dup
  details << "\n" + (data.any? ? "Last #{RESULTS_SIZE} lines...\n\n#{data.last(50).join}" : "No output.")

  @to_addresses.each do |address|

    msg = <<MESSAGE_END
From: #{@from}
To: #{@to}
Subject: #{title}: #{@subject}

#{details}

MESSAGE_END

    smtp = Net::SMTP.new @server, @port
    smtp.enable_starttls if @tls
    smtp.start(@server, @username, @password, @method.to_sym) do 
      smtp.send_message(msg, @from_address, address)
    end
  end
end