Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #41 from carlhoerberg/master

Replaced TMail with Mail in MailExceptions
  • Loading branch information...
commit c72616700b849b2f0b519100cf244a899e945c0d 2 parents f8ec6b7 + df0baec
@rkh rkh authored
Showing with 26 additions and 46 deletions.
  1. +16 −34 lib/rack/contrib/mailexceptions.rb
  2. +10 −12 test/spec_rack_mailexceptions.rb
View
50 lib/rack/contrib/mailexceptions.rb
@@ -1,5 +1,5 @@
require 'net/smtp'
-require 'tmail'
+require 'mail'
require 'erb'
module Rack
@@ -14,10 +14,10 @@ def initialize(app)
@app = app
@config = {
:to => nil,
- :from => ENV['USER'] || 'rack',
+ :from => ENV['USER'] || 'rack@localhost',
:subject => '[exception] %s',
:smtp => {
- :server => 'localhost',
+ :address => 'localhost',
:domain => 'localhost',
:port => 25,
:authentication => :login,
@@ -34,8 +34,6 @@ def call(env)
begin
@app.call(env)
rescue => boom
- # TODO don't allow exceptions from send_notification to
- # propogate
send_notification boom, env
raise
end
@@ -53,41 +51,25 @@ def smtp(settings={})
private
def generate_mail(exception, env)
- mail = TMail::Mail.new
- mail.to = Array(config[:to])
- mail.from = config[:from]
- mail.subject = config[:subject] % [exception.to_s]
- mail.date = Time.now
- mail.set_content_type 'text/plain'
- mail.charset = 'UTF-8'
- mail.body = @template.result(binding)
- mail
+ mail = Mail.new({
+ :from => config[:from],
+ :to => config[:to],
+ :subject => config[:subject] % [exception.to_s],
+ :body => @template.result(binding)
+ })
end
def send_notification(exception, env)
mail = generate_mail(exception, env)
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
+ mail.deliver!
env['mail.sent'] = true
- return if smtp[:server] == 'example.com'
-
- server = service.new(smtp[:server], smtp[:port])
-
- if smtp[:enable_starttls_auto] == :auto
- server.enable_starttls_auto
- elsif smtp[:enable_starttls_auto]
- server.enable_starttls
- end
-
- server.start smtp[:domain], smtp[:user_name], smtp[:password], smtp[:authentication]
-
- mail.to.each do |recipient|
- server.send_message mail.to_s, mail.from, recipient
- end
- end
-
- def service
- Net::SMTP
- end
+ mail
+ end
def extract_body(env)
if io = env['rack.input']
View
22 test/spec_rack_mailexceptions.rb
@@ -2,10 +2,8 @@
require 'rack/mock'
begin
- require 'tmail'
- require 'rack/contrib/mailexceptions'
-
- require File.dirname(__FILE__) + '/mail_settings.rb'
+ require './lib/rack/contrib/mailexceptions'
+ require './test/mail_settings.rb'
class TestError < RuntimeError
end
@@ -57,13 +55,13 @@ def test_exception
mail.smtp @smtp_settings
end
- tmail = mailer.send(:generate_mail, test_exception, @env)
- tmail.to.should.equal ['foo@example.org']
- tmail.from.should.equal ['bar@example.org']
- tmail.subject.should.equal '[ERROR] Suffering Succotash!'
- tmail.body.should.not.be.nil
- tmail.body.should.be =~ /FOO:\s+"BAR"/
- tmail.body.should.be =~ /^\s*THE BODY\s*$/
+ mail = mailer.send(:generate_mail, test_exception, @env)
+ mail.to.should.equal ['foo@example.org']
+ mail.from.should.equal ['bar@example.org']
+ mail.subject.should.equal '[ERROR] Suffering Succotash!'
+ mail.body.should.not.be.nil
+ mail.body.should.be =~ /FOO:\s+"BAR"/
+ mail.body.should.be =~ /^\s*THE BODY\s*$/
end
specify 'catches exceptions raised from app, sends mail, and re-raises' do
@@ -167,5 +165,5 @@ def self.tls; @@_tls; end
end
end
rescue LoadError => boom
- STDERR.puts "WARN: Skipping Rack::MailExceptions tests (tmail not installed)"
+ STDERR.puts "WARN: Skipping Rack::MailExceptions tests (mail not installed)"
end
Please sign in to comment.
Something went wrong with that request. Please try again.