Skip to content

Commit

Permalink
Convert Email contact to new style.
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Jun 30, 2010
1 parent 8ec3ff0 commit 1f31ef7
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 81 deletions.
128 changes: 80 additions & 48 deletions lib/god/contacts/email.rb
@@ -1,90 +1,122 @@
# Send a notice to an email address.
#
# to_email - The String email address to which the email will be sent.
# to_name - The String name corresponding to the recipient.
# from_email - The String email address from which the email will be sent.
# from_name - The String name corresponding to the sender.
# delivery_method - The Symbol delivery method. [ :smtp | :sendmail ]
# (default: :smtp).
#
# === SMTP Options (when delivery_method = :smtp) ===
# server_host - The String hostname of the SMTP server (default: localhost).
# server_port - The Integer port of the SMTP server (default: 25).
# server_auth - The Boolean of whether or not to use authentication
# (default: false).
#
# === SMTP Auth Options (when server_auth = true) ===
# server_domain - The String domain.
# server_user - The String username.
# server_password - The String password.
#
# === Sendmail Options (when delivery_method = :sendmail) ===
# sendmail_path - The String path to the sendmail executable
# (default: "/usr/sbin/sendmail").
# sendmail_args - The String args to send to sendmail (default "-i -t").

require 'time'
require 'net/smtp'

module God
module Contacts

class Email < Contact
class << self
attr_accessor :message_settings, :delivery_method, :server_settings, :sendmail_settings, :format
attr_accessor :to_email, :to_name, :from_email, :from_name,
:delivery_method, :server_host, :server_port,
:server_auth, :server_domain, :server_user,
:server_password, :sendmail_path, :sendmail_args
attr_accessor :format
end

self.message_settings = {:from => 'god@example.com'}

self.delivery_method = :smtp # or :sendmail

self.server_settings = {:address => 'localhost',
:port => 25}
# :domain
# :user_name
# :password
# :authentication

self.sendmail_settings = {:location => '/usr/sbin/sendmail',
:arguments => '-i -t'
}

self.format = lambda do |name, email, message, time, priority, category, host|
self.from_email = 'god@example.com'
self.from_name = 'God Process Monitoring'
self.delivery_method = :smtp
self.server_auth = false
self.server_host = 'localhost'
self.server_port = 25
self.sendmail_path = '/usr/sbin/sendmail'
self.sendmail_args = '-i -t'

self.format = lambda do |name, from_email, from_name, to_email, to_name, message, time, priority, category, host|
<<-EOF
From: god <#{self.message_settings[:from]}>
To: #{name} <#{email}>
From: #{from_name} <#{from_email}>
To: #{to_name || name} <#{to_email}>
Subject: [god] #{message}
Date: #{Time.now.httpdate}
Message-Id: <#{rand(1000000000).to_s(36)}.#{$$}.#{self.message_settings[:from]}>
Date: #{time.httpdate}
Message-Id: <#{rand(1000000000).to_s(36)}.#{$$}.#{from_email}>
Message: #{message}
Host: #{host}
Priority: #{priority}
Category: #{category}
EOF
end

attr_accessor :email


attr_accessor :to_email, :to_name, :from_email, :from_name,
:delivery_method, :server_host, :server_port,
:server_auth, :server_domain, :server_user,
:server_password, :sendmail_path, :sendmail_args

def valid?
valid = true
valid &= complain("Attribute 'email' must be specified", self) if self.email.nil?
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

def notify(message, time, priority, category, host)
begin
body = Email.format.call(self.name, self.email, 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 Email.delivery_method
case arg(: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

# private
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

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]
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, Email.message_settings[:from], self.email
smtp.send_message(mail, arg(:from_email), arg(:to_email))
end
end

def notify_sendmail(mail)
IO.popen("#{Email.sendmail_settings[:location]} #{Email.sendmail_settings[:arguments]}","w+") do |sm|
IO.popen("#{arg(:sendmail_path)} #{arg(:sendmail_args)}","w+") do |sm|
sm.print(mail.gsub(/\r/, ''))
sm.flush
end
Expand Down
55 changes: 22 additions & 33 deletions test/test_email.rb
@@ -1,45 +1,34 @@
require File.dirname(__FILE__) + '/helper'

class TestEmail < Test::Unit::TestCase
def test_exists
God::Contacts::Email
def setup
God::Contacts::Email.to_email = 'dev@example.com'
God::Contacts::Email.from_email = 'god@example.com'
@email = God::Contacts::Email.new
end

def test_unknown_delivery_method_for_notify
assert_nothing_raised do
God::Contacts::Email.any_instance.expects(:notify_smtp).never
God::Contacts::Email.any_instance.expects(:notify_sendmail).never
God::Contacts::Email.delivery_method = :foo_protocol
LOG.expects(:log).times(2)

g = God::Contacts::Email.new
g.notify(:a, :b, :c, :d, :e)
assert_nil g.info
end
def test_validity_delivery
@email.delivery_method = :brainwaves
assert_equal false, @email.valid?
end

def test_smtp_delivery_method_for_notify
assert_nothing_raised do
God::Contacts::Email.any_instance.expects(:notify_sendmail).never
God::Contacts::Email.any_instance.expects(:notify_smtp).once.returns(nil)
God::Contacts::Email.delivery_method = :smtp
g = God::Contacts::Email.new
g.email = 'joe@example.com'
g.notify(:a, :b, :c, :d, :e)
assert_equal "sent email to joe@example.com", g.info
end
@email.delivery_method = :smtp

God::Contacts::Email.any_instance.expects(:notify_sendmail).never
God::Contacts::Email.any_instance.expects(:notify_smtp).once.returns(nil)

@email.notify('msg', Time.now, 'prio', 'cat', 'host')
assert_equal "sent email to dev@example.com via smtp", @email.info
end

def test_sendmail_delivery_method_for_notify
assert_nothing_raised do
God::Contacts::Email.any_instance.expects(:notify_smtp).never
God::Contacts::Email.any_instance.expects(:notify_sendmail).once.returns(nil)
God::Contacts::Email.delivery_method = :sendmail
g = God::Contacts::Email.new
g.email = 'joe@example.com'
g.notify(:a, :b, :c, :d, :e)
assert_equal "sent email to joe@example.com", g.info
end
@email.delivery_method = :sendmail

God::Contacts::Email.any_instance.expects(:notify_smtp).never
God::Contacts::Email.any_instance.expects(:notify_sendmail).once.returns(nil)

@email.notify('msg', Time.now, 'prio', 'cat', 'host')
assert_equal "sent email to dev@example.com via sendmail", @email.info
end

end

0 comments on commit 1f31ef7

Please sign in to comment.