Class: NotifyMe::Log::Mail

Inherits:
Logger
  • Object
show all
Defined in:
lib/notifyme/log/mail.rb

Constant Summary

Constants inherited from Logger

Logger::LOG_FREQUENCE

Instance Method Summary collapse

Methods inherited from Logger

#add_log_history, #can_log?, #clean_log_history!, #fact, #initialize

Constructor Details

This class inherits a constructor from NotifyMe::Log::Logger

Instance Method Details

#<<(task) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/notifyme/log/mail.rb', line 4

def <<(task)
  param = @parameters
  param = {} unless param.is_a?(Hash)

  # some default settings
  default_host = 'localhost'
  smtp_port = param[:port]
  unless smtp_port
    smtp_port = param[:tls] ? 587 : 25
  end
  smtp_host = (param[:address] || param[:host]) || default_host

  default_from_email = 'notifyme@' + smtp_host

  param[:subject] = "NotifyMe report (#{fact(:hostname)} # #{fact(:ipaddress)}): #{task.name}"
  param[:subject] = param[:subject] % 

  param[:from_email] ||= param[:account]
  param[:from_email] ||= default_from_email
  from = if param[:from_name]
           "#{param[:from_name]} <#{param[:from_email]}>"
         else
           param[:from_email]
         end

  param[:body] = param[:body_header].to_s + 
    generate(task) +
    param[:body_footer].to_s

  # go go go!
  recipients = if param[:to_email].is_a? Hash
                 param[:to_email].collect{|name, email| "#{name} <#{email}>"}
               else
                 param[:to_email]
               end

  mail = ::Mail.new do
    from from
    to recipients
    subject param[:subject]
    body param[:body]
  end

  if param[:host]
    smtp_settings = {
      :address => smtp_host,
      :port => param[:port] || smtp_port,
      :domain => param[:helo_domain] || default_host,
      :user_name => param[:account] || param[:user_name],
      :password => param[:password],
      :authentication => param[:authentication] || :plain,
      :openssl_verify_mode => param[:openssl_verify_mode],
      :enable_starttls_auto => param[:enable_starttls_auto] || true,
      :tls => param[:tls],
      :ssl => param[:ssl]
    }
    mail.delivery_method :smtp, smtp_settings
  else
    mail.delivery_method :sendmail
  end
  mail.deliver
end