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
26 def initialize(app)
27   @app = app
28   @config = {
29     :to      => nil,
30     :from    => ENV['USER'] || 'rack@localhost',
31     :subject => '[exception] %s',
32     :smtp    => {
33       :address        => 'localhost',
34       :domain         => 'localhost',
35       :port           => 25,
36       :authentication => :login,
37       :user_name      => nil,
38       :password       => nil
39     }
40   }
41   @template = ERB.new(TEMPLATE)
42   @test_mode = false
43   yield self if block_given?
44 end

Public Instance Methods

call(env) click to toggle source
   # File lib/rack/contrib/mailexceptions.rb
46 def call(env)
47   status, headers, body =
48     begin
49       @app.call(env)
50     rescue => boom
51       send_notification boom, env
52       raise
53     end
54   send_notification env['mail.exception'], env if env['mail.exception']
55   [status, headers, body]
56 end
disable_test_mode() click to toggle source
   # File lib/rack/contrib/mailexceptions.rb
74 def disable_test_mode
75   @test_mode = false
76 end
enable_test_mode() click to toggle source
   # File lib/rack/contrib/mailexceptions.rb
70 def enable_test_mode
71   @test_mode = true
72 end
smtp(settings={}) click to toggle source
   # File lib/rack/contrib/mailexceptions.rb
62 def smtp(settings={})
63   if settings
64     @config[:smtp].merge! settings
65   else
66     @config[:smtp] = nil
67   end
68 end

Private Instance Methods

extract_body(env) click to toggle source
    # File lib/rack/contrib/mailexceptions.rb
107 def extract_body(env)
108   if io = env['rack.input']
109     io.rewind
110     io.read
111   end
112 end
generate_mail(exception, env) click to toggle source
   # File lib/rack/contrib/mailexceptions.rb
79 def generate_mail(exception, env)
80   Mail.new({
81     :from => config[:from],
82     :to => config[:to],
83     :subject => config[:subject] % [exception.to_s],
84     :body => @template.result(binding),
85     :charset => "UTF-8"
86   })
87 end
send_notification(exception, env) click to toggle source
    # File lib/rack/contrib/mailexceptions.rb
 89 def send_notification(exception, env)
 90   mail = generate_mail(exception, env)
 91   if @test_mode
 92     mail.delivery_method :test
 93   elsif config[:smtp]
 94     smtp = config[:smtp]
 95     # for backward compatibility, replace the :server key with :address
 96     address = smtp.delete :server
 97     smtp[:address] = address if address
 98     mail.delivery_method :smtp, smtp
 99   else
100     mail.delivery_method :sendmail
101   end
102   mail.deliver!
103   env['mail.sent'] = true
104   mail
105 end