Permalink
Browse files

Add enable_starttls_auto and openssl_verify_mode options to mail noti…

…fications' config for SMTP transport
  • Loading branch information...
1 parent d3bbd95 commit 16d5e70530cc9144acbafba05548ca03a9c30256 @gi-lunaweb committed Jan 16, 2012
Showing with 42 additions and 28 deletions.
  1. +42 −28 lib/god/contacts/email.rb
View
@@ -1,30 +1,35 @@
# 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).
+# 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 Symbol authentication method. Possible values:
-# [ nil | :plain | :login | :cram_md5 ]
-# The default is nil, which means no authentication. To
-# enable authentication, pass the appropriate symbol and
-# then pass the appropriate SMTP Auth Options (below).
+# 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 Symbol authentication method. Possible values:
+# [ nil | :plain | :login | :cram_md5 ]
+# The default is nil, which means no authentication. To
+# enable authentication, pass the appropriate symbol and
+# then pass the appropriate SMTP Auth Options (below).
+# enable_starttls_auto - Enables SMTP/TLS (STARTTLS) if server accepts
+# (default: false)
+# openssl_verify_mode - OpenSSL verify mode used to validate certificates
+# if needed. Accept an OpenSSL verify mode constant like
+# OpenSSL::SSL::VERIFY_NONE (default: nil)
#
# === SMTP Auth Options (when server_auth != nil) ===
-# server_domain - The String domain.
-# server_user - The String username.
-# server_password - The String password.
+# 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").
+# 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'
@@ -36,6 +41,7 @@ class Email < Contact
class << self
attr_accessor :to_email, :to_name, :from_email, :from_name,
:delivery_method, :server_host, :server_port,
+ :enable_starttls_auto, :openssl_verify_mode,
:server_auth, :server_domain, :server_user,
:server_password, :sendmail_path, :sendmail_args
attr_accessor :format
@@ -44,6 +50,8 @@ class << self
self.from_email = 'god@example.com'
self.from_name = 'God Process Monitoring'
self.delivery_method = :smtp
+ self.enable_starttls_auto = false
+ self.openssl_verify_mode = nil
self.server_auth = nil
self.server_host = 'localhost'
self.server_port = 25
@@ -105,16 +113,22 @@ def notify(message, time, priority, category, host)
end
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)
+ smtp = Net::SMTP.new(arg(:server_host), arg(:server_port))
+
+ if arg(:enable_starttls_auto)
+ if smtp.respond_to?(:enable_starttls_auto)
+ unless arg(:openssl_verify_mode)
+ smtp.enable_starttls_auto
+ else
+ context = Net::SMTP.default_ssl_context
+ context.verify_mode = arg(:openssl_verify_mode)
+ smtp.enable_starttls_auto(context)
+ end
+ end
end
-
- Net::SMTP.start(*args) do |smtp|
- smtp.send_message(mail, arg(:from_email), arg(:to_email))
+
+ smtp.start(arg(:server_domain), arg(:server_user), arg(:server_password), arg(:server_auth)) do |smtp|
+ smtp.send_message(mail, arg(:from_email), [arg(:to_email)] )
end
end

0 comments on commit 16d5e70

Please sign in to comment.