From 383b8bc8e215d5fa2b018d6f0b9c363ea95251cf Mon Sep 17 00:00:00 2001 From: Yoshiyuki Kinjo Date: Sat, 8 Sep 2018 17:14:12 +0900 Subject: Skip delivery notification when perform_deliveries is false. --- actionmailer/CHANGELOG.md | 4 ++++ actionmailer/lib/action_mailer/base.rb | 1 + actionmailer/test/log_subscriber_test.rb | 11 +++++++++++ actionmailer/test/mailers/base_mailer.rb | 5 +++++ guides/source/action_mailer_basics.md | 2 +- 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index 1468a89e96..415c54ec70 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -1,3 +1,7 @@ +* Skip delivery notification when `perform_deliveries` is false. + + *Yoshiyuki Kinjo* + * Allow call `assert_enqueued_email_with` with no block. Example: diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 7f22af83b0..1ab2c55a6c 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -579,6 +579,7 @@ module ActionMailer # calling +deliver_mail+ directly and passing a Mail::Message will do # nothing except tell the logger you sent the email. def deliver_mail(mail) #:nodoc: + return unless mail.perform_deliveries ActiveSupport::Notifications.instrument("deliver.action_mailer") do |payload| set_payload_for_mail(payload, mail) yield # Let Mail do the delivery actions diff --git a/actionmailer/test/log_subscriber_test.rb b/actionmailer/test/log_subscriber_test.rb index 2e89758dfb..48edb80c0c 100644 --- a/actionmailer/test/log_subscriber_test.rb +++ b/actionmailer/test/log_subscriber_test.rb @@ -37,6 +37,17 @@ class AMLogSubscriberTest < ActionMailer::TestCase BaseMailer.deliveries.clear end + def test_deliver_is_not_notified_when_perform_deliveries_is_false + BaseMailer.welcome_without_deliveries.deliver_now + wait + + assert_equal(0, @logger.logged(:info).size) + assert_equal(1, @logger.logged(:debug).size) + assert_match(/BaseMailer#welcome_without_deliveries: processed outbound mail in [\d.]+ms/, @logger.logged(:debug).first) + ensure + BaseMailer.deliveries.clear + end + def test_receive_is_notified fixture = File.read(File.expand_path("fixtures/raw_email", __dir__)) TestMailer.receive(fixture) diff --git a/actionmailer/test/mailers/base_mailer.rb b/actionmailer/test/mailers/base_mailer.rb index bfaecdb658..a3101207dc 100644 --- a/actionmailer/test/mailers/base_mailer.rb +++ b/actionmailer/test/mailers/base_mailer.rb @@ -21,6 +21,11 @@ class BaseMailer < ActionMailer::Base mail(template_name: "welcome", template_path: path) end + def welcome_without_deliveries + mail(template_name: "welcome") + mail.perform_deliveries = false + end + def html_only(hash = {}) mail(hash) end diff --git a/guides/source/action_mailer_basics.md b/guides/source/action_mailer_basics.md index 37cbf3f53d..406615f681 100644 --- a/guides/source/action_mailer_basics.md +++ b/guides/source/action_mailer_basics.md @@ -787,7 +787,7 @@ files (environment.rb, production.rb, etc...) |`sendmail_settings`|Allows you to override options for the `:sendmail` delivery method.| |`raise_delivery_errors`|Whether or not errors should be raised if the email fails to be delivered. This only works if the external email server is configured for immediate delivery.| |`delivery_method`|Defines a delivery method. Possible values are:See [API docs](http://api.rubyonrails.org/classes/ActionMailer/Base.html) for more info.| -|`perform_deliveries`|Determines whether deliveries are actually carried out when the `deliver` method is invoked on the Mail message. By default they are, but this can be turned off to help functional testing.| +|`perform_deliveries`|Determines whether deliveries are actually carried out when the `deliver` method is invoked on the Mail message. By default they are, but this can be turned off to help functional testing. If this value is `false`, `deliveries` array will not be populated even if delivery_method is :test.| |`deliveries`|Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful for unit and functional testing.| |`default_options`|Allows you to set default values for the `mail` method options (`:from`, `:reply_to`, etc.).| -- cgit v1.2.3 From 576209b45bf6d20e91562da77e454bc58bda3658 Mon Sep 17 00:00:00 2001 From: Yoshiyuki Kinjo Date: Sun, 9 Sep 2018 20:16:20 +0900 Subject: Add `perform_deliveries` to a payload of `deliver.action_mailer` notification. --- actionmailer/CHANGELOG.md | 6 +++++- actionmailer/lib/action_mailer/base.rb | 20 +++++++++--------- actionmailer/lib/action_mailer/log_subscriber.rb | 7 ++++++- actionmailer/test/log_subscriber_test.rb | 9 +++++--- guides/source/active_support_instrumentation.md | 26 +++++++++++++----------- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index 415c54ec70..5158f23a27 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -1,4 +1,8 @@ -* Skip delivery notification when `perform_deliveries` is false. +* Add `perform_deliveries` to a payload of `deliver.action_mailer` notification. + + *Yoshiyuki Kinjo* + +* Change delivery logging message when `perform_deliveries` is false. *Yoshiyuki Kinjo* diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 1ab2c55a6c..55f701b18e 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -579,7 +579,6 @@ module ActionMailer # calling +deliver_mail+ directly and passing a Mail::Message will do # nothing except tell the logger you sent the email. def deliver_mail(mail) #:nodoc: - return unless mail.perform_deliveries ActiveSupport::Notifications.instrument("deliver.action_mailer") do |payload| set_payload_for_mail(payload, mail) yield # Let Mail do the delivery actions @@ -589,15 +588,16 @@ module ActionMailer private def set_payload_for_mail(payload, mail) - payload[:mailer] = name - payload[:message_id] = mail.message_id - payload[:subject] = mail.subject - payload[:to] = mail.to - payload[:from] = mail.from - payload[:bcc] = mail.bcc if mail.bcc.present? - payload[:cc] = mail.cc if mail.cc.present? - payload[:date] = mail.date - payload[:mail] = mail.encoded + payload[:mailer] = name + payload[:message_id] = mail.message_id + payload[:subject] = mail.subject + payload[:to] = mail.to + payload[:from] = mail.from + payload[:bcc] = mail.bcc if mail.bcc.present? + payload[:cc] = mail.cc if mail.cc.present? + payload[:date] = mail.date + payload[:mail] = mail.encoded + payload[:perform_deliveries] = mail.perform_deliveries end def method_missing(method_name, *args) diff --git a/actionmailer/lib/action_mailer/log_subscriber.rb b/actionmailer/lib/action_mailer/log_subscriber.rb index 87cfbfff28..25c99342c2 100644 --- a/actionmailer/lib/action_mailer/log_subscriber.rb +++ b/actionmailer/lib/action_mailer/log_subscriber.rb @@ -9,8 +9,13 @@ module ActionMailer # An email was delivered. def deliver(event) info do + perform_deliveries = event.payload[:perform_deliveries] recipients = Array(event.payload[:to]).join(", ") - "Sent mail to #{recipients} (#{event.duration.round(1)}ms)" + if perform_deliveries + "Sent mail to #{recipients} (#{event.duration.round(1)}ms)" + else + "Skipped sending mail to #{recipients} as `perform_deliveries` is false" + end end debug { event.payload[:mail] } diff --git a/actionmailer/test/log_subscriber_test.rb b/actionmailer/test/log_subscriber_test.rb index 48edb80c0c..7686fd10c9 100644 --- a/actionmailer/test/log_subscriber_test.rb +++ b/actionmailer/test/log_subscriber_test.rb @@ -37,13 +37,16 @@ class AMLogSubscriberTest < ActionMailer::TestCase BaseMailer.deliveries.clear end - def test_deliver_is_not_notified_when_perform_deliveries_is_false + def test_deliver_message_when_perform_deliveries_is_false BaseMailer.welcome_without_deliveries.deliver_now wait - assert_equal(0, @logger.logged(:info).size) - assert_equal(1, @logger.logged(:debug).size) + assert_equal(1, @logger.logged(:info).size) + assert_match("Skipped sending mail to system@test.lindsaar.net as `perform_deliveries` is false", @logger.logged(:info).first) + + assert_equal(2, @logger.logged(:debug).size) assert_match(/BaseMailer#welcome_without_deliveries: processed outbound mail in [\d.]+ms/, @logger.logged(:debug).first) + assert_match("Welcome", @logger.logged(:debug).second) ensure BaseMailer.deliveries.clear end diff --git a/guides/source/active_support_instrumentation.md b/guides/source/active_support_instrumentation.md index 8581817d71..69c6a6e414 100644 --- a/guides/source/active_support_instrumentation.md +++ b/guides/source/active_support_instrumentation.md @@ -319,17 +319,18 @@ Action Mailer ### deliver.action_mailer -| Key | Value | -| ------------- | -------------------------------------------- | -| `:mailer` | Name of the mailer class | -| `:message_id` | ID of the message, generated by the Mail gem | -| `:subject` | Subject of the mail | -| `:to` | To address(es) of the mail | -| `:from` | From address of the mail | -| `:bcc` | BCC addresses of the mail | -| `:cc` | CC addresses of the mail | -| `:date` | Date of the mail | -| `:mail` | The encoded form of the mail | +| Key | Value | +| --------------------- | ---------------------------------------------------- | +| `:mailer` | Name of the mailer class | +| `:message_id` | ID of the message, generated by the Mail gem | +| `:subject` | Subject of the mail | +| `:to` | To address(es) of the mail | +| `:from` | From address of the mail | +| `:bcc` | BCC addresses of the mail | +| `:cc` | CC addresses of the mail | +| `:date` | Date of the mail | +| `:mail` | The encoded form of the mail | +| `:perform_deliveries` | Whether delivery of this message is performed or not | ```ruby { @@ -339,7 +340,8 @@ Action Mailer to: ["users@rails.com", "dhh@rails.com"], from: ["me@rails.com"], date: Sat, 10 Mar 2012 14:18:09 +0100, - mail: "..." # omitted for brevity + mail: "...", # omitted for brevity + perform_deliveries: true } ``` -- cgit v1.2.3