class Rack::MailExceptions

use smtp

use Rack::MailExceptions do |mail|
  mail.to 'test@gmail.com'
  mail.smtp :address => 'mail.test.com', :user_name => 'test@test.com', :password => 'test'
end

use sendmail

use Rack::MailExceptions do |mail|
  mail.to 'test@gmail.com'
  mail.smtp false
end

Constants

TEMPLATE

Attributes

config[R]

Public Class Methods

new(app) { |self| ... } click to toggle source
# File lib/rack/contrib/mailexceptions.rb, line 24
def initialize(app)
  @app = app
  @config = {
    :to      => nil,
    :from    => ENV['USER'] || 'rack@localhost',
    :subject => '[exception] %s',
    :smtp    => {
      :address        => 'localhost',
      :domain         => 'localhost',
      :port           => 25,
      :authentication => :login,
      :user_name      => nil,
      :password       => nil
    }
  }
  @template = ERB.new(TEMPLATE)
  @test_mode = false
  yield self if block_given?
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/contrib/mailexceptions.rb, line 44
def call(env)
  status, headers, body =
    begin
      @app.call(env)
    rescue => boom
      send_notification boom, env
      raise
    end
  send_notification env['mail.exception'], env if env['mail.exception']
  [status, headers, body]
end
disable_test_mode() click to toggle source
# File lib/rack/contrib/mailexceptions.rb, line 72
def disable_test_mode
  @test_mode = false
end
enable_test_mode() click to toggle source
# File lib/rack/contrib/mailexceptions.rb, line 68
def enable_test_mode
  @test_mode = true
end
smtp(settings={}) click to toggle source
# File lib/rack/contrib/mailexceptions.rb, line 60
def smtp(settings={})
  if settings
    @config[:smtp].merge! settings
  else
    @config[:smtp] = nil
  end
end

Private Instance Methods

extract_body(env) click to toggle source
# File lib/rack/contrib/mailexceptions.rb, line 105
def extract_body(env)
  if io = env['rack.input']
    io.rewind if io.respond_to?(:rewind)
    io.read
  end
end
generate_mail(exception, env) click to toggle source
# File lib/rack/contrib/mailexceptions.rb, line 77
def generate_mail(exception, env)
  Mail.new({
    :from => config[:from],
    :to => config[:to],
    :subject => config[:subject] % [exception.to_s],
    :body => @template.result(binding),
    :charset => "UTF-8"
  })
end
send_notification(exception, env) click to toggle source
# File lib/rack/contrib/mailexceptions.rb, line 87
def send_notification(exception, env)
  mail = generate_mail(exception, env)
  if @test_mode
    mail.delivery_method :test
  elsif config[:smtp]
    smtp = config[:smtp]
    # for backward compability, replace the :server key with :address
    address = smtp.delete :server
    smtp[:address] = address if address
    mail.delivery_method :smtp, smtp
  else
    mail.delivery_method :sendmail
  end
  mail.deliver!
  env['mail.sent'] = true
  mail
end