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

#arg, defaults, #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.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def delivery_method
  @delivery_method
end

.formatObject

Returns the value of attribute format.



38
39
40
# File 'lib/god/contacts/email.rb', line 38

def format
  @format
end

.from_emailObject

Returns the value of attribute from_email.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def from_email
  @from_email
end

.from_nameObject

Returns the value of attribute from_name.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def from_name
  @from_name
end

.sendmail_argsObject

Returns the value of attribute sendmail_args.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def sendmail_args
  @sendmail_args
end

.sendmail_pathObject

Returns the value of attribute sendmail_path.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def sendmail_path
  @sendmail_path
end

.server_authObject

Returns the value of attribute server_auth.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def server_auth
  @server_auth
end

.server_domainObject

Returns the value of attribute server_domain.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def server_domain
  @server_domain
end

.server_hostObject

Returns the value of attribute server_host.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def server_host
  @server_host
end

.server_passwordObject

Returns the value of attribute server_password.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def server_password
  @server_password
end

.server_portObject

Returns the value of attribute server_port.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def server_port
  @server_port
end

.server_userObject

Returns the value of attribute server_user.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def server_user
  @server_user
end

.to_emailObject

Returns the value of attribute to_email.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def to_email
  @to_email
end

.to_nameObject

Returns the value of attribute to_name.



34
35
36
# File 'lib/god/contacts/email.rb', line 34

def to_name
  @to_name
end

Instance Attribute Details

#delivery_methodObject

Returns the value of attribute delivery_method.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def delivery_method
  @delivery_method
end

#from_emailObject

Returns the value of attribute from_email.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def from_email
  @from_email
end

#from_nameObject

Returns the value of attribute from_name.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def from_name
  @from_name
end

#sendmail_argsObject

Returns the value of attribute sendmail_args.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def sendmail_args
  @sendmail_args
end

#sendmail_pathObject

Returns the value of attribute sendmail_path.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def sendmail_path
  @sendmail_path
end

#server_authObject

Returns the value of attribute server_auth.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def server_auth
  @server_auth
end

#server_domainObject

Returns the value of attribute server_domain.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def server_domain
  @server_domain
end

#server_hostObject

Returns the value of attribute server_host.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def server_host
  @server_host
end

#server_passwordObject

Returns the value of attribute server_password.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def server_password
  @server_password
end

#server_portObject

Returns the value of attribute server_port.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def server_port
  @server_port
end

#server_userObject

Returns the value of attribute server_user.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def server_user
  @server_user
end

#to_emailObject

Returns the value of attribute to_email.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def to_email
  @to_email
end

#to_nameObject

Returns the value of attribute to_name.



65
66
67
# File 'lib/god/contacts/email.rb', line 65

def to_name
  @to_name
end

Instance Method Details

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/god/contacts/email.rb', line 86

def notify(message, time, priority, category, host)
  body = Email.format.call(self.name, arg(:from_email), arg(:from_name),
                           arg(:to_email), arg(:to_name), message, time,
                           priority, category, host)

  case arg(:delivery_method)
    when :smtp
      notify_smtp(body)
    when :sendmail
      notify_sendmail(body)
  end

  self.info = "sent email to #{arg(:to_email)} via #{arg(:delivery_method).to_s}"
rescue Object => e
  applog(nil, :info, "failed to send email to #{arg(:to_email)} via #{arg(:delivery_method).to_s}: #{e.message}")
  applog(nil, :debug, e.backtrace.join("\n"))
end

#notify_sendmail(mail) ⇒ Object



118
119
120
121
122
123
# File 'lib/god/contacts/email.rb', line 118

def notify_sendmail(mail)
  IO.popen("#{arg(:sendmail_path)} #{arg(:sendmail_args)}","w+") do |sm|
    sm.print(mail.gsub(/\r/, ''))
    sm.flush
  end
end

#notify_smtp(mail) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/god/contacts/email.rb', line 104

def notify_smtp(mail)
  args = [arg(:server_host), arg(:server_port)]
  if arg(:server_auth)
    args << arg(:server_domain)
    args << arg(:server_user)
    args << arg(:server_password)
    args << arg(:server_auth)
  end

  Net::SMTP.start(*args) do |smtp|
    smtp.send_message(mail, arg(:from_email), arg(:to_email))
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  valid = true
  valid &= complain("Attribute 'to_email' must be specified", self) unless arg(:to_email)
  valid &= complain("Attribute 'delivery_method' must be one of [ :smtp | :sendmail ]", self) unless [:smtp, :sendmail].include?(arg(:delivery_method))
  if arg(:delivery_method) == :smtp
    valid &= complain("Attribute 'server_host' must be specified", self) unless arg(:server_host)
    valid &= complain("Attribute 'server_port' must be specified", self) unless arg(:server_port)
    if arg(:server_auth)
      valid &= complain("Attribute 'server_domain' must be specified", self) unless arg(:server_domain)
      valid &= complain("Attribute 'server_user' must be specified", self) unless arg(:server_user)
      valid &= complain("Attribute 'server_password' must be specified", self) unless arg(:server_password)
    end
  end
  valid
end