Class: AlertEmail

Inherits:
Object
  • Object
show all
Defined in:
lib/alert/email.rb

Overview

Email alert class Uses localhost for sending email - Probably need to change this in the future.

Instance Method Summary collapse

Constructor Details

#initialize(sender, destination, body) ⇒ AlertEmail

Returns a new instance of AlertEmail.



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

def initialize(sender, destination, body)
  @sender = sender
  @body = body

  @smtp_address = ENV['smtp_address'].nil? ? 'localhost' : ENV['smtp_address']
  @smtp_port = ENV['smtp_port'].nil? ? 25 : ENV['smtp_port']

  @destination = destination
  if destination.is_a? Array
    @destination_fmt = "<#{destination.join('>,<')}>"
  else
    @destination_fmt = destination
  end
end

Instance Method Details

#sendObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/alert/email.rb', line 22

def send
  message = <<MESSAGE_END
From: #{ENV['APP_NAME']} #{@sender}
To: #{@destination_fmt}
Subject: #{ENV['APP_NAME']} Alert

#{@body}
.
MESSAGE_END

Net::SMTP.start(@smtp_address,@smtp_port) do |smtp|
  smtp.send_message message, @sender, @destination
end

rescue Errno::ECONNREFUSED
  puts "*** Conection refused while attempting to connect to SMTP server\n" \
       "*** Recipient, #{@destination}. Body,\n" \
       "*** #{@body}\n"
end