Sha256: 66a9c44c7366e17eeaf8b53095b09b4fdc872a50a2de118416b84e5164c7a0ca

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require 'mail'

module Spanx
  module Notifier
    class Email < Base

      attr_reader :config, :thread

      def initialize(config)
        @config = config[:email]

        configure_email_gateway
      end

      def publish(blocked_ip)
        return unless enabled?

        @thread = Thread.new do
          Thread.current[:name] = "email notifier"
          Logger.log "sending notification email for #{blocked_ip.identifier}"

          mail = Mail.new
          mail.to = config[:to]
          mail.from = config[:from]
          mail.subject = subject(blocked_ip)
          mail.body = generate_block_ip_message(blocked_ip)

          mail.deliver
        end
      end

      def enabled?
        config && config[:enabled]
      end

      private

      def subject(blocked_ip)
        "#{config[:subject] || "IP Blocked:"} #{blocked_ip.identifier}"
      end

      def configure_email_gateway
        return unless enabled?

        Mail.defaults do
          delivery_method :smtp, {}
        end

        settings = Mail::Configuration.instance.delivery_method.settings
        settings[:address] = config[:gateway]
        settings[:port] = '587'
        settings[:domain] = config[:domain]
        settings[:user_name] = config[:from]
        settings[:password] = config[:password]
        settings[:authentication] = :plain
        settings[:enable_starttls_auto] = true
        settings[:openssl_verify_mode] = 'none'
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spanx-0.3.0 lib/spanx/notifier/email.rb
spanx-0.1.1 lib/spanx/notifier/email.rb
spanx-0.1.0 lib/spanx/notifier/email.rb