lib/flapjack/notifiers/mailer/mailer.rb in flapjack-0.4.11 vs lib/flapjack/notifiers/mailer/mailer.rb in flapjack-0.4.12
- old
+ new
@@ -1,31 +1,37 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'net/smtp'
-require 'mailfactory'
+require 'tmail'
+require 'log4r'
module Flapjack
module Notifiers
class Mailer
def initialize(opts={})
- @from_address = opts[:from_address]
+ if opts[:from_address]
+ @from_address = opts[:from_address]
+ else
+ raise ArgumentError, "from address must be provided"
+ end
@website_uri = opts[:website_uri] ? opts[:website_uri].gsub(/\/$/, '') : "http://#{`hostname`}"
@log = opts[:logger]
- @log ||= Log4r::Logger.new("notifier")
+ @log ||= ::Log4r::Logger.new("notifier")
end
def notify(opts={})
- raise unless (opts[:who] && opts[:result])
+ raise ArgumentError, "a recipient was not specified" unless opts[:who]
+ raise ArgumentError, "a result was not specified" unless opts[:result]
- mail = MailFactory.new
+ mail = TMail::Mail.new
mail.to = opts[:who].email
mail.from = @from_address
mail.subject = "Check: #{opts[:result].id}, Status: #{opts[:result].status}"
- mail.text = <<-DESC
+ mail.body = <<-DESC
Check #{opts[:result].id} returned the status "#{opts[:result].status}".
Here was the output:
#{opts[:result].output}
@@ -33,10 +39,10 @@
#{@website_uri}/issue/#{opts[:result].id}
DESC
begin
Net::SMTP.start('localhost') do |smtp|
- smtp.sendmail(mail.to_s, mail.from, mail.to)
+ return smtp.sendmail(mail.to_s, mail.from, mail.to)
end
rescue Errno::ECONNREFUSED
@log.error("Couldn't establish connection to mail server!")
end
end