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


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

def delivery_method
  @delivery_method
end

.formatObject

Returns the value of attribute format


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

def format
  @format
end

.from_emailObject

Returns the value of attribute from_email


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

def from_email
  @from_email
end

.from_nameObject

Returns the value of attribute from_name


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

def from_name
  @from_name
end

.sendmail_argsObject

Returns the value of attribute sendmail_args


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

def sendmail_args
  @sendmail_args
end

.sendmail_pathObject

Returns the value of attribute sendmail_path


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

def sendmail_path
  @sendmail_path
end

.server_authObject

Returns the value of attribute server_auth


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

def server_auth
  @server_auth
end

.server_domainObject

Returns the value of attribute server_domain


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

def server_domain
  @server_domain
end

.server_hostObject

Returns the value of attribute server_host


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

def server_host
  @server_host
end

.server_passwordObject

Returns the value of attribute server_password


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

def server_password
  @server_password
end

.server_portObject

Returns the value of attribute server_port


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

def server_port
  @server_port
end

.server_userObject

Returns the value of attribute server_user


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

def server_user
  @server_user
end

.to_emailObject

Returns the value of attribute to_email


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

def to_email
  @to_email
end

.to_nameObject

Returns the value of attribute to_name


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

def to_name
  @to_name
end

Instance Attribute Details

#delivery_methodObject

Returns the value of attribute delivery_method


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def delivery_method
  @delivery_method
end

#from_emailObject

Returns the value of attribute from_email


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def from_email
  @from_email
end

#from_nameObject

Returns the value of attribute from_name


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def from_name
  @from_name
end

#sendmail_argsObject

Returns the value of attribute sendmail_args


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def sendmail_args
  @sendmail_args
end

#sendmail_pathObject

Returns the value of attribute sendmail_path


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def sendmail_path
  @sendmail_path
end

#server_authObject

Returns the value of attribute server_auth


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def server_auth
  @server_auth
end

#server_domainObject

Returns the value of attribute server_domain


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def server_domain
  @server_domain
end

#server_hostObject

Returns the value of attribute server_host


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def server_host
  @server_host
end

#server_passwordObject

Returns the value of attribute server_password


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def server_password
  @server_password
end

#server_portObject

Returns the value of attribute server_port


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def server_port
  @server_port
end

#server_userObject

Returns the value of attribute server_user


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def server_user
  @server_user
end

#to_emailObject

Returns the value of attribute to_email


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def to_email
  @to_email
end

#to_nameObject

Returns the value of attribute to_name


68
69
70
# File 'lib/god/contacts/email.rb', line 68

def to_name
  @to_name
end

Instance Method Details

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


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

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


121
122
123
124
125
126
# File 'lib/god/contacts/email.rb', line 121

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


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/god/contacts/email.rb', line 107

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


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

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