Class: Alert_Email

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) ⇒ Alert_Email

Returns a new instance of Alert_Email.



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 then
           @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
41
42
# 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 => e
   string = "*** Conection refused while attempting to connect to SMTP server\n"
   string = "#{string}*** Recipient, #{@destination}. Body,\n"
   string = "#{string}*** #{@body}\n"
   puts string
end