Class: God::Contacts::Email

Inherits:
God::Contact show all
Defined in:
lib/god/contacts/email.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from God::Contact

#group, #info, #name

Instance Method Summary collapse

Methods inherited from God::Contact

#friendly_name, generate, normalize, valid?

Methods included from God::Configurable

#base_name, complain, #complain, #friendly_name, #prepare, #reset

Class Attribute Details

.delivery_methodObject

Returns the value of attribute delivery_method.



9
10
11
# File 'lib/god/contacts/email.rb', line 9

def delivery_method
  @delivery_method
end

.formatObject

Returns the value of attribute format.



9
10
11
# File 'lib/god/contacts/email.rb', line 9

def format
  @format
end

.message_settingsObject

Returns the value of attribute message_settings.



9
10
11
# File 'lib/god/contacts/email.rb', line 9

def message_settings
  @message_settings
end

.sendmail_settingsObject

Returns the value of attribute sendmail_settings.



9
10
11
# File 'lib/god/contacts/email.rb', line 9

def sendmail_settings
  @sendmail_settings
end

.server_settingsObject

Returns the value of attribute server_settings.



9
10
11
# File 'lib/god/contacts/email.rb', line 9

def server_settings
  @server_settings
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



42
43
44
# File 'lib/god/contacts/email.rb', line 42

def email
  @email
end

Instance Method Details

#notify(message, time, priority, category, host) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/god/contacts/email.rb', line 50

def notify(message, time, priority, category, host)
  begin
    body = Email.format.call(self.name, self.email, message, time, priority, category, host)

    case Email.delivery_method
    when :smtp
      notify_smtp(body)
    when :sendmail
      notify_sendmail(body)
    else
      raise "unknown delivery method: #{Email.delivery_method}"
    end
    
    self.info = "sent email to #{self.email}"
  rescue => e
    applog(nil, :info, "failed to send email to #{self.email}: #{e.message}")
    applog(nil, :debug, e.backtrace.join("\n"))
  end
end

#notify_sendmail(mail) ⇒ Object



86
87
88
89
90
91
# File 'lib/god/contacts/email.rb', line 86

def notify_sendmail(mail)
  IO.popen("#{Email.sendmail_settings[:location]} #{Email.sendmail_settings[:arguments]}","w+") do |sm|
    sm.print(mail.gsub(/\r/, ''))
    sm.flush
  end
end

#notify_smtp(mail) ⇒ Object

private



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/god/contacts/email.rb', line 72

def notify_smtp(mail)
  args = [Email.server_settings[:address], Email.server_settings[:port]]
  if Email.server_settings[:authentication]
    args << Email.server_settings[:domain]
    args << Email.server_settings[:user_name]
    args << Email.server_settings[:password]
    args << Email.server_settings[:authentication] 
  end

  Net::SMTP.start(*args) do |smtp|
    smtp.send_message mail, Email.message_settings[:from], self.email
  end
end

#valid?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/god/contacts/email.rb', line 44

def valid?
  valid = true
  valid &= complain("Attribute 'email' must be specified", self) if self.email.nil?
  valid
end