From f5b8392cb8588988da357d26c9fd22c90030e09d Mon Sep 17 00:00:00 2001 From: Aaron Blythe Date: Mon, 29 Oct 2012 12:18:01 -0500 Subject: [PATCH] Allow Flexibility of setting from_address and filtering to successful/failed mail --- lib/chef/handler/mail.rb | 42 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/chef/handler/mail.rb b/lib/chef/handler/mail.rb index 07a6395..bdd455e 100644 --- a/lib/chef/handler/mail.rb +++ b/lib/chef/handler/mail.rb @@ -23,6 +23,7 @@ class MailHandler < Chef::Handler def initialize(opts = {}) @options = { :to_address => "root", + :send_statuses => ["Successful", "Failed"], :template_path => File.join(File.dirname(__FILE__), "mail.erb") } @options.merge! opts @@ -30,28 +31,31 @@ def initialize(opts = {}) def report status = success? ? "Successful" : "Failed" - subject = "#{status} Chef run on node #{node.fqdn}" + if options[:send_statuses].include? status + subject = "#{status} Chef run on node #{node.fqdn}" + options[:from_address] ? from_address = options[:from_address] : from_address = "chef-client@#{node.fqdn}" - Chef::Log.debug("mail handler template path: #{options[:template_path]}") - if File.exists? options[:template_path] - template = IO.read(options[:template_path]).chomp - else - Chef::Log.error("mail handler template not found: #{options[:template_path]}") - raise Errno::ENOENT - end + Chef::Log.debug("mail handler template path: #{options[:template_path]}") + if File.exists? options[:template_path] + template = IO.read(options[:template_path]).chomp + else + Chef::Log.error("mail handler template not found: #{options[:template_path]}") + raise Errno::ENOENT + end - context = { - :status => status, - :run_status => run_status - } + context = { + :status => status, + :run_status => run_status + } - body = Erubis::Eruby.new(template).evaluate(context) + body = Erubis::Eruby.new(template).evaluate(context) - Pony.mail( - :to => options[:to_address], - :from => "chef-client@#{node.fqdn}", - :subject => subject, - :body => body - ) + Pony.mail( + :to => options[:to_address], + :from => from_address, + :subject => subject, + :body => body + ) + end end end