From 12e5b923d9395d6c252ed95614246a92ad3083ba Mon Sep 17 00:00:00 2001 From: Tyler Vann-Campbell Date: Mon, 3 Feb 2014 17:21:52 -0800 Subject: [PATCH 1/2] Add SMTP support and use name vs. fqdn --- lib/chef/handler/mail.erb | 2 +- lib/chef/handler/mail.rb | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/chef/handler/mail.erb b/lib/chef/handler/mail.erb index 95c2607..48944de 100644 --- a/lib/chef/handler/mail.erb +++ b/lib/chef/handler/mail.erb @@ -1,4 +1,4 @@ -Results of <%= @status %> Chef run on <%= @run_status.node.fqdn %> +Results of <%= @status %> Chef run on <%= @run_status.node.name %> Start time: <%= @run_status.start_time %> End time: <%= @run_status.end_time %> diff --git a/lib/chef/handler/mail.rb b/lib/chef/handler/mail.rb index 07a6395..35577a7 100644 --- a/lib/chef/handler/mail.rb +++ b/lib/chef/handler/mail.rb @@ -17,20 +17,29 @@ require 'chef/handler' require 'erubis' require 'pony' +require 'yaml' class MailHandler < Chef::Handler attr_reader :options def initialize(opts = {}) @options = { :to_address => "root", - :template_path => File.join(File.dirname(__FILE__), "mail.erb") + :template_path => File.join(File.dirname(__FILE__), "mail.erb"), + :from_address => "chef-client", + :smtp_host => "localhost", + :smtp_port => '25', + :smtp_username => "chef-client", + :smtp_password => "", + :smtp_authentication => :plain, + :smtp_domain => 'localhost.localdomain' } @options.merge! opts + puts @options.to_yaml end def report status = success? ? "Successful" : "Failed" - subject = "#{status} Chef run on node #{node.fqdn}" + subject = "#{status} Chef run on node #{node.name}" Chef::Log.debug("mail handler template path: #{options[:template_path]}") if File.exists? options[:template_path] @@ -44,14 +53,23 @@ def report :status => status, :run_status => run_status } - + body = Erubis::Eruby.new(template).evaluate(context) Pony.mail( :to => options[:to_address], - :from => "chef-client@#{node.fqdn}", + :from => options[:from_address], :subject => subject, - :body => body + :body => body, + :via => :smtp, + :via_options => { + :address => options[:smtp_host], + :port => options[:smtp_port], + :user_name => options[:smtp_username], + :password => options[:smtp_password], + :authentiction => :login, + :enable_starttls_auto => true + } ) end end From dbccf23ada05428e770134b41272350717d0739d Mon Sep 17 00:00:00 2001 From: Tyler Vann-Campbell Date: Tue, 4 Feb 2014 11:19:15 -0800 Subject: [PATCH 2/2] remove yaml dependency --- lib/chef/handler/mail.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chef/handler/mail.rb b/lib/chef/handler/mail.rb index 35577a7..c5dad93 100644 --- a/lib/chef/handler/mail.rb +++ b/lib/chef/handler/mail.rb @@ -17,7 +17,6 @@ require 'chef/handler' require 'erubis' require 'pony' -require 'yaml' class MailHandler < Chef::Handler attr_reader :options @@ -67,7 +66,8 @@ def report :port => options[:smtp_port], :user_name => options[:smtp_username], :password => options[:smtp_password], - :authentiction => :login, + :authentication => options[:smtp_authentication], + :domain => options[:smtp_domain], :enable_starttls_auto => true } )