Class: DaemonKit::ErrorHandlers::Mail

Inherits:
Base show all
Defined in:
lib/daemon_kit/error_handlers/mail.rb

Overview

Send an email notification of the exception via SMTP.

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, instance

Class Attribute Details

.authenticationObject

Returns the value of attribute authentication.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def authentication
  @authentication
end

.domainObject

Returns the value of attribute domain.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def domain
  @domain
end

.hostObject

Returns the value of attribute host.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def host
  @host
end

.passwordObject

Returns the value of attribute password.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def password
  @password
end

.portObject

Returns the value of attribute port.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def port
  @port
end

.prefixObject

Returns the value of attribute prefix.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def prefix
  @prefix
end

.recipientsObject

Returns the value of attribute recipients.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def recipients
  @recipients
end

.senderObject

Returns the value of attribute sender.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def sender
  @sender
end

.tlsObject

Returns the value of attribute tls.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def tls
  @tls
end

.usernameObject

Returns the value of attribute username.



40
41
42
# File 'lib/daemon_kit/error_handlers/mail.rb', line 40

def username
  @username
end

Instance Method Details

#handle_exception(exception) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/daemon_kit/error_handlers/mail.rb', line 53

def handle_exception( exception )

  mail = TMail::Mail.new
  mail.to = self.class.recipients
  mail.from = self.class.sender
  mail.subject = "#{self.class.prefix} #{exception.message}"
  mail.set_content_type 'text', 'plain'
  mail.mime_version = '1.0'
  mail.date = Time.now

  mail.body = <<EOF
DaemonKit caught an exception inside #{DaemonKit.configuration.daemon_name}.

Message: #{exception.message}
Backtrace:
#{exception.backtrace.join("\n  ")}

Environment: #{ENV.inspect}
EOF
  begin
    smtp = Net::SMTP.new( self.class.host, self.class.port )
    smtp.enable_starttls_auto if self.class.tls && smtp.respond_to?(:enable_starttls_auto)
    smtp.start( self.class.domain, self.class.username, self.class.password,
                self.class.authentication ) do |smtp|
      smtp.sendmail( mail.to_s, mail.from, mail.to )
    end
  rescue => e
    DaemonKit.logger.error "Failed to send exception mail: #{e.message}" if DaemonKit.logger
  end
end