Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add subscriber to ActionMailer.

  • Loading branch information...
commit 2a6bc1263e99060897b53a8c806916d198eab572 1 parent d017167
@josevalim josevalim authored
View
19 actionmailer/lib/action_mailer/base.rb
@@ -399,9 +399,10 @@ def method_missing(method_symbol, *parameters) #:nodoc:
# end
# end
def receive(raw_email)
- logger.info "Received mail:\n #{raw_email}" unless logger.nil?
mail = Mail.new(raw_email)
- new.receive(mail)
+ ActiveSupport::Notifications.instrument("action_mailer.receive", :mail => mail) do
+ new.receive(mail)
+ end
end
# Deliver the given mail object directly. This can be used to deliver
@@ -494,17 +495,13 @@ def process(method_name, *args)
def deliver!(mail = @mail)
raise "no mail object available for delivery!" unless mail
- if logger
- logger.info "Sent mail to #{Array(recipients).join(', ')}"
- logger.debug "\n#{mail.encoded}"
- end
-
- ActiveSupport::Notifications.instrument("action_mailer.deliver", :mail => self) do
- begin
+ begin
+ ActiveSupport::Notifications.instrument("action_mailer.deliver",
+ :mail => @mail, :mailer => self) do
self.delivery_method.perform_delivery(mail) if perform_deliveries
- rescue Exception => e # Net::SMTP errors or sendmail pipe errors
- raise e if raise_delivery_errors
end
+ rescue Exception => e # Net::SMTP errors or sendmail pipe errors
+ raise e if raise_delivery_errors
end
mail
View
3  actionmailer/lib/action_mailer/railtie.rb
@@ -5,6 +5,9 @@ module ActionMailer
class Railtie < Rails::Railtie
plugin_name :action_mailer
+ require "action_mailer/railties/subscriber"
+ subscriber ActionMailer::Railties::Subscriber.new
+
initializer "action_mailer.set_configs" do |app|
app.config.action_mailer.each do |k,v|
ActionMailer::Base.send "#{k}=", v
View
20 actionmailer/lib/action_mailer/railties/subscriber.rb
@@ -0,0 +1,20 @@
+module ActionMailer
+ module Railties
+ class Subscriber < Rails::Subscriber
+ def deliver(event)
+ recipients = Array(event.payload[:mailer].recipients).join(', ')
+ info("Sent mail to #{recipients} (%1.fms)" % event.duration)
+ debug("\n#{event.payload[:mail].encoded}")
+ end
+
+ def receive(event)
+ info("Received mail (%.1fms)" % event.duration)
+ debug("\n#{event.payload[:mail].encoded}")
+ end
+
+ def logger
+ ActionMailer::Base.logger
+ end
+ end
+ end
+end
View
34 actionmailer/test/mail_service_test.rb
@@ -688,40 +688,6 @@ def test_performs_delivery_via_sendmail
TestMailer.deliver_signed_up(@recipient)
end
- class FakeLogger
- attr_reader :info_contents, :debug_contents
-
- def initialize
- @info_contents, @debug_contents = "", ""
- end
-
- def info(str = nil, &blk)
- @info_contents << str if str
- @info_contents << blk.call if block_given?
- end
-
- def debug(str = nil, &blk)
- @debug_contents << str if str
- @debug_contents << blk.call if block_given?
- end
- end
-
- def test_delivery_logs_sent_mail
- mail = TestMailer.create_signed_up(@recipient)
- # logger = mock()
- # logger.expects(:info).with("Sent mail to #{@recipient}")
- # logger.expects(:debug).with("\n#{mail.encoded}")
- TestMailer.logger = FakeLogger.new
- TestMailer.deliver_signed_up(@recipient)
- assert(TestMailer.logger.info_contents =~ /Sent mail to #{@recipient}/)
- expected = TestMailer.logger.debug_contents
- actual = "\n#{mail.encoded}"
- expected.gsub!(/Message-ID:.*\r\n/, "Message-ID: <123@456>\r\n")
- actual.gsub!(/Message-ID:.*\r\n/, "Message-ID: <123@456>\r\n")
-
- assert_equal(expected, actual)
- end
-
def test_unquote_quoted_printable_subject
msg = <<EOF
From: me@example.com
View
53 actionmailer/test/subscriber_test.rb
@@ -0,0 +1,53 @@
+require "abstract_unit"
+require "rails/subscriber/test_helper"
+require "action_mailer/railties/subscriber"
+
+module SubscriberTest
+ Rails::Subscriber.add(:action_mailer, ActionMailer::Railties::Subscriber.new)
+
+ class TestMailer < ActionMailer::Base
+ def basic
+ recipients "somewhere@example.com"
+ subject "basic"
+ from "basic@example.com"
+ render :text => "Hello world"
+ end
+
+ def receive(mail)
+ # Do nothing
+ end
+ end
+
+ def set_logger(logger)
+ ActionMailer::Base.logger = logger
+ end
+
+ def test_deliver_is_notified
+ TestMailer.deliver_basic
+ wait
+ assert_equal 1, @logger.logged(:info).size
+ assert_match /Sent mail to somewhere@example.com/, @logger.logged(:info).first
+ assert_equal 1, @logger.logged(:debug).size
+ assert_match /Hello world/, @logger.logged(:debug).first
+ end
+
+ def test_receive_is_notifier
+ fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email")
+ TestMailer.receive(fixture)
+ wait
+ assert_equal 1, @logger.logged(:info).size
+ assert_match /Received mail/, @logger.logged(:info).first
+ assert_equal 1, @logger.logged(:debug).size
+ assert_match /Jamis/, @logger.logged(:debug).first
+ end
+
+ class SyncSubscriberTest < ActionMailer::TestCase
+ include Rails::Subscriber::SyncTestHelper
+ include SubscriberTest
+ end
+
+ class AsyncSubscriberTest < ActionMailer::TestCase
+ include Rails::Subscriber::AsyncTestHelper
+ include SubscriberTest
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.