Class: WatcherAction::SendMail

Inherits:
Object
  • Object
show all
Defined in:
lib/watcher_action/send_mail.rb

Overview

This action will send an email to a given receipient. It doesn’t support any fancy features (you may want to do that handling externally).

Options

to

Email address to which to send the message. May be a list. (required)

sender

Email address of the person sending the mail (required)

subject

Subject of the message. You can put %s for the event message. (defaults if not set)

body

Body of the email message. If set to ‘xml’, it will include an XML representation of the event. If not set, it will default to a sensible description of the event. You can include the event’s message as for the subject

server

Address or name of the mail server to use (defaults to localhost)

port

Port to connect to (default: 25)

user

Mail server user name

pass

Mail server password

authentication

Authentication method (default: plain)

enable_tls

Use the TLS encryption (default: false)

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SendMail

Returns a new instance of SendMail.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/watcher_action/send_mail.rb', line 28

def initialize(config)
  @mail_to = config.get_value(:to, false)
  @sender = config.get_value(:sender, false)
  @subject = config.get_value(:subject, "Watchdogger needs your attention.")
  @body = config.get_value(:body)
  @server = config.get_value(:server, 'localhost')
  @port = config.get_value(:port, '25')
  @user = config.get_value(:user)
  @pass = config.get_value(:pass)
  @authentication = config.get_value(:authentication, :plain).to_sym
  @enable_tls = config.get_value(:enable_tls) || false
end

Instance Method Details

#execute(event) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/watcher_action/send_mail.rb', line 41

def execute(event)
  msg = RMail::Message.new
  msg.header.to = @mail_to
  receipient = msg.header.to.to_s.split(',').first
  msg.header.from = @sender
  msg.header.subject =  @subject % [event.message]
  msg.header.date = Time.now
  if(@body.to_s == 'xml')
    msg.body = event.to_xml
  elsif(@body)
    msg.body = @body % [event.message]
  else
    msg.body = "The #{event.watcher.class.name} watcher of your watchdog triggered\nan event at #{event.timestamp}:\n#{event.message}"
  end

  smtp_params = [@server, @port]
  if(@user && @pass)
    smtp_params.concat([nil, @user, @pass, @authentication])
  end
  
  Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE) if(@enable_tls)
  Thread.new(smtp_params, msg, @sender, @mail_to) do |params, msg, sender, mail_to|
      begin
      Net::SMTP.start(*params) do |smtp|
        smtp.send_message(msg.to_s, sender, mail_to)
      end
      dog_log.debug('SMTP Thread') { "Sent mail to #{mail_to} through #{params.first}" }
    rescue Exception => e
      dog_log.error('SMTP Thread') { "Could not send mail to #{mail_to} on #{params.first}: #{e.message}" }
    end
  end
rescue Exception => e
  dog_log.error('SMTP Action') { "Could not send mail to #{@mail_to} on #{@server}: #{e.message}" }
end