Skip to content

Commit 52db7f2

Browse files
committed
Configure a default timeout for the smtp delivery method
The default is set to 5 and only applied for new applications or applications that opt-in for this new default. Closes rails#42089. [André Luis Leal Cardoso Junior + Rafael Mendonça França]
1 parent cbcface commit 52db7f2

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

actionmailer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
* Configures a default of 5 for both `open_timeout` and `read_timeout` for SMTP Settings.
12

3+
*André Luis Leal Cardoso Junior*
24

35

46
Please check [6-1-stable](https://github.com/rails/rails/blob/6-1-stable/actionmailer/CHANGELOG.md) for previous changes.

actionmailer/lib/action_mailer/railtie.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ class Railtie < Rails::Railtie # :nodoc:
4545
self.delivery_job = delivery_job.constantize
4646
end
4747

48+
if smtp_settings = options.delete(:smtp_settings)
49+
self.smtp_settings = smtp_settings
50+
end
51+
52+
if smtp_timeout = options.delete(:smtp_timeout)
53+
self.smtp_settings[:open_timeout] ||= smtp_timeout
54+
self.smtp_settings[:read_timeout] ||= smtp_timeout
55+
end
56+
4857
options.each { |k, v| send("#{k}=", v) }
4958
end
5059

guides/source/configuring.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,9 @@ There are a number of settings available on `config.action_mailer`:
784784
* `:open_timeout` - Number of seconds to wait while attempting to open a connection.
785785
* `:read_timeout` - Number of seconds to wait until timing-out a read(2) call.
786786

787+
* `config.action_mailer.smtp_timeout` allows to configure both the `:open_timeout` and `:read_timeout`
788+
values for `:smtp` delivery method.
789+
787790
* `config.action_mailer.sendmail_settings` allows detailed configuration for the `sendmail` delivery method. It accepts a hash of options, which can include any of these options:
788791
* `:location` - The location of the sendmail executable. Defaults to `/usr/sbin/sendmail`.
789792
* `:arguments` - The command line arguments. Defaults to `-i`.
@@ -1063,6 +1066,7 @@ text/javascript image/svg+xml application/postscript application/x-shockwave-fla
10631066
- `config.active_support.hash_digest_class`: `OpenSSL::Digest::SHA256`
10641067
- `config.active_support.cache_format_version`: `7.0`
10651068
- `config.action_dispatch.return_only_request_media_type_on_content_type`: `false`
1069+
- `config.action_mailer.smtp_timeout`: `5`
10661070

10671071
#### For '6.1', defaults from previous versions below and:
10681072

@@ -1141,6 +1145,7 @@ text/javascript image/svg+xml application/postscript application/x-shockwave-fla
11411145
- `config.active_support.cache_format_version`: `6.1`
11421146
- `config.action_dispatch.return_only_request_media_type_on_content_type`: `true`
11431147
- `ActiveSupport.utc_to_local_returns_utc_offset_times`: `false`
1148+
- `config.action_mailer.smtp_timeout`: `nil`
11441149

11451150
### Configuring a Database
11461151

railties/lib/rails/application/configuration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ def load_defaults(target_version)
212212
active_support.remove_deprecated_time_with_zone_name = true
213213
active_support.cache_format_version = 7.0
214214
end
215+
216+
if respond_to?(:action_mailer)
217+
action_mailer.smtp_timeout = 5
218+
end
215219
else
216220
raise "Unknown version #{target_version.to_s.inspect}"
217221
end

railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_0.rb.tt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@
3434
# Only change this value after your application is fully deployed to Rails 7.0
3535
# and you have no plans to rollback.
3636
# config.active_support.cache_format_version = 7.0
37+
38+
# Set both the `:open_timeout` and `:read_timeout` values for `:smtp` delivery method.
39+
# Rails.application.config.action_mailer.smtp_timeout = 5

railties/test/application/configuration_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,25 @@ def index
26662666
assert_equal 308, Rails.application.config.action_dispatch.ssl_default_redirect_status
26672667
end
26682668

2669+
test "Rails.application.config.action_mailer.smtp_settings have open_timeout and read_timeout defined as 5 in 7.0 defaults" do
2670+
remove_from_config '.*config\.load_defaults.*\n'
2671+
add_to_config 'config.load_defaults "7.0"'
2672+
2673+
app "development"
2674+
2675+
assert_equal 5, ActionMailer::Base.smtp_settings[:open_timeout]
2676+
assert_equal 5, ActionMailer::Base.smtp_settings[:read_timeout]
2677+
end
2678+
2679+
test "Rails.application.config.action_mailer.smtp_settings does not have open_timeout and read_timeout configured on other versions" do
2680+
remove_from_config '.*config\.load_defaults.*\n'
2681+
2682+
app "development"
2683+
2684+
assert_nil ActionMailer::Base.smtp_settings[:open_timeout]
2685+
assert_nil ActionMailer::Base.smtp_settings[:read_timeout]
2686+
end
2687+
26692688
test "ActiveSupport.utc_to_local_returns_utc_offset_times is true in 6.1 defaults" do
26702689
remove_from_config '.*config\.load_defaults.*\n'
26712690
add_to_config 'config.load_defaults "6.1"'

0 commit comments

Comments
 (0)